How to Select Value from Dropdown in Selenium

โšก Smart Summary

Select Value from Dropdown in Selenium uses the dedicated Select class to control HTML SELECT elements. The class exposes selectByVisibleText, selectByValue, and selectByIndex methods that pick options precisely and support multi-select drop-downs.

  • ๐Ÿ“ฆ Import the package: Bring in org.openqa.selenium.support.ui.Select and wrap the WebElement before calling any selection method.
  • ๐ŸŽฏ Pick the right method: Choose selectByVisibleText, selectByValue, or selectByIndex based on whether display text, value attribute, or position is most stable.
  • ๐Ÿ” Handle multi-select drop-downs: Use isMultiple to detect them, and deselectAll or deselectByVisibleText to clear previous choices.
  • โœ… Verify the selection: Call getFirstSelectedOption to confirm the test actually changed the drop-down state.
  • ๐Ÿค– Use AI locators: Self-healing AI locators repair broken select-element XPaths after UI changes, keeping drop-down tests stable across releases.

Select Value from Dropdown in Selenium

What is the Select Class in Selenium?

The Select class in Selenium is a helper class from the org.openqa.selenium.support.ui package that lets WebDriver interact with HTML <select> elements. It exposes methods to select and deselect options by visible text, value, or index, and to detect whether a drop-down accepts multiple selections.

Because Select is a regular Java class, you create an instance with the new keyword and pass the WebElement that points to the <select> tag on the page.

How to Select Dropdown in Selenium

Before handling a drop-down in Selenium, two steps are required:

  1. Import the package org.openqa.selenium.support.ui.Select.
  2. Instantiate the drop-down as a Select object in Selenium WebDriver.

As an example, open the Mercury Tours’ Registration page (https://demo.guru99.com/test/newtours/register.php) and locate the “Country” drop-down.

Select Dropdown in Selenium

Step 1) Import the Select package.

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

Step 2) Declare the drop-down element as an instance of the Select class. In the example below, the instance is named drpCountry.

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

Step 3) Start controlling the drop-down. Use any Select method to pick an option. The sample below selects “ANTARCTICA”:

drpCountry.selectByVisibleText("ANTARCTICA");

Select Methods in Selenium

The Select class provides several methods to interact with drop-down options. The five most common are described below.

1) selectByVisibleText() and deselectByVisibleText()

  • Selects or deselects the option whose displayed text matches the parameter.
  • Parameter: the exact text displayed for the option.

Example:

drpCountry.selectByVisibleText("ANTARCTICA");

2) selectByValue() and deselectByValue()

  • Selects or deselects the option whose value attribute matches the parameter.
  • Note that the displayed text and the value attribute are often different, as shown below.
  • Parameter: the option’s value attribute.

selectByValue and deselectByValue

Example:

drpCountry.selectByValue("234");

3) selectByIndex() and deselectByIndex()

  • Selects or deselects the option at the given index. Indexes are zero-based.
  • Parameter: the integer index of the option.

Example:

drpCountry.selectByIndex(0);

4) isMultiple()

  • Returns TRUE if the drop-down allows multiple selections at the same time, otherwise FALSE.
  • Parameter: none.

Example:

if (drpCountry.isMultiple()) {
    // do something here
}

5) deselectAll()

  • Clears every selected option. Only valid on multi-select drop-downs.
  • Parameter: none.

Example:

drpCountry.deselectAll();

Complete Code of Select Methods in Selenium

The following Java program demonstrates the Select class on both a single-select and a multi-select drop-down.

package newpackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.By;

public class accessDropDown {
    public static void main(String[] args) {
        System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
        String baseURL = "https://demo.guru99.com/test/newtours/register.php";
        WebDriver driver = new FirefoxDriver();
        driver.get(baseURL);

        // Single-select drop-down
        Select drpCountry = new Select(driver.findElement(By.name("country")));
        drpCountry.selectByVisibleText("ANTARCTICA");

        // Multi-select drop-down
        driver.get("http://jsbin.com/osebed/2");
        Select fruits = new Select(driver.findElement(By.id("fruits")));
        fruits.selectByVisibleText("Banana");
        fruits.selectByIndex(1);
    }
}

Selecting Items in a Multiple SELECT element

The selectByVisibleText() method also works on multi-select drop-downs. As an example, use https://jsbin.com/osebed/2 as the base URL. It exposes a drop-down that accepts multiple selections at once.

Selecting Items in a Multiple Select Element

The snippet below selects the first two options using selectByVisibleText():

Selecting Items in a Multiple Select Element code

Method Reference Table

This table summarises every Select-class method covered above for quick lookup.

Command Description
selectByVisibleText() / deselectByVisibleText() Selects or deselects an option by its displayed text.
selectByValue() / deselectByValue() Selects or deselects an option by its value attribute.
selectByIndex() / deselectByIndex() Selects or deselects an option by its zero-based index.
isMultiple() Returns TRUE if the drop-down allows multiple selections, otherwise FALSE.
deselectAll() Deselects all previously selected options on a multi-select drop-down.

To control drop-down boxes, import the org.openqa.selenium.support.ui.Select package first, then create a Select instance and call any of the methods above.

FAQs

The Select class is a Selenium helper for HTML SELECT elements. It wraps a WebElement and exposes selectByVisibleText, selectByValue, selectByIndex, isMultiple, and deselectAll for clean drop-down automation.

Prefer selectByValue when the value attribute is stable, because labels often change for localization. Use selectByVisibleText when the text is authoritative, and avoid selectByIndex when option order may change.

The Select class only works on real HTML SELECT tags. If the WebElement is a div, ul, or custom widget styled to look like a drop-down, you must click options through WebElement.click() or the Actions API instead.

Call getFirstSelectedOption() on the Select instance and assert its getText() value. For multi-select drop-downs, iterate getAllSelectedOptions() and compare the returned list with the expected selections.

Yes. Use isMultiple() to confirm the drop-down allows multiple values, then call selectByVisibleText multiple times or deselectAll() to reset. Multi-select drop-downs require the HTML SELECT element to declare the multiple attribute.

Use WebDriverWait with ExpectedConditions.numberOfElementsToBeMoreThan on the option locator. This ensures async-populated drop-downs are ready before instantiating the Select class and calling any selection method.

AI-powered self-healing locators detect when a SELECT element’s id, name, or XPath has changed and switch to a working alternative. They reduce flaky drop-down failures in pipelines after UI redesigns.

Yes. AI coding assistants turn a plain-English request such as “select country ANTARCTICA on the register page” into a complete Select instance with the right locator, selectByVisibleText call, and a verification assertion.

Summarize this post with: