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. (org.openqa.selenium.support.ui.Select;).
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(By.id(” “));
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.
    selectByIndex
WebElement element = driver.findElement(By.id("mySelectID")); 
Select s= new Select(element);
s.selectByIndex(0);
    selectByValue
WebElement element = driver.findElement(By.id("mySelectID")); 
Select s= new Select(element);
s.selectByValue("Value");
     selectByVisibleText
WebElement element = driver.findElement(By.id("mySelectID"));
Select s= new Select(element);
s.selectByVisibleText("Option");
           
        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(
    "//Select[@id='mySelectID']/option[normalize-space(text())='Option']")

);
 myOption.click();

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(By.id("mySelectID")) 
Select mySelect= new Select(mySelect);
List<WebElement> options = mySelect.getOptions();
for (WebElement option : options) {
    if (option.getText().equalsIgnoreCase("Option") {

        option.click();

    }

}

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 -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories