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.
Understanding the Select class and when to use it for dropdown interactions
In this lesson, you will learn how to work with HTML <select> elements using Selenium's Select support class.
Select lists behave differently from normal elements because they:
value attributeAllows only one option to be selected at a time.
When to use:
Uses the multiple attribute to allow multiple selections.
When to use:
The Select class wraps a <select> WebElement and provides convenient helper methods.
Construction:
Information:
Selection:
✓ Native HTML select element
✗ Custom dropdown (use click() instead)
123456789101112131415161718192021222324// Step 1: Locate the <select> element
WebElement selectElement = driver.findElement(By.id("country-select"));
// Step 2: Wrap it in a Select object
Select countrySelect = new Select(selectElement);
// Step 3: Select an option by visible text
countrySelect.selectByVisibleText("United States");
// Alternative: Select by value attribute
countrySelect.selectByValue("us");
// Alternative: Select by index (0-based)
countrySelect.selectByIndex(0);
// Get all available options
List<WebElement> allOptions = countrySelect.getOptions();
System.out.println("Total options: " + allOptions.size());
// Get currently selected option
WebElement selectedOption = countrySelect.getFirstSelectedOption();
System.out.println("Selected: " + selectedOption.getText());
driver.quit();<select> element using any standard locator.Select object to access helper methods. This is required before you can use any Select-specific methods.List<WebElement> of all <option> elements in the select.Always locate the <select> element first, then create the Select object. Don't try to create Select directly.
Many modern frameworks use custom dropdowns (React Select, Material-UI, etc.) that don't use <select>. For these, use standard click() and sendKeys().
After selecting, use getFirstSelectedOption() to verify the correct option was selected. This catches selection failures early.