If I want to select an option of a dropdown box, there are several ways to do that. I always used:
driver.findElement(By.id("selection")).sendKeys("Germany");
But that didn't work every time. Sometimes another option was selected. So I googled a little bit and found this piece of code which works every time:
WebElement select = driver.findElement(By.id("selection"));
List<WebElement> options = select.findElements(By.tagName("option"));
for (WebElement option : options) {
if("Germany".equals(option.getText()))
option.click();
}
But that works really really slow. If I have a long list with many items in it, it really takes too much time. So my question is, is there a solution which works every time and is fast?
For some strange reason the
SelectElement
for webdriver (version 2.25.1.0) does not properly work with the firefoxdriver (Firefox 15). Sometimes it may not select an option from a dropdownlist. It does, however, seem to work with the chromedriver... This is a link to the chromedriver... just drop it in the bin dir.Just wrap your WebElement into Select Object as shown below
Once this is done you can select the required value in 3 ways. Consider an HTML file like this
Now to identify dropdown do
To select its option say 'Programmer' you can do
or
or
Happy Coding :)
Try the following:
You can use this