automationScript

How to work with dropdowns in Selenium using Select

In any web based applications, drop-downs are used to help the end user to select a value from a set of predefined values. e.g. Selecting country from the list of countries.

Selenium has a special class dedicated to operations related to dropdowns. i.e. Select class.

We can use Select class in selenium to select dropdown in selenium i.e. select a value from dropdown, get values from the dropdown etc.

In order to work with this class, we need to import it in our code like below.

org.openqa.selenium.support.ui.Select

How to Get all items from a Weblist/Dropdown using Selenium

Other methods of Select Class

Method Description Type
deselectAll()
Clear all selected values from dropdown
void
deselectByIndex​(int index)
Deselect the option at the given index.
void
deselectByValue​(java.lang.String value)
Deselect all options that have a value matching the argument.
void
deselectByVisibleText​(java.lang.String text)
Deselect all options that display text matching the argument.
void
getAllSelectedOptions()
java.util.List
List
getFirstSelectedOption()
gets first selected option
WebElement

Checkout the code repo

Subscribe for updates