Selenium is the fan favorite of every tester; it has been around for years, supports easy automation of the testing process, and offers plug-in options for various browsers. Another benefit Selenium brings to the testing community is easy access to dropdown menus. As there are various types of dropdowns found across websites—single-select and multi-select—identifying and testing them is quite a task. But Selenium has your back! The Select class in Selenium consists of methods that provide options to manage dropdown menus of all types.
What you would need before applying the Select in Selenium for your use is to understand its intricacies, types, and implementation. And this article is your opportunity to begin learning about the Select class methods in Selenium.
Introduction to Select Class in Selenium
On any webpage, in HTML, the dropdowns are usually implemented using the tag. And to perform certain operations on these dropdown menus, Selenium WebDriver provides Select class and helper methods for executing select and deselect options. To use the Select class in your code, you would need to add org.openqa.selenium.support.ui package of Selenium WebDriver.
Here is the syntax for creating an object of the Select class:
Select ObjSel = new Select();
Different Select Methods
The Select class consists of several Select methods in Selenium to help you identify and test different dropdown menu types. Mainly, there are three Select methods that will be useful for you: Selecting a value, getting dropdown options, and deselecting a value. We will discuss all of these.
How to Select a Value From a Dropdown in Selenium?
The Select class in Selenium offers myriad methods to select a value present in the dropdown using varying parameters.
selectByVisibleText
This method is used to select one option from multiple available options in a dropdown menu. It takes a parameter as a string, which corresponds to the value of the selected item, and returns nothing.
Method syntax:
selectByVisibleText(string);
Example:
Select ObjSel =new Select(driver.findElement(By.id("enter id")));
ObjSel.selectByVisibleText("Automation Testing");
selectByIndex
SelectByIndex has a similar functionality as SelectByVisibleText with a difference in passing parameters. This method takes the index number as the parameter rather than text, which is the index value of the Select element. It also returns nothing.
Method syntax:
selectByIndex(int);
Example:
Select ObjSel = new Select(driver.findElement(By.id("enter id")));
ObjSel.selectByIndex(2);
selectByValue
This method selects the value of the dropdown menu option rather than the option itself. It takes a string parameter and returns nothing.
For instance, every dropdown option has a value corresponding to the option. Below is an example:
Method syntax:
selectByValue(string);
Example:
Select ObjSelect = new Select(driver.findElement(By.id("enter id")));
ObjSelect.selectByValue("crowd-testing");
How to Deselect a Value From a Dropdown in Selenium?
Similar to how the Select class provides methods to choose dropdown options, it also offers methods to deselect those options. But keep in mind that it only works for multi-select dropdown menus. Here are all the different deselect methods you can use to deselect already selected options in a dropdown menu:
deselectAll
The deselectAll method works only for dropdown menus that support multiple selections. You can run this method to clear all the selected entries in the dropdown.
Method syntax:
deselectAll();
Example:
Select ObjSel = new Select(driver.findElement(By.id("enter id")));
ObjSel.deselectAll();
deselectByIndex()
The deselectByIndex method does the opposite of selectByIndex. It takes the integer parameter and deselects the particular index.
Method syntax:
deselectByIndex(int)
Example:
Select ObjSel= new Select(driver.findElement(By.id("enter id")));
ObjSel.deselectByIndex(3);
deselectByValue()
This method works similarly to deselectByIndex and takes the value of the corresponding dropdown option to perform the deselection process.
Method syntax:
deselectByValue(string)
Example:
Select ObjSel= new Select(driver.findElement(By.id("enter id")));
ObjSel.deselectByValue("3");
deselectByVisibleText()
The deselectByVisibleText method does the deselection operation using the dropdown menu text as the parameter.
Method syntax:
deselectByVisibleText(String)
Example:
Select ObjSel = new Select(driver.findElement(By.id("enter id")));
ObjSel.deselectByVisibleText("Automation Testing");
How to Get Options from a Dropdown in Selenium?
After learning how to select single and multiple options in a dropdown menu, you now need to understand how to use the value present in the dropdown and what all values are selected among them. The Select class offers different methods to get options from the dropdown:
getOptions
The GetOptions method takes and displays all the options belonging to the Select tag. No matter if the dropdown menu is single-select or multi-select, you can get all the options using this method. It takes no input and returns a list of web elements.
Method syntax:
Select.getOptions();
Example:
Select ObjSel = new Select(driver.findElement(By.id("enter id")));
List <WebElement> options = ObjSel.getOptions();
getFirstSelectedOption()
The getFirstSelectedOption() returns the first option selected in the dropdown. If the menu is single-select, it will return the selected option. If the menu is multi-select, the method will return the first selected option (a WebElement) among all the selected options.
Method syntax:
getFirstSelectedOption(): WebElement
Example:
Select ObjSel = new Select(driver.findElement(By.id("enter id")));
WebElement firstSelectedOptionis = ObjSel.getFirstSelectedOption();
getSelectedOptions()
This method returns all the selected options in the dropdown. For single-select, it gives only one output. For multi-select, the method lists all the selected options. getSelectedOption() returns a list of WebElements.
Method syntax:
getAllSelectedOptions(): List<WebElement>
Example:
Select ObjSel = new Select(driver.findElement(By.id("enter id")));
List<WebElement> AllselectedOptions = ObjSel.getAllSelectedOptions();
How to Select Multiple Items With the Select Command?
If the select tag consists of multiple attributes, it means that the dropdown allows the user to select more than one option. You can use all the above-discussed methods to select multiple options one by one; run the method multiple times for different values. But it isn’t as simple as it sounds. The work is lengthy and requires long manual labor. Here’s an easy and direct way.
How to Check Whether the Dropdown is Multi-Select?
To select multiple items with the Select command, you will have to execute a boolean expression. It is a method that specifies that you can select multiple options at once, and it varies based on the operating system.
For Windows, hold the control (Ctrl) button to select multiple options, whereas for MacOS, use the command button to perform the same operation.
And while coding, the isMultiple method of the Select class is used to validate if the dropdown allows the selection of multiple items. Even when working with Selenium, the isMultiple method is useful in performing multiple selections. The method returns a boolean value without taking any parameter as the input.
Method syntax:
isMultiple(): boolean
Use the isMultiple method to validate if the dropdown allows the selection of multiple items
Here’s a code snippet to run the isMultiple method and select more than one option if the method returns TRUE for multiple selections:
Select ObjSel = new Select(driver.findElement(By.id(“enter id”]);
if(ObjSel.isMultiple())
{ ObjSel.selectByIndex(2);
ObjSel.selectByIndex(3);
ObjSel.selectByValue("automation-testing");
ObjSel.selectByValue("cloud-based-testing");
ObjSel.selectByVisibleText("Automation Testing");
ObjSel.selectByVisibleText("Cloud Based Testing");
}
Select Class Using Testsigma
As opposed to Selenium, Testsigma is a codeless platform that does not necessarily require you to use the Select class to execute functions related to the dropdown menus. Our tool supports Natural Language Processing (NLP) statements for regular Select elements on Web pages. Here are some of the NLP options you can explore for dropdown web elements that with Select HTML tags. But since not all web pages use a similar Select class; newly built web pages implement plug-in-based Select lists – Material UI, Bootstrap, or jQuery select lists. Testsigma supports the testing of all these cases through Click NLPs.
Summary
A web page consists of multiple web elements, all functioning differently and requiring their own set of test cases for verification. Dropdowns are one such web element that you can test only through the Select class in Selenium, which many testers use. The class contains several methods to select and deselect dropdown menu options, which can either be single-select or multi-select. All of these methods require different parameters and return either nothing or boolean values based on their functions.
We also talk about Testsigma and the NLPs it provides for you to verify the Select tags in your code. Easier to manage and work with, the no-code requirement of Testsigma makes checking dropdowns a painless task.
Frequently Asked Questions (FAQs)
What is the syntax of the Select class?
The syntax of the Select class is Select select = new Select(WebElement webelement);
Here, WebElement is the parameter that is the return value of the locators of the specified element.
How to handle the Select class in Selenium?
You can run the Select class in Selenium only after adding org.openqa.selenium.support.ui package of Selenium WebDriver, which helps you to handle the Select class.
Top comments (0)