How to Handle Dropdown in Selenium

Learn via video courses
Topics Covered

Overview

Selenium is a powerful testing framework that enables automated testing of web applications. Dropdown menus, which let users choose from a variety of options, are one of the main components of web applications. Because they need particular treatment to pick and deselect options, dropdown in Selenium. So it is important to know how to handle dropdown in Selenium.

Introduction

Users can choose from a variety of alternatives while using dropdown menus, which are frequently used for forms, filters, and navigation. Dropdown in Selenium, however, can be difficult to test automatically because great care must be taken to pick and deselect options. The Select class, which is a built-in class in WebDriver, is one of the techniques Selenium offers for managing dropdown menus. Dropdown in Selenium can also be handled in a number of additional ways, which we shall discuss in this post.

Pre-requisites

Before knowing how to handle dropdowns in Selenium, it's crucial to have a fundamental understanding of Java programming and Selenium WebDriver. We advise reading the official Selenium documentation and completing a basic tutorial if you are new to Selenium.

Select Class in Selenium WebDriver

The Select class is a built-in class in Selenium WebDriver that provides methods for handling dropdowns in Selenium. To use the Select class, you must first locate the dropdown element using a locator method such as findElement().

Once you have located the dropdown element, you can create a Select object and use its methods to select and deselect options. The Select class provides three methods for selecting options:

selectByIndex

This method selects an option based on its index, starting from 0. For example, if you want to select the second option in a dropdown menu, you would use selectByIndex(1)

selectByValue

This method selects an option based on its value attribute. For example, if you have a dropdown in Selenium with options "Red", "Green", and "Blue", and each option has a value attribute of "r", "g", and "b", respectively, you would use selectByValue("r") to select the "Red" option.

selectByVisibleText

This method selects an option based on its visible text. For example, if you have a dropdown menu with options "Apple", "Banana", and "Orange", you would use selectByVisibleText("Banana") to select the "Banana" option.

Different Methods to Handle Dropdown in Selenium Without Using Select Class

There are different methods for how to handle dropdowns in Selenium without the select class.

Method 1: By Storing All the Options in List and Iterating Through It

This method involves storing all the options in a dropdown in Selenium in a List object and then iterating through the list to select an option. Here's an example:

Explanation In this example, we first locate the dropdown element and click it to display the options. We then use the findElements() method to get all the options in the dropdown menu and store them in a List object. Finally, we iterate through the options in the List and select an option with the visible text "Option 1" by clicking it.

Method 2: By Creating Custom Locator and Without Iterating the List

This method involves creating a custom locator that selects an option from the dropdown in Selenium without iterating through the options. Here's an example:

Explanation In this example, we define a custom locator method selectOption() that selects an option from the dropdown in Selenium by visible text. We then locate and select an option with the visible text "Option 1" using the custom locator method.

Method 3: By Using JavaScriptExecutor Class

This method involves using the JavaScriptExecutor class to execute JavaScript code that selects an option from the dropdown in Selenium. Here's an example of how to handle dropdown in Selenium:

In this example, we first locate the dropdown element. We then create a JavaScriptExecutor object and use its executeScript() method to execute JavaScript code that selects an option with the value "Option 1" from the dropdown menu.

Method 4: By Using SendKeys Method

This method involves using the sendKeys() method to select an option from the dropdown in Selenium. Here's an example:

Explanation In this example, we first locate the dropdown element and click it to display the options. We then use the sendKeys() method to select an option with the visible text "Option 1".

Method 5: By Using Actions Class

This method involves using the Actions class to simulate a user selecting an option from the dropdown menu. Here's an example:

Explanation In this example, we first locate the dropdown element and click it to display the options. We then use the Actions class to move the mouse to an option with visible text "Option 1" and click it to select it.

Deselecting Value From a Drop-down Menu

Sometimes, we may want to deselect a value from a dropdown menu. This is also an important part of how to handle dropdowns in Selenium. This can be done using the deselectAll() method of the Select class. Here's an example:

In this example, we first locate the dropdown in Selenium and use the Select class to select an option with the visible text "Option 1". We then use the deselectAll() method of the Select class to deselect the option.

Conclusion

  • The Select class is a powerful tool for handling dropdowns in Selenium. It provides several methods for selecting options from a dropdown menu.
  • There are three methods to select an option from a drop-down menu using the Select class: selectByIndex, selectByValue, and selectByVisibleText.
  • There are several other methods for how to handle dropdown in Selenium, including storing all the options in a List and iterating through them, etc.
  • To deselect an option from a dropdown menu, we can use the deselectAll() method of the Select class.
  • Choosing the appropriate method to handle drop-downs in Selenium depends on the specific requirements of the project.