Home Guide Select Class in Selenium : How to select a value in dropdown list?

Select Class in Selenium : How to select a value in dropdown list?

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.

The article covers the following topics:

  • Introduction to Select Class in Selenium
  • Different Select Methods
  • Selecting Multiple items with select command
  • Demo: Illustrating the use of Select class in Selenium

Introduction to 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.

Syntax:

Select objSelect = new Select();

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

Different Select Methods

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

1. selectByVisibleText: selectByVisibleText(String arg0): void

This 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 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.

Illustrating the use of Select class in Selenium

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.

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free