We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Master single-select dropdowns using selectByVisibleText, selectByValue, and selectByIndex
Single-select dropdowns allow choosing only one option at a time. Selecting a new option automatically deselects the previous one.
Selects an option by its visible label.
Selects an option by its value attribute.
Selects an option by its 0-based index position.
selectByVisibleText(...) when the label text is stableselectByValue(...) when backend relies on specific value attributesselectByIndex(...) unless option order is guaranteed stableImagine a page called "Select List Playground" with a Number Selector:
Currently selected number: One
Options Details:
Try selecting different options to see the selection change!
Your Selenium code will automate this using the Select class with three different selection methods.
123456789101112131415161718WebElement numberSelectElement = driver.findElement(By.name("selectomatic"));
Select numberSelect = new Select(numberSelectElement);
// Method 1: Select by visible text (most readable)
numberSelect.selectByVisibleText("Four");
// Method 2: Select by value attribute (useful for backend validation)
numberSelect.selectByValue("four");
// Method 3: Select by index (use when position is stable)
numberSelect.selectByIndex(2); // Index 2 = "Four"
// Verify the selection
WebElement selectedOption = numberSelect.getFirstSelectedOption();
System.out.println("Selected: " + selectedOption.getText());
// Output: "Selected: Four"
driver.quit();<select> element using its name="selectomatic".<select> in a Select object, enabling helper methods for selecting options."Four". This is the most readable selection method and works well when the label is stable.value="four". Useful when your tests need to mirror backend logic that depends on value attributes.getText() to verify the selection.Prefer selectByVisibleText(...) for readability. Tests that select "United States" are more maintainable than selecting "us" or index 2.
After selecting, use getFirstSelectedOption() to verify the correct option was selected. This catches selection failures immediately.
If you get NoSuchElementException, the visible text or value doesn't match exactly. Check for extra spaces, case sensitivity, or special characters.