Pages

Sunday 8 December 2013

How I can select a value in dropdownlist that is not visible in selenium?

As display is set to 'none' for 'Select' tag, Selenium is unable to identify dropdown list values.
One way of handling is by clicking that dropdown and enabling the list.
Other way is by executing JavaScript to enable the dropdown list and then selecting value using WebDriver's 'Select' API.

Here is the code to handle using JavaScript:
 JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById('country').style.display='block';");

//Then Select required value
Select dropdown = new Select(driver.findElement(By.id("country")));
dropdown.selectByVisibleText("India");  


Assuming HTML code for select is as below:

8 comments:

  1. Its a query?

    What is heightened privileges browsers?

    http://www.bestqtptraining.com/

    ReplyDelete
  2. very helpful it works!!!!!!!!!!!!!!!!!!

    ReplyDelete
  3. very helpful it works!!!!!!!!!!!!!!!!!!

    ReplyDelete
  4. I am getting as Javascript Error in my selenium script if i use above code.

    ReplyDelete
  5. Trade FX At Home On Your PC: roboforex login Is A Forex Trading Company. The Company States That You Can Make On Average 80 – 300 Pips Per Trade. roboforex login States That It Is Simple And Easy To Get Started.

    ReplyDelete