0
0
Selenium-pythonHow-ToBeginner · 4 min read

How to Use Select Class in Selenium for Dropdown Handling

Use the Select class in Selenium to handle dropdown menus by first locating the <select> element, then creating a Select object with it. You can select options by visible text, value, or index using methods like selectByVisibleText(), selectByValue(), or selectByIndex().
📐

Syntax

The Select class requires a WebElement representing the <select> dropdown. You create a Select object by passing this element to its constructor. Then, use its methods to select options.

  • selectByVisibleText(String text): Selects option by the text shown.
  • selectByValue(String value): Selects option by the value attribute.
  • selectByIndex(int index): Selects option by its position starting at 0.
java
WebElement dropdown = driver.findElement(By.id("dropdownId"));
Select select = new Select(dropdown);
select.selectByVisibleText("Option Text");
// or
select.selectByValue("optionValue");
// or
select.selectByIndex(2);
💻

Example

This example shows how to open a webpage with a dropdown, locate it, create a Select object, and select an option by visible text. It prints the selected option to confirm the action.

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 SelectExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://www.seleniumeasy.com/test/basic-select-dropdown-demo.html");
            WebElement dropdown = driver.findElement(By.id("select-demo"));
            Select select = new Select(dropdown);
            select.selectByVisibleText("Friday");
            String selected = select.getFirstSelectedOption().getText();
            System.out.println("Selected option: " + selected);
        } finally {
            driver.quit();
        }
    }
}
Output
Selected option: Friday
⚠️

Common Pitfalls

  • Trying to use Select on elements that are not <select> tags causes errors.
  • Not waiting for the dropdown to be present before interacting can cause NoSuchElementException.
  • Using incorrect locators leads to failure to find the dropdown.
  • For multi-select dropdowns, use select.isMultiple() to check if multiple selections are allowed.
java
/* Wrong: Using Select on a non-select element */
WebElement divElement = driver.findElement(By.id("not-a-select"));
Select select = new Select(divElement); // Throws UnexpectedTagNameException

/* Right: Always ensure the element is a <select> tag */
WebElement dropdown = driver.findElement(By.tagName("select"));
Select select = new Select(dropdown);
📊

Quick Reference

MethodDescription
selectByVisibleText(String text)Select option by visible text
selectByValue(String value)Select option by value attribute
selectByIndex(int index)Select option by index starting at 0
getFirstSelectedOption()Get the currently selected option
getOptions()Get all options in the dropdown
isMultiple()Check if dropdown supports multiple selections

Key Takeaways

Use the Select class only with tag.