Contents

    Guides

    How to Handle Dropdown in Selenium WebDriver

    Published on

    October 29, 2025
    How to Handle Dropdown in Selenium WebDriver

    Dropdowns are common UI elements in web applications used to let users choose from multiple options. In automation testing, handling dropdowns efficiently ensures end-to-end test coverage of form submissions, search filters, and configuration menus. Selenium WebDriver provides multiple approaches for interacting with dropdowns—ranging from standard HTML <select> elements to dynamically generated custom dropdowns.

    This guide provides a detailed breakdown of handling dropdowns in Selenium using Java, including code examples, best practices, and advanced handling techniques for both static and dynamic dropdowns.

    Introduction to Dropdowns in Web Automation

    Dropdown menus (also called select menus or combo boxes) are used to present a list of predefined options to users. In HTML, they are primarily implemented using the <select> tag along with <option> tags for individual choices. However, many modern web applications now use custom dropdowns built with JavaScript, Angular, React, or Vue components—making automation slightly more complex.

    Selenium WebDriver supports both standard and non-standard dropdown handling through dedicated APIs and custom logic. Understanding the underlying structure of each dropdown type is essential for selecting the right approach.

    Understanding HTML Select Micro-Structure

    Before interacting with a dropdown, it’s important to understand its HTML structure. A typical HTML dropdown looks like this:

    <select id="country">

      <option value="IN">India</option>

      <option value="US">United States</option>

      <option value="UK">United Kingdom</option>

    </select>

    Key attributes:

    • <select> tag represents the dropdown container.
    • <option> tags represent individual selectable items.
    • Each <option> can have a value and display text.

    Selenium’s Select class is designed specifically to interact with such elements, providing methods to select or deselect options easily.

    Handling Standard Dropdowns Using the Select Class

    Selenium WebDriver provides the Select class (part of org.openqa.selenium.support.ui) for automating dropdown interactions in standard HTML <select> elements.

    1. Import and Initialize the Select Class

    The first step is to locate the dropdown WebElement and initialize a Select object.

    import org.openqa.selenium.By;

    import org.openqa.selenium.WebDriver;

    import org.openqa.selenium.WebElement;

    import org.openqa.selenium.chrome.ChromeDriver;

    import org.openqa.selenium.support.ui.Select;

    public class DropdownExample {

        public static void main(String[] args) {

            WebDriver driver = new ChromeDriver();

            driver.get("https://example.com/form");

            WebElement dropdown = driver.findElement(By.id("country"));

            Select select = new Select(dropdown);

        }

    }

    2. Selecting Options from the Dropdown

    The Select class provides three main methods to select an option:

    selectByVisibleText() – Selects based on the displayed text.

    select.selectByVisibleText("India");

    selectByValue() – Selects based on the value attribute.

    select.selectByValue("US");

    selectByIndex() – Selects based on the position index (starting from 0).

    select.selectByIndex(2);

    3. Validating Selected Options

    You can verify which option is currently selected using:

    WebElement selectedOption = select.getFirstSelectedOption();

    System.out.println("Selected: " + selectedOption.getText());

    Managing Non-Standard or Custom Dropdowns (without Select)

    Many modern applications use dynamic dropdowns built with custom components like <div> or <li> elements instead of standard <select> tags. These dropdowns cannot be handled by the Select class and require custom logic.

    Example HTML Structure

    <div class="dropdown">

      <button class="dropdown-toggle">Select Country</button>

      <ul class="dropdown-menu">

        <li>India</li>

        <li>United States</li>

        <li>United Kingdom</li>

      </ul>

    </div>

    Handling via Click and FindElement

    WebElement dropdownButton = driver.findElement(By.className("dropdown-toggle"));

    dropdownButton.click();

    List<WebElement> options = driver.findElements(By.xpath("//ul[@class='dropdown-menu']//li"));

    for (WebElement option : options) {

        if (option.getText().equals("United States")) {

            option.click();

            break;

        }

    }

    Explanation:

    • The script clicks the dropdown to reveal its options.
    • Then, it retrieves all options under the dropdown menu using findElements().
    • Finally, it loops through the options and clicks the desired one.

    When to Use JavaScript Executor

    If the dropdown is hidden behind JavaScript events or not visible, you can use JavascriptExecutor to perform the click:

    JavascriptExecutor js = (JavascriptExecutor) driver;

    js.executeScript("arguments[0].click();", dropdownButton);

    Working with Multi-Select Dropdowns and Deselect Operations

    Multi-select dropdowns allow users to select multiple options at once. Selenium supports them through the Select class if the HTML element includes the multiple attribute.

    Example

    <select id="skills" multiple>

      <option value="java">Java</option>

      <option value="python">Python</option>

      <option value="selenium">Selenium</option>

    </select>

    Handling Multi-Select in Selenium

    Select multiSelect = new Select(driver.findElement(By.id("skills")));

    multiSelect.selectByVisibleText("Java");

    multiSelect.selectByVisibleText("Selenium");

    // Deselect one option

    multiSelect.deselectByVisibleText("Java");

    // Retrieve all selected options

    List<WebElement> selectedOptions = multiSelect.getAllSelectedOptions();

    for (WebElement option : selectedOptions) {

        System.out.println(option.getText());

    }

    You can also deselect all selected options at once using deselectAll() when resetting the form.

    Handling Dynamic or Asynchronously-Populated Dropdowns

    Dynamic dropdowns are populated based on user interaction or API responses (e.g., selecting a country populates a “state” dropdown).

    Example Workflow

    1. Select a country from the first dropdown.
    2. Wait for the “state” dropdown to populate.
    3. Select a state dynamically.

    Implementation

    Select country = new Select(driver.findElement(By.id("country")));

    country.selectByVisibleText("India");

    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//select[@id='state']/option[text()='Maharashtra']")));

    Select state = new Select(driver.findElement(By.id("state")));

    state.selectByVisibleText("Maharashtra");

    This approach ensures the test waits for data to load before interacting with it, preventing NoSuchElementException.

    Best Practices for Dropdown Handling in Selenium

    Here are some of the best practices for Dropdown Handling in Selenium:

    • Use Explicit Waits: Always wait for dropdown elements or options to load before interacting.
    • Avoid Hardcoded Indexes: Prefer selectByVisibleText() or selectByValue() for reliability.
    • Handle Stale Element Exceptions: Re-locate dropdown elements when the page reloads.
    • Refrain from Fixed Delays: Use dynamic waits instead of Thread.sleep().
    • Encapsulate Dropdown Actions: Create reusable methods for selecting and validating dropdown values to reduce code duplication.

    Troubleshooting Common Dropdown Issues

    Dropdown interactions can fail due to multiple reasons. Below are some common issues and solutions:

    • Element Not Interactable: The dropdown might not be visible; use WebDriverWait or JavaScript Executor to ensure visibility.
    • Incorrect XPath or Locator: Verify that the locator uniquely identifies the dropdown and its options.
    • Overlapping Elements: In cases where overlays block clicks, scroll the element into view using Actions or JavaScript.
    • Timing Issues: Always use waits to handle dynamically loaded dropdowns.

    Scaling Dropdown Tests with Cloud & Real-Device Platforms

    Testing dropdowns locally ensures functionality but does not guarantee compatibility across all browsers or devices. Dropdowns often behave differently depending on browser rendering or JavaScript execution.

    To ensure accurate testing across environments, use cloud-based platforms like BrowserStack Automate.

    Benefits of Using BrowserStack Automate:

    • Run Selenium dropdown tests on real browsers and devices (not emulators).
    • Validate dropdown interactions in Chrome, Firefox, Safari, and Edge simultaneously.
    • Debug failures using screenshots, console logs, and video recordings.
    • Scale parallel executions for faster test cycles.

    By integrating Selenium with BrowserStack Automate, you can ensure dropdown interactions perform consistently across environments, providing real-world confidence in your application’s reliability.

    Conclusion

    Handling dropdowns effectively in Selenium is essential for achieving comprehensive automation coverage. From standard HTML dropdowns to complex JavaScript-driven ones, Selenium provides multiple methods to interact with them.

    By leveraging the Select class for standard dropdowns and custom locators or JavaScript for dynamic ones, you can handle a wide variety of dropdowns efficiently. Coupled with tools like BrowserStack Automate, these tests can be executed across real devices and browsers, ensuring your web applications deliver a seamless experience for all users.

    Run Selenium Tests on Cloud

    Data-rich bug reports loved by everyone

    Get visual proof, steps to reproduce and technical logs with one click

    Make bug reporting 50% faster and 100% less painful

    Rating LogosStars
    4.6
    |
    Category leader

    Liked the article? Spread the word

    Put your knowledge to practice

    Try Bird on your next bug - you’ll love it

    “Game changer”

    Julie, Head of QA

    star-ratingstar-ratingstar-ratingstar-ratingstar-rating

    Overall rating: 4.7/5

    Try Bird later, from your desktop