Handling Dropdowns using Selenium Java
Table of contents
Static Dropdown:
Description: Static dropdowns are the simplest type, where the options do not change dynamically. The list of choices remains constant, and users can select from a predefined set of options.
Handling in Selenium Java: Static dropdowns are often managed using the
Select
class in Selenium. It provides methods likeselectByIndex
,selectByValue
, andselectByVisibleText
for interacting with the options.You can identify the static dropdown by the <select> tag.
Select Class - The Maestro of Dropdowns ๐ถ
The select class can be used to work with the dropdown elements as youโll be able to choose the option using the various Selenium commands.
Select sel = new Select(driver.findElement(By.xpath("XPATHVALUE"))); sel.selectByIndex(INDEX_VALUE); sel.selectByValue("VALUE"); sel.selectByVisibleText("VISIBLE_TEXT_VALUE");
getOptions() - Counting Choices ๐
Count the number of options in a dropdown with the
getOptions()
command.int count = sel.getOptions().size();
getFirstSelectedOption() - The Chosen One ๐
Discover the first selected value from the dropdown with
getFirstSelectedOption()
.String selectedOption = sel.getFirstSelectedOption().getText();
getAllSelectedOptions() - Many Choices ๐
If you want to return a number of selected options, youโll have to use
getAllSelectedOptions()
.int count = sel.getAllSelectedOptions().size();
isMultiple() - Multiselect Mysteries ๐ค
But what if the dropdown isnโt made for multiple options? So the isMultiple() command will come in handy as it returns true if the element supports multiple selecting options at the same time.
boolean isMultiselect = sel.isMultiple();
Deselect the Options - Undoing Choices ๐
Undo your selections using commands like
deselectAll()
,deselectByIndex()
,deselectByValue()
, anddeselectByVisibleText()
.sel.deselectAll(); sel.deselectByIndex(INDEX_VALUE); sel.deselectByValue("VALUE"); sel.deselectByVisibleText("VISIBLE_TEXT_VALUE");
Dynamic Dropdown:
Description: Dynamic dropdowns present a challenge as the available options may change based on user input or other factors. Options are loaded dynamically, making traditional handling methods less straightforward.
Handling in Selenium Java: Handling dynamic dropdowns involves the use of explicit waits and dynamic locators to ensure that the dropdown options are fully loaded before interacting with them.
Let's consider an example scenario on a travel planning website where users need to select their departure and destination cities. As the user starts typing the city name in the "Departure City" or "Destination City" input field, the website dynamically suggests matching cities in a dropdown menu.
Autosuggestive Dropdown:
Description: Autosuggestive dropdowns provide users with suggestions as they type into the input field. The dropdown dynamically displays matching options based on the entered text, assisting users in selecting the desired choice.
Handling in Selenium Java: Autosuggestive dropdowns are typically managed by simulating user input, waiting for suggestion lists to appear, and interacting with the dynamically generated options. Selenium commands such as sendKeys and explicit waits are commonly used.
Let's consider an example scenario on a travel planning website where users need to select their country. As the user starts typing the country names in the input field, the website dynamically suggests matching countries in a dropdown menu as shown in the above image.
Understanding and effectively handling these three types of dropdowns is crucial for creating robust and reliable Selenium automation scripts tailored to the diverse dropdown scenarios encountered during web testing.