Working with Dropdown in Selenium WebDriver

In this tutorial, we will learn how to work with Dropdowns using Selenium WebDriver. We all must have come across Dropdowns while filling up forms where you select one option out of several options. Sometimes, you get a dropdown where you can select multiple options too. we will learn how to handle those dropdowns using Selenium WebDriver as well. Selenium provides us with a “Select”  class to perform operations on dropdown. This class can be found under the Selenium’s Support.UI.Select package.

The best way to learn a topic is with practical example. Let us consider below scenario:

  1. Launch browser.
  2. Invoke URL “http://www.ebay.in”
  3. Enter some value in “Search Product” field. Let’s say we enter “Apple Watches”.
  4. Select the category on your search from the dropdown. Let’s select category as “Watches”.
  5. Click on the Search button.
  6. Get the value of the total listings obtained as per your search.

Here is the Sample code:

package day4;

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;

public class EbayProject {

	WebDriver Driver;
	public void invokeBrowser(){
		/*Setting property of a system variable used by chrome driver*/
		System.setProperty("webdriver.chrome.driver", "C:\\Users\\sdhingra\\workspace\\libs\\chromedriver.exe");

		Driver = new ChromeDriver();

		String url = "http://www.ebay.in";

		//To maximise the browser
		Driver.manage().window().maximize();
		//To delete all cookies
		Driver.manage().deleteAllCookies();
		//To invoke URL
		Driver.get(url);

	}

	public void searchProduct(String product, String category){

		//Locating Search Product field and entering value in it
		Driver.findElement(By.id("gh-ac")).sendKeys(product);
		//Locating dropDown
		WebElement dropElement = Driver.findElement(By.id("gh-cat"));
		//Object instantiation for selecting values from dropDown
		Select dropdown = new Select(dropElement);

		//Select value from the  DropDown
		dropdown.selectByVisibleText("Watches");
		//Clicking on Search button
		Driver.findElement(By.id("gh-btn")).click();
		//Fetching count of total listings
		String result = Driver.findElement(By.className("listingscnt")).getText();
		//Printing the count of  total listings
		System.out.println("Result is : "+ result);
	}

**Here creation of another class where main method resides to execute the Script.

package day4;

public class DemoEbay {

/**
* @param args
*/
public static void main(String[] args) {

//Object instantiation for accessing 'EbayProject' class methods
EbayProject ebay = new EbayProject();
//Calling method invokeBrowser
ebay.invokeBrowser();
//Passing value for  Search product field and value to be selected from dropDown
ebay.searchProduct("Apple watches", "Watches");
}

}

EXPLANATION- The code above covers all the steps mentioned in our Scenario. Basically launching the browser, maximising browser window, deleting all the cookies and then invoking URL remains same in every script. Now, let’s have a look at the Screenshots below.

  • Locating the Search Product text field with its id and entering the value in it. We are passing “Apple watches” in the argument “product”.

           Driver.findElement(By.id(“gh-ac”)).sendKeys(product);

Locating Search Product text field

 Locating Search Product text field

2. Here we are locating DropDown field with its unique id.

WebElement dropElement = Driver.findElement(By.id(“gh-cat”));

Locating the dropDown field

Locating the dropDown field

3. For selecting any value from DropDown we have “Select” class that comes with our Selenium package.We need to import this package-import org.openqa.selenium.support.ui.Select;

Now, the most thing to note is we can Select the value from a dropDown through several methods and there are several actions that can be performed on a dropDown. So, here is the list below-

Select class from Selenium WebDriver is used to handle Dropdown.

Now this dropdown object of class Select has below methods to work with on a Dropdown:

  • selectByValue(“value”); – Selects an option by value from a dropdown.
  • selectByVisibleText(“text”); – Selects an option by visible text from a dropdown.
  • selectByIndex(index); – Selects by index from the dropdown.
  • deselectByValue(“value”); – Deselects an option by value from a dropdown.
  • deselectByVisibleText(“text”); – Deselects an option by visible text from a dropdown.
  • deselectByIndex(index); – Deselects an option by index from a dropdown.
  • deselectAll(); – Deselects All options in a dropdown.
  • isMultiple(); – Boolean operation which verifies whether a dropdown allows multiple selection or not.
  • getOptions(); – Returns a list of all options from a dropdown.
  • getFirstSelectedOption(); – Returns first option selected in a dropdown as a WebElement .
  • getAllSelectedOptions(); – Returns a list of all selected options in a dropdown.

We have selected the value using selectByVisibleText(“text”) in our code.
dropdown.selectByVisibleText(“Watches”);

Locating items in DropDown

Locating items in DropDown

4. Here we have located “Search” button with its id and clicking on it.

Driver.findElement(By.id(“gh-btn”)).click();

Clicking on Search button

Clicking on Search button

5. Here we have located the total listings for our product search with its “className” locator. Later fetching the text (text here contains the total listings count) using getText() method and taking its value in a String and then printing its value on the console.

String result = Driver.findElement(By.className(“listingscnt”)).getText();

System.out.println(“Result is : “+ result);

 

Locating the total listings for the product search

 This was all about working with Dropdown in Selenium WebDriver.

PS: For any questions, queries and feedback, feel free to write us at amruta@qatechhub.com or support@qatechhub.com. Happy Learning ?

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: