Thursday, October 31, 2013

Handle Drop down values (Locate, Print count and print values)

Drop downs are also another famous input type where you almost in every website. Drop downs are also treated as Lists in java. same as the radio button post we can extract the values in the drop down to a list and do out operations . Below code will illustrate how we can perform that 

WebDriver A = new FirefoxDriver();  
A.get("http://www.gic.gov.lk/gic/index.php?option=com_findnearest&task=train");
WebElement From = A.findElement(By.id("startStation"));

List<WebElement> Options  = From.findElements(By.tagName("option")); 
System.out.println(Options.size());

for (int i = 0; i < Options.size(); i++) {
System.out.println(Options.get(i).getText() );

}

You can use this same example in any of your scenarios, Just change the link and the locator accordingly.

if we want to pass any value to the drop down to be selected then we can use the value given in the tag itself.

A.findElement(By.xpath("//*[@id='startStation']")).sendKeys("AGT");






1 comment:

  1. When i execute the same code then below error occured in eclipse, why?

    the type list is not generic it cannot be parameterized with arguments

    ReplyDelete