How to Select value from Drop down list in Selenium WebDriver
Selenium Tutorial for Beginners

How to Select value from Drop down list in Selenium WebDriver

Continuing on our Selenium Beginner’s series, let’s see How to Select value from drop down list in Selenium WebDriver using Java. Hope you have already gone through the previous tutorials…

#4. Common Page elements & User actions to automate
#5. How to enter value in ‘Textbox’ using Selenium WebDriver
#6. How to click a Button in Selenium WebDriver using Java
#7. How to Mouse Hover an element in Selenium WebDriver

Here we will try to select a drop-down value from the Birthday drop-down on Facebook.com login page.

Select value from drop down list in Selenium WebDriver

Inspect the Element first

This can be done by using browser tools. In simple terms just right click on the element (‘Birthday’ dropdown) & select ‘Inspect Element’. Note down the ‘attribute-value’ pairs, in this case id, name & title. Please note that the dropdown element is present as the <select> tag. Selenium WebDriver provides a dedicated ‘Select’ class to perform multiple actions on these elements.

Inspect Dropdown Selenium WebDriver

<< THE CODE >>

The steps followed are,

  1. Open a browser
  2. Open the website URL & maximize the window
  3. Find the ‘Birthday’ drop-down
  4. Select the values from the Day, Month & Year drop-downs

 

Open Browser & Application

The code till,

is to open the browser and application. For reference, please visit our earlier posts in the series.

Identify the Dropdown | The Select element

In order to select value from drop down list in Selenium WebDriver you have to create a Select element instead of the default WebElement. An element can be identified using different properties. Here we have identified these three dropdowns using the id, name and xpath (using ‘title’ attribute) properties. For details on different element locators, click here…

After identifying the dropdowns, we are saving it in the ‘Day’, ‘Month’ and ‘Year’ instances of type ‘Select’ which is an in-built class available in Selenium. We need to import this class in order to create objects,

The SELECT Class

The Select class in Selenium WebDriver Models a SELECT tag, providing helper methods to select and deselect options. When creating an instance of Select class, a check is made that the given element is, indeed, a SELECT tag. If it is not, then a UnexpectedTagNameException is thrown. The Select class provides different methods to operate selectable elements,

  • deselectAll() – Clear all selected entries.
  • deselectByIndex(int index) – Deselect the option at the given index.
  • deselectByValue(java.lang.String value) – Deselect all options that have a value matching the argument.
  • deselectByVisibleText(java.lang.String text) – Deselect all options that display text matching the argument.
  • getAllSelectedOptions()
  • getFirstSelectedOption()
  • getOptions()
  • isMultiple()
  • selectByIndex(int index) – Select the option at the given index.
  • selectByValue(java.lang.String value) – Select all options that have a value matching the argument.
  • selectByVisibleText(java.lang.String text)

Select value from drop down list in Selenium WebDriver

Inspect Dropdown Options Selenium WebDriver

Selenium WebDriver provides different ways to select value from the dropdown,

  • Select by Index (select the option at the given index. This is done by examining the “index” attribute of an element, and not merely by counting)
  • Select by Visible Text (select all options that display text matching the argument)
  • Select by Value (select all options that have a value matching the argument)

The alternatives

The commented code above shows some alternative ways to select value from drop down list in Selenium WebDriver. You can even use the default sendKeys method or can concatenate instance creation + element identification + selecting value in a single statement.

The Result

Select value from drop down list in Selenium WebDriver | Once you run this program it will open the browser >> open the Facebook URL >> Select the Birthday ’27-Dec-1987’ from the dropdown element.

Save

Save

Save

Save

Save

Save

Save

Save

Save

Save

Leave a Reply

Your email address will not be published. Required fields are marked *