How to handle dropdown in selenium WebDriver?

 Two ways to handle dropdown –

         1.      Using Select class
The big secret to working with drop down is that you don’t want to work with them as WebElements, but instead create a Select element for them. The Select class (java and python documentation) includes utility methods that allow you to perform common tasks. We will be working with the following html:
<select id="id_state" class="form-control" name="id_state" style="">
<option value="">-</option>
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">Arizona</option>
<option value="4">Arkansas</option>
<option value="5">California</option>
<option value="6">Colorado</option>
<option value="7">Connecticut</option>
<option value="8">Delaware</option>
In order to deal with dropdown values through selenium WebDriver, Selenium has Select class. (;).
We can select value from dropdown, by below simple steps.
Firstly, Identify WebElement with Select tag, that is for your dropdown.
Ex –                  WebElement element = driver.findElement(” “));
Then, create an object of Select class.
Ex.                     Select s = new Select(listbox);
Then Select the value by one of the applicable methods like ByVisibleText, ByIndex ….whichever can be applied.
WebElement element = driver.findElement("mySelectID")); 
Select s= new Select(element);
WebElement element = driver.findElement("mySelectID")); 
Select s= new Select(element);
WebElement element = driver.findElement("mySelectID"));
Select s= new Select(element);
        2.       Without Using Select class
We can deal with dropdown value without using select class as well. Below are two examples how this can be achieved.

Method 1:

In this example, we find the option via a complex xpath, then click on it:

WebElement myoption = driver.findElement(By.xpath(


Method 2:

In this example, we find all the options, iterate over them, and click the one we want. This is useful if you have some more complex criteria.

WebElement mySelectElm = driver.findElement("mySelectID")) 
Select mySelect= new Select(mySelect);
List<WebElement> options = mySelect.getOptions();
for (WebElement option : options) {
    if (option.getText().equalsIgnoreCase("Option") {;



This is all about Selecting dropdown. However, note that, method 1 is more recommended, that is by using Select class.

Refer below post for complete example of Automation of Registration page using Selenium WebDriver.

Refer below video on same –

Hope This Helps !!!!!

Leave Comment

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

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

Get the Framework code at Github Repo: