How to select an option from a drop down using selenium Web Driver

Web Driver has inbuilt class called “Select” which provides multiple options to select the drop down value. We will go through with every possible option one by one.

Sample HTML code for a drop down

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

Method 1

1. selectByValue():

If you look at the drop down html code mentioned above, for very option there is a value attribute given by the web developer. We can use that value attribute to select a particular option. Let say if user wants to select a month “March” then the value for that particular month is “3”.

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("3"); // This command will select the month "March".

Method 2

2. selectByIndex() :

Here we will use the index to select the option from a drop down. Basically Index starts from 0. Means for the first option, index will be 1 and so on. If user wants to select “May” then he will have to pass “5” as a parameter.

select.selectByIndex(5); 

Method 3

3. selectByVisibleText() :

User can also the text to select the option. Let say if user wants to select “April”. Now look at the html code above, for “April” month, the visible text is “Apr”.

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

Method 4

Note : SendKeys() function is not provided by “Select” class. Generally, we use this method to enter data into input box.

4. sendKeys() :

User can also use sendKeys() to select the options from drop down however I would not recommend to use this function. Although this method will work but not every time. I mean to say the chances of failures are high as compare to above methods. In order to use this method, you just need to pass the text which is visible on drop down. Here I passed “May” as a parameter.

 element.sendKeys("May"); 

=========================================================================================================

Example :

a) Launch http://automate-apps.com/how-to-select-an-option-from-a-drop-down-using-selenium-web-driver/
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/how-to-select-an-option-from-a-drop-down-using-selenium-web-driver/");
        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 leave the comment. You can also like us on Facebook.

Leave a Reply

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