App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Get Started free
Home Guide Select Class in Selenium : How to handle dropdown using Selenium

Select Class in Selenium : How to handle dropdown using Selenium

By Neha Vaidya, Community Contributor -

A webpage consists of multiple elements such as text fields, buttons, menus, drop-down options. As every tester is aware, Selenium is the most widely used tool for end-to-end automation testing of a website. This article discusses how Selenium is used to find web elements in a drop-down menu on a website.

What is Select Class in Selenium?

In Selenium, the Select class provides the implementation of the HTML SELECT tag. A Select tag provides the helper methods with select and deselect options. As Select is an ordinary class, its object is created by the keyword New and also specifies the location of the web element.

Select Class Syntax in Selenium:

Select objSelect = new Select();

In the select class syntax above, it clearly states that Select class is asking for an element type object for its constructor, i.e it will create an object of the select class.

How to use Select Class in Selenium?

Selenium offers Select Class which can be used to select value in the dropdown list. There are different methods in the Select Class which can be used to perform different actions in the dropdown element. It allows you to select the option based on its text, index, and value, select multiple options, or deselect all.

Different Select Class Methods to handle Select Dropdown in Selenium

The following are the most commonly used select class methods in selenium to deal with a drop-down list:

Select Class Methods in Selenium

1. selectByVisibleText: selectByVisibleText(String arg0): void

This select class method is used to select one of the options in a drop-down box or an option among multiple selection boxes. It takes a parameter of String which is one of the values of Select element and it returns nothing.

Syntax:

obj.Select.selectByVisibleText(“text”);

Example:

Select objSelect =new Select(driver.findElement(By.id("search-box")));
objSelect.selectByVisibleText("Automation");

2. selectByIndex: selectByIndex(int arg0) : void

This method is similar to ‘selectByVisibleText’, but the difference here is that the user has to provide the index number for the option rather than text. It takes the integer parameter which is the index value of Select element and it returns nothing.

Syntax:

oSelect.selectByIndex(int);

Example:

Select objSelect = new Select(driver.findElement(By.id("Seacrch-box")));
Select.selectByIndex(4);

3. selectByValue: selectByValue(String arg0) : void

This method asks for the value of the desired option rather than the option text or an index. It takes a String parameter which is one of the values of Select element and it does not return anything.

Syntax:

oSelect.selectByValue(“text”);

Example:

Select objSelect = new Select(driver.findElement(By.id("Search-box")));
objSelect.selectByValue("Automation Testing");

4. getOptions: getOptions( ) : List<WebElement>

This method gets all the options belonging to the Select tag. It takes no parameter and returns List<WebElements>.

Syntax:

oSelect.getOptions();

Example:

Select objSelect = new Select(driver.findElement(By.id("Search-box")));
List <WebElement> elementCount = oSelect.getOptions();
System.out.println(elementCount.size());

5. deselectAll()

This method clears all the selected entries. This is only valid when the drop-down element supports multiple selections.

Syntax:

objSelect.deselectAll();

Now let’s delve deeper to understand how to select multiple items with the Select command.

Try Selenium Testing on Real Device Cloud for Free

How to Select multiple items in Dropdown with the Select command?

The multiple select attribute is a boolean expression. This method specifies that multiple options can be selected at once. These options vary for different operating systems and browsers.

  • For Windows: Press the control (ctrl) button to select multiple options.
  • For Mac: Hold down the command button to select multiple options.

Use the isMultiple method to select multiple commands.

isMultiple(): boolean – This method informs whether the Select element supports multiple selection options at the same time or not. This method accepts nothing and returns a boolean value (true/false).

Syntax:

objSelect.isMultiple();

Example:

Select objSelect = new Select(driver.findElement(By.id(Element_ID)));
objSelect.selectByIndex(index);
objSelect.selectByIndex(index);
// Or can be used as
objSelect.selectByVisibleText(text);
objSelect.selectByVisibleText(text);
// Or can be used as
objSelect.selectByValue(value);
objSelect.selectByValue(value);

Now let’s look at a real-time example of the select command.

How to Select Value from Dropdown in Selenium using Select Class: Example

In this example, let’s explore how to select a value in a drop-down list using the Select class in Selenium.

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
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 {
@Test
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "Path_of_Chrome_Driver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.browserstack.com/");
driver.manage().window().maximize();
WebElement developers_dropdown = driver.findElement(By.id("developers-menu-toggle"));
Select objSelect = new Select(developers_dropdown);
objSelect.selectByIndex(2);
Thread.sleep(3000);
driver.navigate("https://www.browserstack.com/");
WebElement solutions_dropdown = driver.findElement(By.id("solutions-menu-dropdown"));
Select solutions = new Select(solutions_dropdown);
solutions.selectByValue("4000");
Thread.sleep(3000);
WebElement solutions_d = driver.findElement(By.id("solutions-menu-dropdown"));
Select s1 = new Select(solutions_d();
s1.selectByVisibleText("Geolocation Testing"); 
}
}

The code above will direct Selenium to navigate to the BrowserStack home page and select the values from the drop-down lists available on the Developers and Solutions tabs as shown in the below figure.

Try Testing Code on BrowserStack Automate for Free

Select class in selenium

 

Select class in selenium webdriver
Since drop-down lists are a common feature on most websites, using the Select class in Selenium is quite useful when it comes to testing that option in websites. This article aims to take users through the process and help them thoroughly test websites to ensure optimal user experience in all possible aspects.

Tags
Automation Testing Selenium Selenium Webdriver

Featured Articles

How to Select Value & Handle Dropdown in Selenium without Select Class

CSS Selector in Selenium: Locate Elements with Examples

App & Browser Testing Made Easy

Seamlessly test across 20,000+ real devices with BrowserStack