Handling Different Types of Drop-downs in Selenium with Java

Selenium is a powerful tool for automating web applications, allowing testers and developers to simulate user interactions. One common interaction that needs to be handled is selecting options from drop-down menus. In this blog post, we’ll explore various types of drop-downs and how to interact with them using Selenium WebDriver in Java.

Understanding Drop-downs

Drop-downs are commonly used in web applications to allow users to select one or multiple options from a list. They can be broadly categorized into two types:

  1. Single Select Drop-down: Allows the selection of only one option at a time.
  2. Multi-Select Drop-down: Allows the selection of multiple options simultaneously.

Setting Up Selenium WebDriver

Before we dive into handling drop-downs, ensure you have the necessary setup. You’ll need:

  • Java Development Kit (JDK)
  • Maven or Gradle for dependency management
  • Selenium WebDriver library
  • A browser driver (e.g., ChromeDriver for Chrome)

You can include the Selenium dependency in your pom.xml for Maven:

<dependency>

    <groupId>org.seleniumhq.selenium</groupId>

    <artifactId>selenium-java</artifactId>

    <version>4.21.0</version> <!-- Use the latest version -->

</dependency>

Interacting with Single Select Drop-downs

For single-select drop-downs, Selenium provides the Select class, which offers methods to interact with the drop-down options easily.

Example Code

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class SingleSelectDropDown {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("URL_OF_YOUR_WEBPAGE");

        // Locate the drop-down element
        WebElement dropDown = driver.findElement(By.id("dropdownId"));

        // Create a Select instance
        Select select = new Select(dropDown);

        // Select by visible text
        select.selectByVisibleText("Option1");

        // Select by index
        select.selectByIndex(2);

        // Select by value
        select.selectByValue("valueOfOption3");

        driver.quit();
    }
}

Interacting with Multi-Select Drop-downs

Handling multi-select drop-downs is similar but requires using additional methods provided by the Select class.

Example Code

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

import java.util.List;

public class MultiSelectDropDown {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("URL_OF_YOUR_WEBPAGE");

        // Locate the multi-select drop-down element
        WebElement multiSelect = driver.findElement(By.id("multiSelectId"));

        // Create a Select instance
        Select select = new Select(multiSelect);

        // Check if the drop-down allows multiple selections
        if (select.isMultiple()) {
            // Select multiple options
            select.selectByVisibleText("Option1");
            select.selectByVisibleText("Option2");

            // Deselect an option
            select.deselectByVisibleText("Option1");

            // Get all selected options
            List<WebElement> selectedOptions = select.getAllSelectedOptions();
            for (WebElement option : selectedOptions) {
                System.out.println("Selected Option: " + option.getText());
            }
        }

        driver.quit();
    }
}

Handling Dynamic Drop-downs

Dynamic drop-downs, where options load based on user interactions or external data, can be handled by waiting for the options to load. Use Selenium's WebDriverWait to manage such cases effectively.

Example Code

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

public class DynamicDropDown {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("URL_OF_YOUR_WEBPAGE");

        // Interact with a trigger for the dynamic drop-down
        WebElement triggerElement = driver.findElement(By.id("triggerId"));
        triggerElement.click();

        // Wait for the drop-down options to be present
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dynamicDropdownId")));

        // Now locate the dynamic drop-down
        WebElement dynamicDropDown = driver.findElement(By.id("dynamicDropdownId"));
        Select select = new Select(dynamicDropDown);
        select.selectByVisibleText("Dynamic Option");

        driver.quit();
    }
}

Followers