
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.
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.
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:
Selenium’s Select class is designed specifically to interact with such elements, providing methods to select or deselect options easily.
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());
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:
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);
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.
Dynamic dropdowns are populated based on user interaction or API responses (e.g., selecting a country populates a “state” dropdown).
Example Workflow
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.
Here are some of the best practices for Dropdown Handling in Selenium:

Dropdown interactions can fail due to multiple reasons. Below are some common issues and solutions:
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:
By integrating Selenium with BrowserStack Automate, you can ensure dropdown interactions perform consistently across environments, providing real-world confidence in your application’s reliability.
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
Get visual proof, steps to reproduce and technical logs with one click
Continue reading
Try Bird on your next bug - you’ll love it
“Game changer”
Julie, Head of QA
Try Bird later, from your desktop