Open In App

Selenium Handling Drop-Downs using Java

Last Updated : 21 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Selenium is a powerful tool for web automation and testing, and one of its core components is the WebDriver. When working with web applications, handling drop-down menus is a common requirement. In this guide, we’ll explore how to handle drop-downs using Selenium with Java. Drop-downs are key elements in many web forms, and mastering their manipulation is crucial for effective test automation.

Whether you’re selecting options by visible text, index, or value, Selenium’s Select class provides the functionality you need to automate these interactions seamlessly.

Prerequisite

We will be required 3 main things:

  1. Java Development Kit (JDK) installed.
  2. Browser Driver (e.g., ChromeDriver for Chrome).
  3. IDE like Eclipse or IntelliJ IDEA.

Dependencies Selenium Handling Drop-Downs using Java

We will be required to have dependencies for selenium and for that, we will add dependencies in pom.xml

pom.xml

XML
<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.21.0</version>
</dependency>
<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>4.21.0</version>
</dependency>


Index.html

HTML
<!DOCTYPE html>
<html>
  <head>
    <title>Drop-Down Test</title>
  </head>
  <body>
    <h1>Drop-Down Test Page</h1>
    <form>
      <label for="dropdownId">Select an option:</label>
      <select id="dropdownId" name="options">
        <option value="option1">Option 1</option>
        <option value="option2">Option 2</option>
        <option value="option3">Option 3</option>
      </select>
    </form>
  </body>
</html>


Application.java

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 Application {
    public static void main(String[] args) {
        // Set the path to the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "chromeDriverPath");
        
        // Initialize WebDriver
        WebDriver driver = new ChromeDriver();
        
        // Navigate to the desired webpage
        driver.get("http://127.0.0.1:5500/htmlfile.html");
        
        // Locate the drop-down element
        WebElement dropdownElement = driver.findElement(By.id("dropdownId"));
        
        // Create an instance of Select class
        Select dropdown = new Select(dropdownElement);
        
        // Select option by visible text
        dropdown.selectByVisibleText("Option 2");
        
        // Select option by index
        dropdown.selectByIndex(2); // Selects the second option (index starts from 0)
        
        // Select option by value
        dropdown.selectByValue("option2");
        
        // Get the selected option
        WebElement selectedOption = dropdown.getFirstSelectedOption();
        System.out.println("Selected option: " + selectedOption.getText());
        
        // Close the browser
        //driver.quit();
    }
}


Output

Conclusion

Handling drop-downs in Selenium using Java is a crucial skill for effective web automation. By utilizing the Select class, you can easily interact with standard HTML drop-downs to select options by visible text, index, or value. This capability enhances the automation of web applications and ensures that your tests can accurately interact with a variety of user interface elements. For custom drop-downs, additional strategies may be required. Mastering these techniques will improve the robustness and reliability of your Selenium tests.


Next Article
Article Tags :

Similar Reads