[英]no such element: Unable to locate element: when I use elementToBeClickable method
我必须等待某个元素出现,然后单击它。 这是我尝试并获得NoSuchElementException的代码。
我有300秒等待元素,但它正在尝试查找元素: tpo.fwOptimizationTestResults()
而不等待300秒
WebElement fwResults = (new WebDriverWait(driver, 300))
.until(ExpectedConditions.elementToBeClickable(tpo.fwOptimizationTestResults()));
public WebElement fwOptimizationTestResults() {
//return driver.findElement(By.xpath("//*[@class='table table-condensed table-bordered']"));
return driver.findElement(By.xpath("//table[contains(@class, 'table-condensed')]"));
}
错误:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//table[contains(@class, 'table-condensed')]"}
(Session info: chrome=75.0.3770.80)
异常不是来自elementToBeClickable
,而是来自fwOptimizationTestResults
。 您正在使用driver.findElement()
抛出异常并在预期条件之前进行评估。
有两个重载, elementToBeClickable(WebElement)
和elementToBeClickable(By)
,您应该使用第二个重载
WebElement fwResults = (new WebDriverWait(driver, 300)).until(ExpectedConditions.elementToBeClickable(By.xpath("//table[contains(@class, 'table-condensed')]")));
您可能想要修改代码,以将ignoring
节添加到WebDriverWait中,这样它就不会在NPE上失败:
WebElement fwResults = (new WebDriverWait(driver, 5))
.ignoring(NullPointerException.class)
.until(ExpectedConditions.elementToBeClickable(tpo.fwOptimizationTestResults()));
然后将您的WebElement函数放入try块中 ,而不是引发异常-如果未找到element,则返回null
:
public WebElement fwOptimizationTestResults() {
try {
return driver.findElement(By.xpath("//table[contains(@class, 'table-condensed')]"));
} catch (NoSuchElementException ex) {
return null;
}
}
更多信息:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.