How to Select option from Dropdown using Selenium WebDriver

In this tutorial, we will see that how to select an option from Dropdown using Selenium WebDriver.
Selenium provides an inbuilt class called Select which has capability to select the drop down value.

First, you need to import Select class into your script to access it. There are 3 following functions in Select class which can be used to Select Dropdown. Lets look at them one by one.

import org.openqa.selenium.support.ui.Select;

SelectDropdownOptions

Sample HTML code for a drop down

<select id="month" name="test_month" aria-label="Month">
	<option selected="val-1" value="0">Month</option>
	<option value="val-2">Jan</option>
	<option value="val-3">Feb</option>
	<option value="val-4">Mar</option>
	<option value="val-5">Apr</option>
	<option value="val-6">May</option>
	<option value="val-7">Jun</option>
	<option value="val-8">Jul</option>
	<option value="val-9">Aug</option>
	<option value="val-10">Sep</option>
	<option value="val-11">Oct</option>
	<option value="val-12">Nov</option>
	<option value="val-13">Dec</option>
</select>

1. selectByValue()
If you look at the Dropdown HTML code mentioned above that for very option there is a value attribute. We can use that value attribute to select a particular option. Let say, if user wants to select a month “March” then as per above HTML code, value for that particular month is “val-3”. Hence, code to select March would be as follows:

WebElement element = driver.findElement(By.id("month")); // Finds the drop down element 
//Create an object of Select class and pass drop down element as a parameter
Select select = new Select(element); 
select.selectByValue("val-3"); // This command will select the month "March".

2. selectByIndex()
Here, we will use the index to select the option from a drop down. As Index starts from 0. Means for the first option, index would be 0 and for second option, it would be 1 and so on. If you want to select “May” then you need to pass “5” as a parameter.

select.selectByIndex(5); 

3. selectByVisibleText() :
You can also use the visible text to select the option. If you look at above HTML code, the visible text for Dropdown options are Jan, Feb,Mar,Apr etc. And if you want to select “April” month then the text visible for April month is “Apr”.

select.selectByVisibleText("Apr"); // This command will select April option from drop down

4. sendKeys()
Note : sendKeys() function is not provided by “Select” class. Generally, we use this method to enter data into input box. However, you can also use sendKeys() to select the options from drop down. However we would not recommend to use this function. Although, this method would work but not everytime. Mean to say that the chances of failures are high as compare to functions provided by Select class. In order to use this method, you just need to pass the text which is visible on drop down. Let say, if you want to select “May” then you need to pass “May” as a parameter.

 element.sendKeys("May"); 

Sample Script:

a) Launch http://automate-apps.com/demopage.html
b) Select Month “May” from the drop down using index.
c) Wait for few seconds.
d) Select Month “Feb” now using visible text.
e) Close browser

import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class DropDown {
    WebDriver driver;

    @Test
    public void testApp() throws InterruptedException {
        driver = new FirefoxDriver();
        driver.get("http://automate-apps.com/demopage.html");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        WebElement element = driver.findElement(By.id("month"));
        Select select = new Select(element);
        select.selectByIndex(5);
        Thread.sleep(3000);
        select.selectByVisibleText("Feb");
        Thread.sleep(3000);
        driver.quit();
    }

}

If you really like the information provided above, please don’t forget to hit a like on Facebook Page, you can also leave a comment.

Leave a Reply

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