问题
I want to get values(String) listed in a dropdown, and compare those values with a predefined list of values(String). I'm trying to implement this in Selenium WebDriver using JAVA as scripting language. Can anyone please guide me through?
Situation: Suppose in a webpage there is a dropdown listing country names, I want to read those country names from dropdown, and verify each of them is present in an existing list of country names.
回答1:
Below is the code you can use to print the values of a drop-down. You can modify the same to compare with your input list.
WebElement dropdown = driver.findElement(By.id("provide id of the dropdown"));
List<WebElement> dropdown_values=dropdown.findElements(By.tagName("option"));
Iterator<WebElement> it=dropdown_values.iterator();
while(it.hasNext())
{
System.out.println(it.next().getText());
}
来源:https://stackoverflow.com/questions/38130974/selenium-webdriver-handling-dropdowns