Software Testing

Selenium Select Class

Selenium Select Class

In Selenium WebDriver, the Select class is used to handle HTML drop-down elements that are created using the <select> tag. It provides built-in methods to easily select and deselect options from a drop-down list.

When to Use

Use the Select class only when the drop-down element is implemented with the
<select> HTML tag.

  • Works with standard HTML <select> dropdowns
  • Does not work with custom dropdowns created using <div> or JavaScript

Importing the Select Class

Java

import org.openqa.selenium.support.ui.Select;

Python

from selenium.webdriver.support.ui import Select

Creating a Select Object

Java


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

Python


dropdown = driver.find_element(By.id("country"))
select = Select(dropdown)
  

Selecting Options

Select by Visible Text

select.selectByVisibleText("India");

Select by Value

select.selectByValue("IN");

Select by Index

select.selectByIndex(2);

Handling Multi-Select Dropdowns

Some dropdowns allow selecting more than one option. The Select class provides
methods to handle such scenarios.

Check if Multiple Selection Is Allowed

select.isMultiple();

Select Multiple Options


select.selectByVisibleText("Apple");
select.selectByVisibleText("Orange");
  

Deselect Options


select.deselectAll();
select.deselectByVisibleText("Apple");
  

Getting Selected Options

Get First Selected Option

select.getFirstSelectedOption().getText();

Get All Selected Options

select.getAllSelectedOptions();

Common Exceptions

  • UnexpectedTagNameException – Element is not a <select> tag
  • NoSuchElementException – Option does not exist
  • ElementNotInteractableException – Dropdown is not visible or enabled

 

The Selenium Select class simplifies interaction with standard HTML dropdowns. By using its built-in methods, you can write cleaner, more reliable test scripts for selecting and managing options in web applications.