Working with Dropdown in Selenium WebDriver

September 1, 2016

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:

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

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 🙂

Amrita Joshi

About the Author

Amrita Joshi

Leave a Comment: