我们有一个类似的问题。我们所做的是创建了一个自定义的 waitForWebElement 方法来轮询站点,你可以做类似的事情
public WebElement waitForWebElement(final By by, final boolean isDisplayed) {
// Waiting 30 seconds for an element to be present on the page, checking
// for its presence once every 5 seconds.
Wait<SearchContext> wait = new FluentWait<SearchContext>(getSearchElement()).withTimeout(timeout, TimeUnit.SECONDS).pollingEvery(pollSeconds, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
WebElement webElement = wait.until(new Function<SearchContext, WebElement>() {
public WebElement apply(SearchContext search) {
try{
WebElement element = search.findElement(by);
catch(NoSuchElementException e){
if(CHECK_YOUR_ERROR_PRESENT){
throw CriticalPageException()
}else{
throw e;
}
}
return element;
}
});
return webElement;
}
编辑: getSearchElement() 只返回您的搜索上下文