How to select option from Drop Down List
How Selenium supports DropDown List?
How Selenium supports DropDown List?
- Selenium webdriver provides a class called Select from package "org.openqa.selenium.support.ui.Select
- Interestingly Select Class has 3 methods by which dropdown list values can be selected they are
- selectByIndex():Selection will be made on the indexing of the list.First member of list will have index as zero and so on it will be incremented for the next members.
- selectByValue():Selection will be made on the basis on numbering of the list
- selectByVisibleText(): Selection will be made of the text visible on the drop down list
Drop down Menu |
The following code will select Date/Month/Year on the facebook login page by using the methods described above
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Dropdown {
public static void main(String[] args) throws IOException, InterruptedException {
WebDriver a= new FirefoxDriver();
a.manage().window().maximize();
a.get("http://www.facebook.com/");
Select index= new Select(a.findElement(By.xpath(".//*[@id='day']")));
index.selectByIndex(1);
Select month= new Select(a.findElement(By.xpath(".//*[@id='month']")));
month.selectByVisibleText("Jan");
Select year= new Select(a.findElement(By.xpath(".//*[@id='year']")));
year.selectByValue("1989");
a.quit();
}
}
'
Notes
1- As Select is a class 3 different types of objects has been created to cater different type of dropdown list for day/month/year
2- Values on the drop down are accessed via Index/Visible text/Value
Output
0 comments:
Post a Comment