Selenium Webdriver script to get all options from dropdown list


package selenium_examples;

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;

import java.util.concurrent.TimeUnit;
import java.util.List;

public class example1 {

public static void main(String[] args)
   {

WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com");
driver.findElement(By.name("userName")).sendKeys("mercury");
driver.findElement(By.name("password")).sendKeys("mercury");
driver.findElement(By.name("login")).click();
driver.manage().timeouts().implicitlyWait(100,TimeUnit.SECONDS);
   Select droplist = new Select(driver.findElement(By.name("fromPort")));

        droplist.selectByVisibleText("London");


WebElement we = driver.findElement(By.name("fromPort"));

List options =  we.findElements(By.tagName("option"));
for (WebElement option : options) {
System.out.println(option.getText());
}


}

}
   
Output will be :-

Acapulco
Frankfurt
London
New York
Paris
Portland
San Francisco
Seattle
Sydney
Zurich

Above script logins into Mercury tours website and selects value "London" from Departing From dropdown list and it displays all the options of dropdown list.


No comments:

Related Posts Plugin for WordPress, Blogger...