If I want to select an option of a dropdown box, there are several ways to do that. I always used:
driver.findElem
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
Select dropdown = new Select(driver.findElement(By.id("identifier")));
Once this is done you can select the required value in 3 ways. Consider an HTML file like this
<html>
<body>
<select id = "designation">
<option value = "MD">MD</option>
<option value = "prog"> Programmer </option>
<option value = "CEO"> CEO </option>
</option>
</select>
<body>
</html>
Now to identify dropdown do
Select dropdown = new Select(driver.findElement(By.id("designation")));
To select its option say 'Programmer' you can do
dropdown.selectByVisibleText("Programmer ");
or
dropdown.selectByIndex(1);
or
dropdown.selectByValue("prog");
Happy Coding :)
select = driver.FindElement(By.CssSelector("select[uniq id']"));
selectElement = new SelectElement(select);
var optionList =
driver.FindElements(By.CssSelector("select[uniq id']>option"));
selectElement.SelectByText(optionList[GenerateRandomNumber(1, optionList.Count())].Text);
Try the following:
import org.openqa.selenium.support.ui.Select;
Select droplist = new Select(driver.findElement(By.Id("selection")));
droplist.selectByVisibleText("Germany");
I have to struggle to find how to achieve especial those who are new to this tool (like me)
C# code:
IWebElement ddl = ffDriver.FindElement(By.Id("ddlGoTo"));
OpenQA.Selenium.Support.UI.SelectElement clickthis = new OpenQA.Selenium.Support.UI.SelectElement(ddl);
clickthis.SelectByText("Your Text");
hope this help others!
Try the Select helper class and see if that makes any difference?
String valueToSelect= "Germany";
WebElement select = driver.findElement(By.id("selection"));
Select dropDown = new Select(select);
String selected = dropDown.getFirstSelectedOption().getText();
if(selected.equals(valueToSelect)) {//do stuff already selected}
List<WebElement> Options = dropDown.getOptions();
for(WebElement option:Options){
if(option.getText().equals(valueToSelect)){
option.click();
}
}