How to select DropDown & Multiple Select Operations


How to select DropDown & Multiple Select Operations

In this article, we will be learning how to handle the simple drop-down or list box using Selenium WebDriver.

What is DropDown or List Box?

It is a component on GUI, from where users can select one or more values from the set of values presented in a list that is always visible. 

How to handle Listbox using Selenium WebDriver?


In SELENIUM, we handle Listbox using Select class and findElements() method. 

● Select class is present in org.openqa.SELENIUM.support.ui package. 

● Select class has a parameterized constructor which accepts an argument of WebElement object (List box element).

● Following are the available methods of Select class 
selectByIndex() 
selectByValue() 
selectByVisibleText() 
deSelectByIndex() 
deSelectByValue() 
deSelectByVisibleText() 
isMultiple() 
getOptions() 
getAllSelectedOptions() 
getFirstSelectedOption() 
deSelectAll() 

The list box can be handled in 2 ways. 


1. By using findElements() method. 

2. By using SELECT class. We can use select class only and only if the list box is develop using select tagname<>. 

The SELECT class has multiple non-static methods, in order to call them; we need to create an object of the select class. When we create an object of Select class, we pass the reference of the list box on which we want to perform actions as an argument to the select class constructor. Using this reference variable, we call methods like, 

getOptions() - this method returns the address of all the options present in the list box in the form of a list of webelement. 

getAllSelectedOptions() - this method returns the address of all the selected options from the list box in the form of a list of webelement. 

If none of the elements are selected in the list box, it returns an empty list object. 

getFirstSelectedOption() - this method returns the address of the first selected option in the list box. 

If none of the elements are selected in the list box, it throws NoSuchElementException, because this method internally calls findElement method. 

In order to select an option in the list box, we have 3 methods like, 

selectByIndex() - this method is used to select an option in the list box by using the index. The index of an element in the list box starts from 0 (zero). 

If the specified index doesn't match with any element in the list box, it throws NoSuchElementException

selectByValue() -- this method is used to select an option in the list box by using the value attribute. 



If the specified value doesn't match with any element in the list box, it throws NoSuchElementException

selectByVisibleText() -- this method is used to select an option in the list box by using the text of the element. 
If the specified text doesn't match with any element text in the list box, it throws NoSuchElementException

Below is the code for handling this list box

How to handle Multi-Select List box?


If the list box is of type multi-select, using which we can select multiple options simultaneously.

we can also deselect any option in the list box which is already selected by using few methods like,

deselectByIndex(),
deselectByValue(),
deselectByVisibleText() and
deselectAll(). 

If the list box is not of type multi-select, and if we try to call any of the deselect methods, we get an exception called UNSUPPORTEDOPERATIONEXCEPTION



To check whether the list box is a single select or multi-select, we can use isMultiple() method. This method returns true if the list box is a multi-select list box and if it is a single select list box, it returns false.

Below is the HTML snapshot of the Multi-list box


Below is the code for handling this Multi-list box





SHARE THIS

Author:

My Name is Ankur Jain and I am currently working as Automation Test Architect.I am ISTQB Certified Test Manager,Certified UI Path RPA Developer as well as Certified Scrum Master with total 12 years of working experience with lot of big banking clients around the globe.I love to Design Automation Testing Frameworks with Selenium,Appium,Protractor,Cucumber,Rest-Assured, Katalon Studio and currently exploring lot in Dev-OPS as well. I am currently staying in Mumbai, Maharashtra. Please Connect with me through Contact Us page of this website.

Previous Post
Next Post