How to Select Value from a DropDown in Selenium WebDriver

Unlike other web elements, we can’t just locate the dropdown object and select a value from it in Selenium WebDriver.In this post, I will show you how to select value from dropdown in Selenium WebDriver and perform additional validations to avoid a Selenium WebDriver runtime exceptions.

First and foremost you will have to understand the prerequisites of handling drop-downs in Selenium WebDriver.

Select Class in Selenium WebDriver to Select value from WebList

The ‘Select‘ Class in Selenium WebDriver is used to select and deselect a value in a dropdown. We have to pass the dropdown WebElement object as a parameter to the Select class constructor to initialize its object.
Syntax:

WebElement DropDownElement = driver.findElement(By.id(“Dropdown1″));
Select dropdownSelectObject = new Select(DropDownElement );

We will consider the following DOM structure for the examples. This is the Facebook Signup popup page.

Automating DropDown in Selenium WebDriver

Select value from the Dropdown in Selenium WebDriver?

There are three possible ways to select an item from the drop-down menu.

  • selectByIndex 
  • selectByValue
  • selectByVisibleText

How to select a value from a Dropdown By Using its Index In Selenium?

The selectByIndex() method is used to select an item based on its index. The index of the first item starts from 0.

A complete example of all select methods has been given at the end of this article.

How to select a value from a Dropdown By Using its Value attribute In Selenium?

The selectByValue() method is used to select an item by using its ‘value‘ attribute in the Option Tag.

Note: It is not necessary that the dropdown item has a value attrubute.If the value attribute is not available this method won’t work.

How to select a value from a dropdown By Using its Visible Text Value In Selenium?

The selectByVisibleText() method is used to select an item from a dropdown based on the items or text values present in the dropdown.

How to Get Selected Value from a Dropdown In Selenium?

Sometimes we have to verify whether we have selected the correct value from the dropdown. To validate the selected value we can get the selected option from the dropdown and compare it with the expected value. You can use the following Selenium Code to get the selected value from the dropdown.

How to Check whether a DropDown is Visible and Enabled on the Page?

Whenever you are going to perform any action on a web element it is standard practice to first check whether it is visible on the page. If it is visible the next thing we should check whether it is enabled or not. The following example illustrates how to do it. Here we will first check whether the required dropdown is visible on the page using isDisplayed() method. If it is visible we will check whether it is in the enabled state using the isEnabled() method. If it is enabled we will select a value from it.

How to Check whether a value exists in a DropDown in Seleneium?

What would happen if you try to select a value from the dropdown but that value does not exist in the dropdown. There could be multiple reasons like you have provided an incorrect value, the values in the dropdown, and that particular value is not loaded in the dropdown. In all such situations, Selenium will throw a runtime exception.

Exception in thread “main” org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:

So, it would be a better approach to first check whether the required value exists in the dropdown that you want to select to avoid runtime exceptions.

Complete Code Showing all Examples around a Dropdown in Selenium WebDriver

Let’s consider the DOM Structure of the Facebook Signup Page.Select a Value from a DropDown In Selenium WebDriver-1Test Scenario:

  • Launch a new chrome browser
  • Open Facebook.com
  • Click on the Create New Account Button
  • Switch to the Sign Up Pop-up window
  • Select Jun month on the Sign-Up popup window
  • Verify Jun month has been selected in the dropdown

Conclusion

  • We can select a value from a drop-down by initializing the object of the Select class
  • We can locate the drop-down elements by id, name, CSS, and XPath selector.
  • We can perform validation operations like isDisplayed() and isEnabled() before selecting any value from dropdown to get rid of runtime exceptions thrown by Selenium WebDriver.

Recommended Posts

Leave a Reply