How do I work with dropdowns in Selenium Webdriver?

  • This is a canonical question, intended to provide a comprehensive answer to many related questions.

    I understand the basics of working with Selenium Webdriver; I can navigate to pages, click buttons, and type into text boxes. But now I want to do things with dropdown boxes (also known as "Select" boxes).

    How can I perform common tasks like iterating over the options in the dropdown or selecting options from the dropdown?

  • Yamikuronue

    Yamikuronue Correct answer

    6 years ago

    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(mySelectElm);
    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(mySelectElm);
    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(mySelectElm);
    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(mySelectElm);
    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(mySelectElm);
    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"
    }
    

    Note that in C# the `SelectElement` class is part of the `Selenium.Support` project. So you have to make sure you have that instead in addition to `Selenium.WebDriver`.

    Can I do multiple upvotes for this answer? Awesome answer!

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM