Tech Blog

Testing

Handling Dropdown using Selenium Webdriver

By Vaanyashree P M3 min read
← Back to blog

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.

Keep reading

You may be interested in

Browse all articles →
Software Testing: Challenges and Solutions
February 8, 20244 min read

Software Testing: Challenges and Solutions

Introduction – Testing software is a crucial aspect of the development process. It makes sure that the product’s functionality works as expected, fulfills the user requirements, and makes sure it works without errors. In this blog, we will list down some of the most common challenges faced in software testing and conclude with how to […]

By Saboor ChandshahaRead More →
Streamlining Software Quality: The Power of Automating Black Box Testing
February 8, 20244 min read

Streamlining Software Quality: The Power of Automating Black Box Testing

Introduction :  In the dynamic realm of software development, ensuring the robustness and reliability of applications is of paramount importance. Black box testing, a technique that assesses software functionality without requiring knowledge of its internal code, plays a vital role in identifying potential defects. As the complexity of software systems continues to escalate, the manual […]

By Anthony Gilchrist G SRead More →
Implementing best practices for end-to-end testing in cloud migration
January 31, 20245 min read

Implementing best practices for end-to-end testing in cloud migration

Importance of End-to-End Testing Assume an organization’s core systems are now hosted on the cloud—a complex ecosystem linked by data threads. End-to-end testing is the foundation upon which the reliability and performance of this interconnected web is built. This rigorous testing technique makes it possible to detect vulnerabilities, optimize operations, and improve user experience. Active […]

By Rahul ChakravartiRead More →