Select Value From Multi Select DropDown In Selenium WebDriver

In my previous article, we have seen how to select a value from a normal dropdown or weblist. In this post, I will show you how to select multiple values from a multi select dropdown in Selenium WebDriver.We will also see how to handle drop down having a checkbox as a selection option. The basic difference between a simple dropdown and a multi-select dropdown is that apart from selecting multiple values we can also deselect values from a multi-select dropdown and Selenium does have dedicated methods to deselect values as well.

So here we go.

Handling Multi Select DropDown in Selenium WebDriver

We will see the following things in this tutorial.

DeSelect values from multi-select dropdown using the following Methods 

  • Select values from multi-select dropdown
  • deselectByIndex 
  • deselectByValue
  • deselectByVisibleText
  • deselectAll

Select value from dropdown having checkboxes as select option.

Multi-Select Dropdown

We will try to understand the examples by using the following HTML Page.

Select a Value from multi-select DropDown In Selenium WebDriver

The HTML source code for the sample page is as follows. Please copy it in a notepad and save the code as an HTML file.

How to Select Values from Multi-Select DropDown Using Selenium Webdriver

There are two ways to select multiple values from a multi-select dropdown

  1. Select value one by one by using either of the methods visible Text, Value, or by Index of the items.
  2. Using the Action class

Example – Selecting multiple values from dropdown One by One

In the following code, I am selecting the first option of the dropdown using its index and after that, I am selecting value ASP and Python by using selectByVisibleText method.Checkout the following script for better understanding.

Example – Selecting multiple values from dropdown using Action Class

The Action Class will behave in the same way as you will do select multiple options. You will press the Ctrl button from Keyboard and then click on the desired options that you want to select. The approach is as follows.

Create the WebElement object for the desired options that you want to select.

Example:

The complete code is as follows

Deselect a Value from a Dropdwon Using deselectByIndex Method

We can use deselectByIndex method to deselect any option using its index beginning from 0.

A complete example of all select methods has been given at the end of this article.

Deselect a Value from a Dropdwon Using deselectByValue Method

We can use deselectByValue method to deselect any option using value attribute of option tag.

A complete example of all deselect methods will be given below after discussing all deselect methods.

Deselect a Value from a Dropdwon Using deselectByVisibleText Method

We can use deselectByVisibleText method to deselect any option using the option visible text value.

A complete example of all deselect methods will be given below after discussing all deselect methods.

Handling Dropdown with CheckBoxes In Selenium WebDriver

On modern web designs, some dropdowns give users to select multiple options using checkbox selection. The following is an example of such dropdowns.

dropdown-with-checkboxes-in-selenium-webdriver

If you come across such a dropdown you can use the following approach. The sample page is available at https://nileshpatel17.github.io/ng-multiselect-dropdown/.  The values of the checkboxes are not easily accessible as they don’t have unique property so I will use XPath to select the required values.

Select dropdown with checkboxes using xpath in selenium webdriver

Approach to select dropdown checkboxes

Select dropdown with checkboxes using xpath approach in selenium webdriver

  • Click on the reset button to clear all pre-selected values
  • Click on the small arrow icon on the dropdown to show dropdown values
  • Select the desired option from the dropdown using XPath

Please refer to the following code carefully to understand the aforementioned approach.

Recommended Posts

Leave a Reply