DropDown Handle using selenium:

 

  • In Web page Dropdown / ListBox consider as a “Select” object in WebDriver.
  • We declare the Dropdown WebElement as an instance / object of the “Select” class to select any option from DropDown.
  • Using “select” method we select and deselect options from DropDown / ListBox.
  • “Select” is a class which is provided by Selenium WebDriver to perform multiple operations on DropDown WebElement object and Multiple Select object in Web Page.
  • In Selenium select / deselect methods will throw NoSuchElementException if no matching option elements are found in Web Page.

 

Import below Package to Handle DropDown:

 

Dropdown Html Code:

 

Code to Select Dropdown: 

1)   Identify dropdown Without WebElement:

OR

2)   Identify dropdown Using WebElement: 

 

Different Dropdown option Select Methods:

  1. selectByVisibleText Method.
  2. selectByIndex Method.
  3. selectByValue Method.

 

1. selectByVisibleText Method

  • It takes a parameter of the string which is a value of the select element and perform select operation.

 

2. selectByIndex Method

  • It fetch a parameter of int which is the index value of Select element and perform select operation.

 

3. selectByValue Method

  • It takes a element with string using value and perform select operation.

 

Different Dropdown option De-Select Methods:

  1. deselectByVisibleText Method.
  2. deselectByValue Method.
  3. deselectByIndex Method.
  4. deselectAll Method.
  5. isMultiple Method.

 

1. Deselect By Visible Text:

When you wants to remove selection from list box for particular option then you can use webdriver’s deselectByVisibleText() method.

 

2. Deselect By Value:

You can also deselect option from list box by using value of List Box.

 

3. Deselect By Index:

Given bellow syntax will remove selection by index = 1 index available in Integer format.

 

4. DeselectAll:

Using this method able to Deselect all Selected options from Multi Select Dropdown all options will be UnSelected.

 

5. isMultiple:

Methods check element support multiple selecting options at the same time or not verify Functionality can we check / select multiple option’s.

 

Example of Dropdown with Select / De-Select / isMultiple Method:

 

Example of Select Multiple options from Multi Select Dropdown handle using Selenium: 

 

Example of Get All Months from dropdown & print: