Canarys | IT Services

Blogs

Handling dropdown’s using Selenium Webdriver

Date:
Author:
Share

Dropdown’s can be handled in Selenium webdriver using Select Class which has some predefined methods like selectByIndex(index), selectByValue(value), selectByVisibleText(text), getFirstSelectedOption(),getOptions()

Select class can be found under org.openqa.selenium.support.ui.Select package

Consider dropdown (Fig a) from a sample webpage.

If we inspect the above dropdown refer fig (a) its html code will be as shown below

In order to use the any of the Select Class methods, first we have to identify the dropdown field. In the above sample webpage I have identified the dropdown using id as the locator as shown below.

WebElement categories= driver.findElement(By.id(“instant-search-categories”));

Once dropdown field has been identified, create an object for Select class of selenium as shown below:

Select categoriesList = new Select (categories);

NOTE: Here Select is the Class name and categoriesList is the object of Select Class

Now, let us see how to handle dropdown list using Select Class methods

  1. selectByIndex(index): – selectByIndex method accepts index as an argument. Based on the index value specified it will select the value from the dropdown.

NOTE: Index value will always starts from 0, hence if you have specified index as 2, selectByIndex method will select the 3rd elements from the dropdown

Code to select dropdown option based on Index  :

WebElement categories= driver.findElement(By.id(“instant-search-categories”));

Select categoriesList= new Select(categories);

categoriesList.selectByIndex(3);

Output: “Blocks” option will be selected. 

2. selectByValue(value):- selectByValue method accepts String as an argument. selectByValue method selects the options from the dropdown based on the String value specified in the arguments.

Code to select dropdown option based on value  :

WebElement categories = driver.findElement(By.id(“instant-search-categories”));

Select categoriesList = new Select(categories);

categoriesList.selectByValue(“6”);

Output: “Tiles” option will be selected. 

3. selectByVisibleText(text):- selectByVisibleText method accepts String as an argument. selectByVisibleText method selects the options from the dropdown based on the text value specified in the arguments.

NOTE:  This is case sensitive it means if I pass “Fittings” and dropdown has “fittings” then Selenium will not be able to select value and it will fail your program so make sure Text which you are passing is correct

Code to select dropdown option based on value  :

WebElement categories = driver.findElement(By.id(“instant-search-categories”));

Select categoriesList = new Select(categories);

categoriesList.selectByVisibleText(“Fittings”);

Output: “Fittings” option will be selected.

4. getFirstSelectedOption():- getFirstSelectedOption menthod will return the first selected option in the dropdown. It returns a WebElement.

Code to get the first selected option from Dropdown  :

WebElement categories = driver.findElement(By.id(“instant-search-categories”));

WebElement fstItem = categoriesList.getFirstSelectedOption();

System.out.println(“First item selected in the list: ” +fstItem.getText());

Output: First option selected option in the dropdown will be printed on the console.

5. getOptions():- getOption method will return all the options in the dropdown.

Code to display all the option present in the Dropdown  : 

WebElement categories = driver.findElement(By.id(“instant-search-categories”));

List getAllcat = categoriesList.getOptions();

int categoriescount = getAllcat.size();

for (int i=0;i<categoriescount ;i++)<categoriescount;i++)< p=””></categoriescount;i++)<>

{

System.out.println(getAllcat.get(i).getText());

}

Output: All the options present in the dropdown will be printed on the console.

Leave a Reply

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

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.