Handling Drop down

Purpose of this tutorial is to give you some basic idea about handling drop down using Selenium WebDriver. It is commonly asked question during the interview. So in this tutorial, we will discuss like what is drop down and how we can handle this with one brief example. Let’s start.
Drop down is basically one web element which contain one or more than one value. We use Select class and it’s method to handle drop down in Selenium. This Select class is used for selecting or deselecting values options from drop down.
For example:

WebElement dropdownElement = driver.findElement(By.xpath("dropdown_webelement_xpath"));
Select select = new Select(dropdownElement);

Note: We can choose any of locator type here like id, name, classname, xpath etc for element dropdownElement.
Now, after creating Select class object we can get all its method to handle dropdown.

Ways to select values from dropdown
We have various ways to select values from dropdown

  • selectByIndex(int indexValue)
  • selectByValue(String value)
  • selectByVisibleText(String text)
  • getOptions()

1. selectByIndex(int indexValue)
In this method, we give the index value in the drop down.
For example: Let’s suppose, we need to select 3rd position value. Index start from 0th so index 2 will be 3rd position in dropdown.

WebElement dropdownElement = driver.findElement(By.xpath("dropdown_webelement_xpath"));
Select select = new Select(dropdownElement);
select.selectByIndex(2);

2. selectByValue(String value)
In this method, we give the string value as parameter for selecting value in the drop down other than the text. This value can be similar or not with available text in drop down.
For example: Let’s suppose, we need to select value as ‘employed’ in employment status dropdown.

WebElement dropdownElement = driver.findElement(By.xpath("dropdown_employmentStatusElement_xpath"));
Select select = new Select(dropdownElement);
select.selectByValue(“employed”);

3. selectByVisibleText(String text)
In this method, we give the visible text available on browser GUI.
For example: Let’s suppose, we need to select visible text as ‘male’ in gender dropdown.

 
WebElement dropdownElement = driver.findElement(By.xpath("dropdown_genderElement_xpath"));
Select select = new Select(dropdownElement);
select.selectByVisibleText(“male”);

4. getOptions()
This method returns a list of all the options available in dropdown. It returns List.
For example: Let’s suppose, we need to count the number of states available in a dropdown.

  
WebElement dropdownElement = driver.findElement(By.xpath("stateElement_xpath"));
Select select = new Select(dropdownElement);
List stateCount = select.getOptions();
System.out.println(“Number of states ” + stateCount.size());

We have few others useful methods available in Select class.
5. isMultiple()
This method returns a boolean value like true or false. It verify whether drop-down allow multi select option or not.
6. getAllSelectedOptions()
This method returns a list of all the selected options in drop down
7. getFirstSelectedOption()
This method returns the first selected option, if there is an option of multi-select in drop down.
8. deselectAll()
This method deselect all the selected options in drop down.
9. deselectByIndex(int indexValue)
This method deselect the drop down option by its index value.
10. deselectByValue(String value)
This method deselect the drop down option by its value attribute.
11. deselectByVisibleText(String text)
This method deselect the drop down option by visible text.

Let’s see one brief example:
In our example, we will handle birthday dropdown available in facebook login page. See below screenshot.

facebook login page

 

Code Section:

package testpackage;

import java.util.List;
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;
import org.testng.annotations.Test;

public class DropdownTest {

	@Test
	public void dropDownCheck() {

		System.setProperty("webdriver.chrome.driver", "C:/Users/Hemant/workspace/Project07Aug/testgitproject1/chromedriver_win32/chromedriver.exe");
		
		// Open chrome driver
		WebDriver driver = new ChromeDriver(); 

		// Open facebook page url on browser window
		driver.get("https://www.facebook.com/");

		// Here we have dropdown for Birthday field
		// Selecting month from dropdown
		WebElement elements = driver.findElement(By.id("month"));

		// First we need to create object of Select class and pass above created
		// elements as parameter
		Select optionSelect = new Select(elements);

		// Select dropdown value by its index
		// For example, if we need to select month as March. So we will set
		// index as 2 becauce it's value start from 0
		optionSelect.selectByIndex(2);

		// Select drop down option by Value attribute
		optionSelect.selectByValue("6");

		// Select srop down option by visible text
		optionSelect.selectByVisibleText("Oct");

		// We can use getOptions method to get all the options in list of web elements
		List list = optionSelect.getOptions();
		System.out.println("Size of drop down" + list.size());

	}
}

Output:

Dropdown value select by Index 2
Dropdown value select by value 6
Dropdown value select by text 'Oct'
Size of drop down 13