How to select a value from drop down list in Selenium Web Driver using Java


Its very easy to select an option from the list.We can select an option by the following ways.


  1. Select by Index.
  2. Select by Value.
  3. Select by Visible Text.

Below is the program with commented description.




package com.selenium_basics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class SelectFromList {
 // for more visit http://learn-selenium-easy.blogspot.com
 
  @Test 
  public void SelectValue() throws InterruptedException{
  
   WebDriver driver=new FirefoxDriver();
   driver.get("http://learn-selenium-easy.blogspot.in/2016/02/how-to-select-value-from-drop-down-list-in-selenium-web-driver-using-java.html");
   Thread.sleep(1000);
   
   Select post_title=new Select(driver.findElement(By.xpath("//*[@id='month']")));
   
   post_title.selectByIndex(10);//Select the first option in the list by index.
   
   
   Thread.sleep(1000);
   
   post_title.selectByValue("01");//Select option from the list by the value.
   
   Thread.sleep(1000);   
   
   
   post_title.selectByVisibleText("June");//select option by visible text on the list.
   
   Thread.sleep(1000);
   
   //Thread .Sleep is used only for demonstration purposes.
  
  
   
   driver.close();
   driver.quit();
  }

}


Demo Area