Most broken dropdown tests aren’t Selenium’s fault. They’re written for the wrong kind of dropdown.
On screen, every dropdown looks roughly the same, with a box, an arrow and a list. Underneath, it could be a native HTML <select>, a stack of <div>s, an input with a filtered list, a menu that opens on hover, or a component that loads its options from an API after you click. Selenium’s Select class only works with the first of those. Use it on anything else and the test fails before it’s even started.
To handle a dropdown in Selenium WebDriver, first check what it’s built from. If it’s a native <select> element, use the Select class and its selectByVisibleText, selectByValue or selectByIndex methods. If it’s a custom component, open it with a click, wait for the options to appear, then click the option you want, or type and use the keyboard for searchable lists.
This article starts with a quick way to tell which kind you’re facing, then gives working code for each one, in Java and Python, followed by the errors you’ll meet and how to fix them.
First, Identify the Dropdown
Open DevTools, right-click the dropdown and choose Inspect. Thirty seconds spent here saves hours later. This table maps what you’ll see to what you should use.
What DevTools Shows
What It Is
What to Use
A <select> with <option> children, visible on the page
A native dropdown
The Select class
A <select multiple>
A native multi-select
The Select class, with the multi-select methods
A <button> or <div> that opens a <ul> or a list of <div>s
A custom dropdown
Click, wait, click
An <input> with role="combobox", and a list that filters as you type
A searchable or autocomplete dropdown
Type, wait, then click or use the keyboard
A hidden <select> next to a styled custom widget
A custom skin over a native select (common with Select2 or Bootstrap Select)
Interact with the visible widget, not the hidden select
Options appear at the bottom of <body>, far from the trigger
A dropdown rendered in a portal
Click, wait, then search for options from the whole page
A menu that opens when you hover over it
A hover menu
The Actions class
A #shadow-root above the options
A web component, such as a Salesforce Lightning combobox
A shadow-aware approach
That hidden-select row catches a lot of people. Libraries like Select2 keep the original <select> in the page but hide it, and show their own widget on top. The Select class will find the hidden element, then fail with an ElementNotInteractableException because nobody can click a hidden option. Test the widget the user actually sees.
Native Dropdowns With the Select Class
When the dropdown is a real, visible <select>, the Select class is the cleanest option Selenium offers.
Create the Select Object
Java
importorg.openqa.selenium.By;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.support.ui.Select;
WebElement countryField = driver.findElement(By.id("country"));
Select country = new Select(countryField);
importorg.openqa.selenium.By;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.support.ui.Select;
WebElement countryField = driver.findElement(By.id("country"));
Select country = new Select(countryField);
importorg.openqa.selenium.By;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.support.ui.Select;
WebElement countryField = driver.findElement(By.id("country"));
Select country = new Select(countryField);
importorg.openqa.selenium.By;importorg.openqa.selenium.WebElement;importorg.openqa.selenium.support.ui.Select;
WebElement countryField = driver.findElement(By.id("country"));
Select country = new Select(countryField);
country.selectByVisibleText("United Kingdom");// what the user seescountry.selectByValue("GB");// the option's value attributecountry.selectByIndex(3);// position, counting from 0
country.selectByVisibleText("United Kingdom");// what the user seescountry.selectByValue("GB");// the option's value attributecountry.selectByIndex(3);// position, counting from 0
country.selectByVisibleText("United Kingdom");// what the user seescountry.selectByValue("GB");// the option's value attributecountry.selectByIndex(3);// position, counting from 0
country.selectByVisibleText("United Kingdom");// what the user seescountry.selectByValue("GB");// the option's value attributecountry.selectByIndex(3);// position, counting from 0
Which one to use depends on what’s most stable in your application.
By visible text reads most like the test’s intent, and is the best default, unless the labels change between languages or environments.
By value survives label changes when developers keep stable codes behind the text, like "GB" for the United Kingdom.
By index is quickest to write and the first to break, because the order of options changes more often than anyone expects.
One detail that changed in Selenium 4. Recent releases refuse to select a disabled option, and throw an exception instead of quietly clicking it. That’s helpful, because a test that "selected" a disabled option was never testing anything real.
Read What’s Selected, and What’s Available
Java
String selected = country.getFirstSelectedOption().getText();
List<WebElement> all = country.getOptions();assertEquals("United Kingdom",selected);assertEquals(195,all.size());// the full list loaded
String selected = country.getFirstSelectedOption().getText();
List<WebElement> all = country.getOptions();assertEquals("United Kingdom",selected);assertEquals(195,all.size());// the full list loaded
String selected = country.getFirstSelectedOption().getText();
List<WebElement> all = country.getOptions();assertEquals("United Kingdom",selected);assertEquals(195,all.size());// the full list loaded
String selected = country.getFirstSelectedOption().getText();
List<WebElement> all = country.getOptions();assertEquals("United Kingdom",selected);assertEquals(195,all.size());// the full list loaded
Checking the whole list is underrated. A dropdown that’s missing an option, or has a duplicate, is a real defect that a test which only selects one value will never notice.
Multi-Select Dropdowns
Java
Select skills = new Select(driver.findElement(By.id("skills")));if(skills.isMultiple()){skills.selectByVisibleText("Java");skills.selectByVisibleText("Python");assertEquals(2,skills.getAllSelectedOptions().size());skills.deselectByVisibleText("Java");skills.deselectAll();}
Select skills = new Select(driver.findElement(By.id("skills")));if(skills.isMultiple()){skills.selectByVisibleText("Java");skills.selectByVisibleText("Python");assertEquals(2,skills.getAllSelectedOptions().size());skills.deselectByVisibleText("Java");skills.deselectAll();}
Select skills = new Select(driver.findElement(By.id("skills")));if(skills.isMultiple()){skills.selectByVisibleText("Java");skills.selectByVisibleText("Python");assertEquals(2,skills.getAllSelectedOptions().size());skills.deselectByVisibleText("Java");skills.deselectAll();}
Select skills = new Select(driver.findElement(By.id("skills")));if(skills.isMultiple()){skills.selectByVisibleText("Java");skills.selectByVisibleText("Python");assertEquals(2,skills.getAllSelectedOptions().size());skills.deselectByVisibleText("Java");skills.deselectAll();}
Always check isMultiple() first. Calling deselectAll() on a single-select dropdown throws an UnsupportedOperationException in Java, and a NotImplementedError in Python.
Custom Dropdowns Without the Select Class
Most modern applications build their own dropdowns. A typical one looks something like this.
HTML
<divclass="dropdown"><buttonclass="dropdown-toggle"aria-expanded="false">Choose a country</button><ulclass="dropdown-menu"><lidata-value="GB">United Kingdom</li><lidata-value="IE">Ireland</li><lidata-value="FR">France</li></ul></div>
<divclass="dropdown"><buttonclass="dropdown-toggle"aria-expanded="false">Choose a country</button><ulclass="dropdown-menu"><lidata-value="GB">United Kingdom</li><lidata-value="IE">Ireland</li><lidata-value="FR">France</li></ul></div>
<divclass="dropdown"><buttonclass="dropdown-toggle"aria-expanded="false">Choose a country</button><ulclass="dropdown-menu"><lidata-value="GB">United Kingdom</li><lidata-value="IE">Ireland</li><lidata-value="FR">France</li></ul></div>
<divclass="dropdown"><buttonclass="dropdown-toggle"aria-expanded="false">Choose a country</button><ulclass="dropdown-menu"><lidata-value="GB">United Kingdom</li><lidata-value="IE">Ireland</li><lidata-value="FR">France</li></ul></div>
The pattern is always the same. Click to open, wait for the list, click the option, then check it worked.
Java
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(10));driver.findElement(By.cssSelector(".dropdown-toggle")).click();wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".dropdown-menu")));wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".dropdown-menu li[data-value='GB']"))).click();assertEquals("United Kingdom",driver.findElement(By.cssSelector(".dropdown-toggle")).getText());
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(10));driver.findElement(By.cssSelector(".dropdown-toggle")).click();wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".dropdown-menu")));wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".dropdown-menu li[data-value='GB']"))).click();assertEquals("United Kingdom",driver.findElement(By.cssSelector(".dropdown-toggle")).getText());
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(10));driver.findElement(By.cssSelector(".dropdown-toggle")).click();wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".dropdown-menu")));wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".dropdown-menu li[data-value='GB']"))).click();assertEquals("United Kingdom",driver.findElement(By.cssSelector(".dropdown-toggle")).getText());
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(10));driver.findElement(By.cssSelector(".dropdown-toggle")).click();wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".dropdown-menu")));wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".dropdown-menu li[data-value='GB']"))).click();assertEquals("United Kingdom",driver.findElement(By.cssSelector(".dropdown-toggle")).getText());
Two habits prevent most failures. Wait for the list after opening it, rather than reaching for an option that may not be rendered yet. And locate options by stable text or data attributes, never by position, because component libraries often reorder or virtualise long lists.
Building locators that survive markup changes is a skill of its own, and dynamic XPath in Selenium covers the patterns that hold up when IDs and classes keep shifting.
Options Rendered in a Portal
Many component libraries, including Material UI and React-based libraries, render the open list at the end of <body>, not inside the dropdown. A locator scoped to the dropdown’s container will never find the options. Search the whole page instead.
Java
driver.findElement(By.id("country-trigger")).click();// search from the driver, not from the dropdown elementwait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@role='option' and normalize-space()='United Kingdom']"))).click();
driver.findElement(By.id("country-trigger")).click();// search from the driver, not from the dropdown elementwait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@role='option' and normalize-space()='United Kingdom']"))).click();
driver.findElement(By.id("country-trigger")).click();// search from the driver, not from the dropdown elementwait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@role='option' and normalize-space()='United Kingdom']"))).click();
driver.findElement(By.id("country-trigger")).click();// search from the driver, not from the dropdown elementwait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@role='option' and normalize-space()='United Kingdom']"))).click();
If a locator works in DevTools but fails in your test, check where the options actually are once the list is open. This is usually the reason.
Searchable and Autocomplete Dropdowns
These combine a text input with a list that filters as you type. Type, wait for the filtered option, then choose it.
Java
WebElement search = driver.findElement(By.cssSelector("input[role='combobox']"));search.click();search.sendKeys("United K");wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@role='option' and normalize-space()='United Kingdom']"))).click();
WebElement search = driver.findElement(By.cssSelector("input[role='combobox']"));search.click();search.sendKeys("United K");wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@role='option' and normalize-space()='United Kingdom']"))).click();
WebElement search = driver.findElement(By.cssSelector("input[role='combobox']"));search.click();search.sendKeys("United K");wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@role='option' and normalize-space()='United Kingdom']"))).click();
WebElement search = driver.findElement(By.cssSelector("input[role='combobox']"));search.click();search.sendKeys("United K");wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@role='option' and normalize-space()='United Kingdom']"))).click();
Many searchable dropdowns, including React Select, also accept the keyboard. That’s often more reliable than clicking, and it tests something real users depend on.
Before writing either version, try the dropdown by hand. Some clear the text when you choose, some keep it, and some need Enter or Tab to confirm. Automate what the component actually does.
Hover Menus
Navigation menus that open on hover need Selenium’s Actions class to move the mouse.
Java
Actions actions = new Actions(driver);
WebElement products = driver.findElement(By.linkText("Products"));actions.moveToElement(products).perform();wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Insurance"))).click();
Actions actions = new Actions(driver);
WebElement products = driver.findElement(By.linkText("Products"));actions.moveToElement(products).perform();wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Insurance"))).click();
Actions actions = new Actions(driver);
WebElement products = driver.findElement(By.linkText("Products"));actions.moveToElement(products).perform();wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Insurance"))).click();
Actions actions = new Actions(driver);
WebElement products = driver.findElement(By.linkText("Products"));actions.moveToElement(products).perform();wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Insurance"))).click();
Hover menus are the most fragile dropdowns to automate, because animations and small cursor movements can close them before the click lands. They’re also hard for keyboard users.
If you can, ask the team to support opening on click or focus as well. It makes the menu more accessible and the test more stable.
Dropdowns That Load Their Options Later
Some dropdowns fetch their options from an API only after you open them. For a moment, the list is empty. Wait for the option you need, not for a fixed time.
Java
driver.findElement(By.id("city-trigger")).click();
WebElement manchester = new WebDriverWait(driver,Duration.ofSeconds(15))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@role='option' and normalize-space()='Manchester']")));manchester.click();
driver.findElement(By.id("city-trigger")).click();
WebElement manchester = new WebDriverWait(driver,Duration.ofSeconds(15))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@role='option' and normalize-space()='Manchester']")));manchester.click();
driver.findElement(By.id("city-trigger")).click();
WebElement manchester = new WebDriverWait(driver,Duration.ofSeconds(15))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@role='option' and normalize-space()='Manchester']")));manchester.click();
driver.findElement(By.id("city-trigger")).click();
WebElement manchester = new WebDriverWait(driver,Duration.ofSeconds(15))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@role='option' and normalize-space()='Manchester']")));manchester.click();
Avoid Thread.sleep here. A fixed pause is either too long, slowing every run, or too short on a slow day, producing failures nobody can reproduce.
Choosing between implicit, explicit and fluent waits makes a bigger difference to dropdown stability than most locator tweaks, as Selenium timeouts explains with examples.
Dropdowns Inside Shadow DOM or iFrames
Salesforce Lightning comboboxes and other web components put their options inside a shadow root, where ordinary locators can’t reach. Embedded widgets sometimes sit inside an iFrame. In both cases, the dropdown code above still works, once you’ve got through the boundary first.
Getting through a shadow root, including the traps with XPath and slotted content, is walked through step by step in shadow DOM testing, and switching into a frame is covered by handling frames in Selenium.
A Reusable Dropdown Helper
Rather than repeating open, wait and click in every test, put it in one place. When a dropdown changes, you fix it once.
Java
publicvoidchoose(By trigger, String optionText){
WebElement field = driver.findElement(trigger);if(field.getTagName().equalsIgnoreCase("select")){new Select(field).selectByVisibleText(optionText);return;}field.click();
By option = By.xpath("//*[(@role='option' or self::li) and normalize-space()='" + optionText + "']");wait.until(ExpectedConditions.elementToBeClickable(option)).click();}
publicvoidchoose(By trigger, String optionText){
WebElement field = driver.findElement(trigger);if(field.getTagName().equalsIgnoreCase("select")){new Select(field).selectByVisibleText(optionText);return;}field.click();
By option = By.xpath("//*[(@role='option' or self::li) and normalize-space()='" + optionText + "']");wait.until(ExpectedConditions.elementToBeClickable(option)).click();}
publicvoidchoose(By trigger, String optionText){
WebElement field = driver.findElement(trigger);if(field.getTagName().equalsIgnoreCase("select")){new Select(field).selectByVisibleText(optionText);return;}field.click();
By option = By.xpath("//*[(@role='option' or self::li) and normalize-space()='" + optionText + "']");wait.until(ExpectedConditions.elementToBeClickable(option)).click();}
publicvoidchoose(By trigger, String optionText){
WebElement field = driver.findElement(trigger);if(field.getTagName().equalsIgnoreCase("select")){new Select(field).selectByVisibleText(optionText);return;}field.click();
By option = By.xpath("//*[(@role='option' or self::li) and normalize-space()='" + optionText + "']");wait.until(ExpectedConditions.elementToBeClickable(option)).click();}
A helper like this belongs in your page objects, so tests read as "choose United Kingdom as the country", not as a list of clicks.
Keeping screen details like these out of the tests themselves is the whole point of the page object model.
Common Dropdown Errors, and How to Fix Them
Error
What It Usually Means
The Fix
UnexpectedTagNameException
The Select class was used on something that isn’t a <select>
Check the element in DevTools and switch to click, wait and click
ElementNotInteractableException
The dropdown or option is hidden, covered or still animating, or you’ve targeted a hidden native select behind a custom widget
Wait until it’s clickable, and target the visible widget
StaleElementReferenceException
The list re-rendered after you found an option, which is common in React and Angular
Find the option again right before clicking it
NoSuchElementException
The options haven’t loaded yet, or live in a portal outside your search area
Wait for the option, and search the whole page
Click succeeds, but nothing changes
A custom widget ignored the click, or a JavaScript shortcut skipped its events
Read the selected value back and assert on it
Multi-select loses choices when reopened
The custom widget doesn’t keep its state between openings
Check the widget’s real behaviour before assuming persistence
A word on JavaScript shortcuts. Setting a select’s value directly with JavascriptExecutor can make a stubborn test pass, but it skips the events a real user triggers, so dependent fields may never update. Treat it as a last resort, and assert on the result if you use it.
Inspect before you code: Knowing the dropdown type decides everything else.
Choose by text or value, not position: Index-based tests break silently when options are reordered.
Wait for conditions, not time: Wait for the option you need to be clickable.
Assert after every choice: Read the selected value back. A click without an exception isn’t proof.
Test the list, not just one option: Missing, duplicate or misordered options are defects too.
Keep dropdown logic in one helper: A change to a component library then means one fix, not fifty.
Testing Dropdowns by Intent, Not Mechanism
Every example above describes how to operate a dropdown. That’s why they break. When a team swaps one component library for another, the mechanism changes completely, even though the user still just picks a country.
Virtuoso QA lets you write the step the way a user would describe it.
A step such as Pick "United Kingdom" from "Country" (illustrative) names the field by its visible label, so the same step works whether the field is a native select or a custom component. Behind that label, Smart Element Identification keeps several other ways of finding the element, and falls back through them if the first doesn’t match. When a component library upgrade moves things around, Virtuoso QA works out where the element went and either heals the step, visibly and reversibly, or proposes a fix for someone to accept. If it can’t heal with confidence, the test fails clearly rather than picking the wrong option.
For teams moving off Selenium, GENerator converts existing Selenium suites into plain English journeys, so dropdown coverage you’ve already built isn’t thrown away. Failures come with root cause analysis, including the failing step, logs and network calls, and journeys run across 2,000+ browser and OS combinations, which matters for dropdowns because they’re among the components that differ most between browsers.
Virtuoso QA’s AI test automation lets you pick options by their visible labels in plain English, across every kind of dropdown, and heals steps visibly when the markup moves.
Adwitiya works directly with QA teams adopting the platform and turns what he sees into practical guides on test design, regression strategy and keeping coverage honest.
See what your next release looks like with Virtuoso
Book a walkthrough on your applications and your workflows. Bring a requirement, a user journey, or a brittle Selenium script, and watch the loop run on something you recognise.
See what your next release looks like with Virtuoso
Book a walkthrough on your applications and your workflows. Bring a requirement, a user journey, or a brittle Selenium script, and watch the loop run on something you recognise.
See what your next release looks like with Virtuoso
Book a walkthrough on your applications and your workflows. Bring a requirement, a user journey, or a brittle Selenium script, and watch the loop run on something you recognise.
Virtuoso QA is establishing the standard of proof for software releases. Its governed QA loop turns business requirements into tests for any browser-based application: AI proposes, a deterministic engine executes, a person approves what matters, and every decision leaves evidence.
Virtuoso QA is establishing the standard of proof for software releases. Its governed QA loop turns business requirements into tests for any browser-based application: AI proposes, a deterministic engine executes, a person approves what matters, and every decision leaves evidence.
Virtuoso QA is establishing the standard of proof for software releases. Its governed QA loop turns business requirements into tests for any browser-based application: AI proposes, a deterministic engine executes, a person approves what matters, and every decision leaves evidence.