The select class is present in org.openqa.selenium.support.ui package. It extends object class and implements the ISelect interface. An HTML <select> tag provides methods and properties for selecting and deselecting options in a dropdown.
Here, we have shown the country dropdown list for demo purposes in the below screenshot. This kind of country list is generally seen in the registration form of a web application.
We can also see the basic HTML coding of the dropdown list on the backend side.
In the above code, <select> tag is used to create a drop-down list. The <option> tags inside the <select> tag define the available options in the dropdown list that can be selected. The first item in the drop-down list is by default selected.
How to create an Object of Select Class?
Since the select is an ordinary class, its object is created by using the new keyword. After creating object, we will have to pass the dropdown WebElement as parameter to its constructor.
The syntax to create an object of select class is as follows:
Select select = new Select( WebElement element);
When the element is not a SELECT, UnexpectedTagNameException will throw.
Once you are able to create an object of the select class, you can access all the methods provided by the SELECT class by just typing select + dot.
The select + dot (select.) will provide the list of all methods under the Select class. You can choose any method according to the requirement of your test case.
Let’s see a glance at the following source code to look at the list of all methods under select class.
Program source code 1:
List of all methods under the select class can be seen in the below screenshot.
Now let’s move towards to learn about the different types of methods under this Select class.
Types of Method in Select Class
The select class provides us multiple methods to select an element from the dropdown list. They are as follows:
This method is used to select an option by passing a value associated with the value attribute. For example, in the previous country list, if we say selectByValue(“AR”), Argentina as a country will get selected.
It takes a String value as a parameter which is one of the values of Select element. It does not return anything.
It neither accept anything as a parameter nor returns anything.
It accepts the integer value (index) as a parameter for deselecting the option from the list and returns nothing.
It accepts the string value as a parameter for deselecting the option from the list and returns nothing.
It accepts the string text as a parameter for deselecting the option and returns nothing.
getFirstSelectedOption() method does not accept anything as a parameter. It returns the first web element from the dropdown list.
It accepts nothing as a parameter and returns the selected options of the list.
It accepts nothing as a parameter and returns boolean. If the list supports multiple options, it will return true otherwise false returns.
Hope that this tutorial has covered almost all the important points related to Select class in Selenium. I hope that you will have understood the syntax and examples of select class. In the next tutorial, we will discuss how to select an element from the dropdown list with realtime scenarios.
Thanks for reading!