How to find drop down web element on the page using selenium locators

Posted in Selenium By Raj On October 24, 2017

In this article, I am going to explain to you, how to find out drop down element on the page using selenium locators, the first thing which I am going to do in my selenium test script is importing below package

Finding Element

Using id attribute, I am going to find out the drop down element on the page, here I am using annotations to find out the element but, you can use driver.findElement(By.name(“country”)), I am using name locator here to find out country

The HTML  for drop-down list is different than the normal HTML controls and it looks something like below, here every option you see in the below HTML contains a value attribute and option text which is in between options tags 

Selenium dropdown selection

Using select class which we imported earlier will be used to select the options tags first

If you want to find out by text then you need to use below code

If you want to find out by value then you need to use below code

If you want to find out by index then use below

Selenium dropdown deselection

If you want to deselect the text which was already selected then you need to use below code

If you want to deselect by value then you need to use below code

If you want to deselect by index then use below

Selenium dropdown multiselection

You can use combination to select multiple items in the dropdown list, check below code, it will select India and china items in the dropdown list

Dropdown selection example

I am going to test facebook registration date of birth drop-down list by using below code

After executing above selenium drop-down selection code, the date of birth on the registration page of the facebook will be populated something like below

selenium-dropdown-selection