Handling Dropdown using Selenium Webdriver


What is Dropdown?

Dropdown is a menu that consists of several options and one can select single or multiple options. Generally, the dropdown displays a placeholder saying “select” or the very first option will be displayed once the option is selected and then the selected option will be displayed. Handling dropdowns through Selenium in Java is one of the most effective and easier ways. In this case, we use certain classes and methods of Java to handle dropdowns.

Problems with poorly implemented dropdown

We often come across a poorly-implemented navigational dropdown menu, where we have no way to exit the menu without selecting any one of the options from the dropdown. Isn’t it an annoying experience? To ensure a seamless user experience across the application or website, test automation engineers are often required to investigate any possible flaw with the dropdown.

A Solution to handle the dropdown

Single Dropdown: Here, we are provided with a menu, where a user can select only one option out of multiple options present in the menu. 

Note:-  <select> is the element that we use to create the dropdown list, and <option> is used to give the multiple options button for the users.

The Select class in the Selenium web driver is used for selecting and deselecting options from the dropdown list. The web element is passed as a parameter to its constructor to initialize the Select class.

WebElement VariableName = driver.findElement(By.id(“Values”));

For ex:
WebElement ele = driver.findElementBy.id(“Countries”);

There are three different ways to handle dropdown in Selenium using Java.

1) selectByIndex()
Selecting the attribute based on the attribute value stored in the array
Indexing starts from 0.
Example: s.selectByIndex(“0”);

2) selectByValue()
Selecting the attribute by the value of the attribute
Example: s.selectByValue(“4”);

3) selectByVisibleText()
Selecting the attribute based on the text visible on the screen
Example: s.selectByVisibleText(“Brazil”);

Let us perform the execution of the Countries dropdown in any of the applications, which has a country-based dropdown. While handling Selenium, we need to follow the following steps:

Step 1: We need a webpage where we can use the script. Therefore, we have to create an HTML script for the Countries dropdown in any of the applications having a country-based dropdown page and save it in the .html extension.

Step 2: We need to create a Selenium script for handling the dropdown of the web page.

Here are the ways by which we select the same element:

1) selectByIndex()

WebElement ele = driver.findElement(By.id("Countries"));
Select s= new Select(ele);
s.selectByIndex(9);

2) selectByValue()

WebElement ele = driver.findElement(By.id("Countries"));
Select s= new Select(ele);
s.selectByValue("10");

3) selectByVisibleText()

WebElement ele = driver.findElement(By.id("Countries"));
Select s= new Select(ele);
s.selectByVisibleText("France");

Conclusion:

Using the above mentioned HTML and Selenium script, we will be able to handle all forms of the dropdown, which will be used for selecting any value from the dropdown and can be considered to be of great help while running the automation for the same. The methods help the automation engineer to handle dropdown smoothly using this script and to manage web pages as well without manual intervention.

Leave A Comment

Your email address will not be published. Required fields are marked *