首页 > 解决方案 > 带有 Java 的 Selenium Webdriver(Angular 应用程序)

问题描述

我编写了下面的代码,用于从下拉列表中选择提到的值。
在这里,我只想@data-option-array-index=1,3,5,9,28,34从下拉列表中选择这些。
我想使用一种数组或循环,它只为这些选定的值运行。但我做不到

element=driver.findElement(By.xpath("//li[@class='search-field']"));
element.click();
element=driver.findElement(By.xpath("//li[@data-option-array-index='1']"));
element.click();
String text=driver.findElement(By.xpath("//li[@class='search-choice']")).getText();
System.out.println("Element text is: "+ text);

标签: javaseleniumselenium-webdriver

解决方案


使用 findElements,

List<WebElement> choises=driver.findElements(By.xpath("//li[@class='search-choice']"));
List<String> listValues= new Arraylist<String>();
for(WebElement choise : choises){
   listValues.add(choise.getText());
}

推荐阅读