Handling Dropdowns using Selenium Java

Rinaldo BadigarRinaldo Badigar
3 min read

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 like selectByIndex, selectByValue, and selectByVisibleText 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(), and deselectByVisibleText().

      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.

0
Subscribe to my newsletter

Read articles from Rinaldo Badigar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rinaldo Badigar
Rinaldo Badigar

Software Engineer with 3+ years of experience in Automation, with a strong foundation in Manual, API, and Performance testing, ensuring high-quality software delivery for Ecommerce and Banking applications.