#Using the Select Utility Class
The big secret to working with dropdowns is that you don't want to work with them as WebElements, but instead create a Select element for them. The Select class (java and python documentation) includes utility methods that allow you to perform common tasks. We will be working with the following html:
<select id="mySelectID">
<option value="Value">Option</option>
<option value="NotValue">Not Option</option>
</select>
##Select by option name
Java:
WebElement mySelectElm = driver.findElement(By.id("mySelectID"));
Select mySelect= new Select(mySelect);
selMySelect.selectByVisibleText("Option");
Python:
mySelect = Select(driver.find_element_by_id("mySelectID"))
mySelect.select_by_visible_text("Option")
C#:
var mySelectElm = driver.FindElement(By.Id("mySelectID"));
var mySelect = new SelectElement(mySelectElm);
selectElement.SelectByText("Option");
##Select by option value
Java:
WebElement mySelectElm = driver.findElement(By.id("mySelectID"));
Select mySelect= new Select(mySelect);
selMySelect.selectByValue("Value");
Python:
mySelect = Select(driver.find_element_by_id("mySelectID"))
mySelect.select_by_value("Value")
C#:
var mySelectElm = driver.FindElement(By.Id("mySelectID"));
var mySelect = new SelectElement(mySelectElm);
selectElement.SelectByValue("Value");
##Select by index
Java:
WebElement mySelectElm = driver.findElement(By.id("mySelectID"));
Select mySelect= new Select(mySelect);
selMySelect.selectByIndex(0);
Python:
mySelect = Select(driver.find_element_by_id("mySelectID"))
mySelect.select_by_index(0)
C#:
var mySelectElm = driver.FindElement(By.Id("mySelectID"));
var mySelect = new SelectElement(mySelectElm);
selectElement.SelectByIndex(0);
##Get the selected option
Java:
WebElement mySelectElm = driver.findElement(By.id("mySelectID"));
Select mySelect= new Select(mySelect);
WebElement option = mySelect.getFirstSelectedOption();
System.out.println(option.getText()); //prints "Option"
Python:
mySelect = Select(driver.find_element_by_id("mySelectID"))
option = mySelect.first_selected_option
print option.text #prints "Option"
C#:
var mySelectElm = driver.FindElement(By.Id("mySelectID"));
var mySelect = new SelectElement(mySelectElm);
var option = mySelect.SelectedOption;
Console.write(option.Text); //prints "Option"
##Get the list of options
Java:
WebElement mySelectElm = driver.findElement(By.id("mySelectID"));
Select mySelect= new Select(mySelect);
List<WebElement> options = mySelect.getOptions();
for (WebElement option : options) {
System.out.println(option.getText()); //Prints "Option", followed by "Not Option"
}
Python:
mySelect = Select(driver.find_element_by_id("mySelectID"))
print [o.text for o in mySelect.options] #Prints "Option", followed by "Not Option"
C#:
var mySelectElm = driver.FindElement(By.Id("mySelectID"));
var mySelect = new SelectElement(mySelectElm);
var options = mySelect.SelectedOptions;
foreach(var option in options) {
Console.write(option.Text); //Prints "Option", followed by "Not Option"
}