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.

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:
- Import the package org.openqa.selenium.support.ui.Select.
- Instantiate the drop-down as a
Selectobject 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.
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
valueattribute matches the parameter. - Note that the displayed text and the
valueattribute are often different, as shown below. - Parameter: the option’s
valueattribute.
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
TRUEif the drop-down allows multiple selections at the same time, otherwiseFALSE. - 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.
The snippet below selects the first two options using selectByVisibleText():
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.

.png)
.png)
.png)
.png)