为什么我的wait方法没有正确等待元素可见?
我已将等待变量设置为10秒
我的方法对你来说是否正确?
public boolean WaitUntilWebElementIsVisible(WebElement element) {
try {
Thread.sleep(2000);
this.wait.until(ExpectedConditions.visibilityOf(element)).isDisplayed();
System.out.println("WebElement is visible using locator: " + element.toString());
return true;
} catch (Exception e) {
System.out.println("WebElement is NOT visible, using locator: " + element.toString() + " ,Exception: " + e.getMessage());
Assert.fail("Method failed: WaitUntilWebElementIsVisible");
//Assert.fail("WebElement is NOT visible, using locator: " + element.toString());
return false;
}
}
答案 0 :(得分:0)
你的代码看起来不错。你可以考虑增加时间。 10秒非常小。尝试30秒或更长时间。
答案 1 :(得分:0)
在方法中执行以下操作:
boolean result = false;
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(timeOutInSeconds, TimeUnit.SECONDS)
.pollingEvery(pollingIntervalInSeconds, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class)
.ignoring(StaleElementReferenceException.class);
wait.until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply( WebDriver webDriver ) {
try {
return webDriver.findElements(locator).size() > 0 && webDriver.findElement(locator).isDisplayed();
} catch ( Exception e ) {
System.out.println("Got Exception for locator " +locator+ " while waiting for it to display. Trying again...");
return false;
}
}
});
result = true;