Working with Dropdowns in Selenium WebDriver

Working with Dropdowns in Selenium WebDriver

16 July 2024 Stephan Petzl Leave a comment QA

Handling dropdowns, also known as “Select” boxes, is a common requirement in web automation. This guide will walk you through various methods to interact with dropdowns using Selenium WebDriver across different programming languages such as Java, Python, and C#. These techniques will help you select options by their visible text, value, or index and retrieve selected options and lists of options.

Using the Select Utility Class

The Select class in Selenium provides utility methods to perform common tasks on dropdowns. Below is an example of how to use this class with a sample HTML structure:

        
            <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);
            mySelect.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);
            mySelect.SelectByText("Option");
        
    

Select by Option Value

Java:

        
            WebElement mySelectElm = driver.findElement(By.id("mySelectID"));
            Select mySelect = new Select(mySelectElm);
            mySelect.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);
            mySelect.SelectByValue("Value");
        
    

Select by Index

Java:

        
            WebElement mySelectElm = driver.findElement(By.id("mySelectID"));
            Select mySelect = new Select(mySelectElm);
            mySelect.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);
            mySelect.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.Options;
            foreach(var option in options) {
                Console.Write(option.Text); //Prints "Option", followed by "Not Option"
            }
        
    

Conclusion

Using the Select class simplifies interacting with dropdown elements in Selenium WebDriver. Whether you are using Java, Python, or C#, these methods offer a robust way to handle dropdowns efficiently.

If you are looking for a no-code solution to automate your tests, consider using Repeato. Repeato is a fast and efficient no-code test automation tool for iOS and Android applications. Its computer vision and AI-based approach make it simple to set up and use, ensuring high-quality assurance for your apps without the need for extensive coding knowledge.

Like this article? there’s more where that came from!