Its very easy to select an option from the list.We can select an option by the following ways.
- Select by Index.
- Select by Value.
- 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