Pages

Monday, 9 December 2013

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: