3 回答
![?](http://img1.sycdn.imooc.com/545869470001a00302200220-100-100.jpg)
TA贡献1827条经验 获得超8个赞
无法弄清楚为什么我不能将这些相同类型的元素添加到我的列表中 -
您的代码中很可能有拼写错误。它应该是element而不是elements:
for (WebElement element : elements){
webElementList.add(element);
}
在“visibleWebElement”上出现此错误 -
driver.findElement()方法接受一个By对象作为参数,它是一个选择器。但是您正在传递visibleWebElementswhich 是一个WebElement对象,这就是您的代码出错的原因。例如,如果您想通过 xpath 定位元素,这是使用它的正确方法:
WebElement your_element = driver.findElement(By.xpath("//whatever xpath"));
您可以直接isDisplayed()在 WebElement 上使用该方法:
public void estaVisible(WebElement visibleWebElements) {
boolean esvisible = visibleWebElements.isDisplayed();
Assert.assertTrue(esvisible);
return esvisible;
}
![?](http://img1.sycdn.imooc.com/545847f50001126402200220-100-100.jpg)
TA贡献1812条经验 获得超5个赞
此函数将返回一个布尔值并检查列表元素是否可见。
public boolean isListElementsVisible(WebDriver driver, By locator) {
boolean result = false;
try {
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(locator));
result = true;
} catch (Exception e) {
System.out.println(e.getMessage());
result = false;
}
return result;
}
添加回答
举报