In this tutorial, we will learn how to handle Drop Down and Multiple Select Operations.

Drop-Down Box

Before we can control drop-down boxes, we must do following two things:

  1. Import the package org.openqa.selenium.support.ui.Select
  2. Instantiate the drop-down box as a "Select" object in WebDriver

As an example, go to Mercury Tours' Registration page (http://newtours.demoaut.com/mercuryregister.php) and notice the "Country" drop-down box there.  

How to Select Option from DropDown using Selenium Webdriver

Step 1

Import the "Select" package.

How to Select Option from DropDown using Selenium Webdriver

Step 2

Declare the drop-down element as an instance of the Select class. In the example below, we named this instance as "drpCountry".

How to Select Option from DropDown using Selenium Webdriver

Step 3

We can now start controlling "drpCountry" by using any of the available Select methods. The sample code below will select the option "ANTARCTICA."

How to Select Option from DropDown using Selenium Webdriver

Selecting Items in a Multiple SELECT elements

We can also use the selectByVisibleText() method in selecting multiple options in a multi SELECT element. As an example, we will take http://jsbin.com/osebed/2 as the base URL. It contains a drop-down box that allows multiple selections at a time.

How to Select Option from DropDown using Selenium Webdriver

The code below will select the first two options using the selectByVisibleText() method.

How to Select Option from DropDown using Selenium Webdriver

Select Methods

The following are the most common methods used on drop-down elements.

Method Description
selectByVisibleText() and deselectByVisibleText() Example: How to Select Option from DropDown using Selenium Webdriver
  • Selects/deselects the option that displays the text matching the parameter.
  • Parameter: The exactly displayed text of a particular option
selectByValue() and deselectByValue() Example: How to Select Option from DropDown using Selenium Webdriver
  • Selects/deselects the option whose "value" attribute matches the specified parameter.
  • Parameter: value of the "value" attribute
  • Remember that not all drop-down options have the same text and "value", like in the example below.
How to Select Option from DropDown using Selenium Webdriver  
selectByIndex() and deselectByIndex() Example: How to Select Option from DropDown using Selenium Webdriver  
  • Selects/deselects the option at the given index.
  • Parameter: the index of the option to be selected.
isMultiple() Example: How to Select Option from DropDown using Selenium Webdriver  
  • Returns TRUE if the drop-down element allows multiple selections at a time; FALSE if otherwise.
  • Needs parameters needed
deselectAll() Example: How to Select Option from DropDown using Selenium Webdriver  
  • Clears all selected entries. This is only valid when the drop-down element supports multiple selections.
  • No parameters needed

Here is the complete code

package newproject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class accessDropDown {
 public static void main(String[] args) { 
		System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");
	    String baseUrl = "http://newtours.demoaut.com/mercuryregister.php";
	    WebDriver driver = new FirefoxDriver();
		driver.get(baseURL);

		Select drpCountry = new Select(driver.findElement(By.name("country")));
		drpCountry.select.ByVisibletext("ANTARCTICA")

		//Selecting Items in a Multiple SELECT elements
		driver.get(http://jsbin.com/osebed/2);
		Select fruits = new Select(driver.findElement(By.id("fruits")));
		fruits.selectByVisibleText("Banana")
		fruits.selectByIndex(1);
 }
}

Summary

Element Command Description
Drop-Down Box selectByVisibleText()/ deselectByVisibleText() selects/deselects an option by its displayed text
selectByValue()/ deselectByValue() selects/deselects an option by the value of its "value" attribute
selectByIndex()/ deselectByIndex() selects/deselects an option by its index
isMultiple() returns TRUE if the drop-down element allows multiple selection at a time; FALSE if otherwise
deselectAll() deselects all previously selected options

To control drop-down boxes, you must first import the org.openqa.selenium.support.ui.Select package and then create a Select instance.