Handling Dropdowns in Selenium WebDriver Using Java

Dropdowns are a common UI element in web applications, allowing users to select one or more options from a list. When automating such interactions using Selenium WebDriver in Java, it’s essential to understand how to work with these elements effectively. Selenium provides a straightforward way to handle dropdowns through the Select class. In this blog post, we’ll walk through the different techniques for interacting with dropdowns using Selenium WebDriver and Java.


Understanding Dropdown Elements

In HTML, a standard dropdown is implemented using the <select> tag. Here’s a simple example:

html


<select id="country">

  <option value="us">United States</option>

  <option value="uk">United Kingdom</option>

  <option value="in">India</option>

</select>

Selenium can handle these types of dropdowns using the Select class found in the org.openqa.selenium.support.ui package.


Setting Up Selenium with Java

Before handling dropdowns, ensure you’ve properly set up your Selenium environment. You’ll need:

  • Java Development Kit (JDK)
  • Selenium WebDriver library
  • A browser driver (e.g., ChromeDriver)

Here’s a Maven dependency for Selenium:


xml


<dependency>

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

  <artifactId>selenium-java</artifactId>

  <version>4.20.0</version>

</dependency>

Interacting with Dropdowns Using the Select Class

Here’s how you can select options from a dropdown using Selenium:


java


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 DropdownExample {

    public static void main(String[] args) {

        WebDriver driver = new ChromeDriver();

        driver.get("https://example.com");


        WebElement dropdown = driver.findElement(By.id("country"));

        Select select = new Select(dropdown);


        // Select by visible text

        select.selectByVisibleText("India");


        // Select by value attribute

        select.selectByValue("uk");


        // Select by index (starts from 0)

        select.selectByIndex(0);


        driver.quit();

    }

}

Useful Select Class Methods

selectByVisibleText(String text): Selects option by its visible text.

selectByValue(String value): Selects option by the value of the "value" attribute.

selectByIndex(int index): Selects option by index (starting at 0).

getFirstSelectedOption(): Returns the first selected option in the dropdown.

getOptions(): Returns all options within the dropdown.

isMultiple(): Checks if the dropdown allows multiple selections.


Handling Multi-Select Dropdowns

Some dropdowns allow users to select more than one option. To interact with them:

java

Copy

Edit

if (select.isMultiple()) {

    select.selectByValue("us");

    select.selectByValue("uk");

    select.deselectAll(); // Deselects all options

}

Tips for Working with Custom Dropdowns

Not all dropdowns use the standard <select> tag. Modern UIs often implement custom dropdowns using <div>, <ul>, or JavaScript frameworks. In such cases:

Click to open the dropdown.

Locate the desired item using XPath or CSS selectors.

Click the item.

Example:

java

Copy

Edit

driver.findElement(By.id("custom-dropdown")).click();

driver.findElement(By.xpath("//li[text()='India']")).click();

Conclusion

Handling dropdowns in Selenium WebDriver using Java is straightforward when working with standard HTML <select> elements thanks to the Select class. For custom-styled dropdowns, you'll need to rely on standard WebDriver commands to interact with the DOM. Understanding both scenarios equips you to automate a wide range of real-world web interfaces. Mastering dropdown handling is a key step in building reliable and robust Selenium test automation scripts.

 Learn Selenium with Java  Course  in Hyderabad

Read More: Using XPath Selectors in Selenium WebDriver with Java

Visit Quality Thought Training Institute Hyderabad
Get Direction


Comments

Popular posts from this blog

Tosca vs Selenium: Which One to Choose?

Flask API Optimization: Using Content Delivery Networks (CDNs)

Using ID and Name Locators in Selenium Python