Selenium WebDriver: Handling DropDowns [duplicate]

℡╲_俬逩灬. 提交于 2021-02-11 13:14:48

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!