Ancora Imparo - I'm still learning (Michelangelo at 87)

How to Select/Get the Drop-down Option in Selenium 2

Text fields, buttons, checkboxes, dropdowns, and other web elements can be found on a web page. With the widespread use of forms on websites these days, we occasionally run into dropdowns. There are several sorts of dropdowns, the most common of which are single-select (which allows picking only one value) and multi-select (which allows selecting multiple values. Selenium WebDriver has a class called “Select” that has numerous methods for dealing with dropdowns. 

In Selenium webdriver, we can choose from a dropdown menu, the Selenium Select class can be used to manipulate the dropdowns. In HTML, the select tag represents a dropdown while the option tag represents the entries in the dropdown.

Selenium WebDriver’s “org.openqa.selenium.support.ui” package provides the “Select” class. You can build a Select object by bypassing the object of the “WebElement” class, which displays the object returned by the WebElement’s matching locator. As a result, you may build a Select class object with the following syntax:

Select select = new Select(WebElement webelement);

The WebElement object returned by the locators of the chosen element is the only parameter accepted by the Select class constructor.

The “Select” class has a number of methods for dealing with dropdown menus. This include:

  • selectByIndex
  • selectByValue
  • selectByVisibleText

selectByIndex

This approach uses the dropdown option’s index number to choose it. As an argument, we offer an integer value for the index number. The index starts counting at zero. It has the following syntax:

selectByIndex(int arg0) : void

selectByValue

This approach uses the value of the dropdown option to choose it. As an argument, we offer a string value as the value. It has the following syntax:

selectByValue(String arg0) : void

selectByVisibleText

This approach allows you to choose one choice from a dropdown or a multi-select dropdown depending on the text in the dropdown. The String value of the <select> element must be given as an input. It has the following syntax:

selectByVisibleText(String arg0): void

How to select multiple values from a dropdown in Selenium?

If the <select> tag contains multiple attributes, it means that the dropdown allows selecting multiple values. We can use any of the methods we used to select one value from the dropdown to select multiple values by invoking the methods multiple times for different values. The Select class provides the “isMultiple()” method, which determines whether the select web element allows for multiple selections. It returns a boolean value, i.e., True/False, without taking any argument.

isMultiple(): Boolean

You can use the Select class’s various select methods on the many values you plan to select once you’ve determined whether the web element is multi-select or not.

How to get Options from a Dropdown in Selenium?

The Select class provides the following methods to get the options of a dropdown:

  • getOptions()
  • getFirstSelectedOption()
  • getSelectedOptions()

getOptions()

Sometimes, you may need all of the options in a dropdown or multi-select box. This is where the Select class’s getOptions() method comes in handy. It has the following syntax:

getOptions(): List<WebElement>

getFirstSelectedOption()

This method returns the dropdown’s first selected choice. This function will return the selected value of the dropdown if it is a single-select dropdown, and the first selected value of the dropdown if it is a multi-select dropdown. It has the following syntax:

getFirstSelectedOption(): WebElement

getAllSelectedOptions()

This function retrieves all of the dropdown’s selected options. If the dropdown is a single-select dropdown, this method will return the dropdown’s only selected value; if the dropdown is a multi-select dropdown, this method will return all of the dropdown’s selected values. It has the following syntax:

getAllSelectedOptions():List<WebElement>

How to deselect a value from a dropdown in Selenium?


We can deselect values in a DropDown & Multi-Choose just like we may select them in a DropDown & Multi-Select. However, the deselect approach is only applicable to Multi-Select. The various deselect methods outlined here can be used to deselect pre-selected items from a Multi-select element:

  • deselectAll()
  • deselectByIndex()
  • deselectByValue()
  • deselectByVisibleText()

deselectAll

This approach will remove all of the dropdown’s chosen entries. It has the following syntax:

deselectAll(): void

deselectByIndex()

The Select class provides the deselectByIndex() method, which works similarly to the selectByIndex() method for deselecting an option from a dropdown menu. To deselect an option, use the option’s index number. It has the following syntax:

deselectByIndex(int arg0): void

deselectbyValue()

The Select class provides the deselectByValue() method, which works similarly to the selectByValue() method for selecting an option from a dropdown menu. You can deselect an option by changing its value. It has the following syntax:

deselectByValue(String arg0): void

deselectByVisibleText

The Select class provides the deselectByVisibleText() method, which works similarly to the selectByVisibleText() method for selecting an option from a dropdown menu. To deselect an option, utilize the option’s text. It has the following syntax:

deselectByVisibleText(String arg0): void

About the Autor

Jamiu Idowu

Jamiu Idowu

Entrepreneur seeking to shape the world through IT and emerging technologies. Enjoys research and technical writing, and can serve as a bridge between technology and its users.

Liked this article?

We have a lot more where that came from!
Join the subscribers who stay ahead of the pack.

By entering your email, you agree to our Terms of Service and Privacy Policy.