我正在使用 jBehave/Selenium 进行自动化测试。
现在我使用以下代码来获取页面上的可见元素;
public WebElement getVisibleElement( final By by, final WebElement parentElement, int timeoutValue, TimeUnit timeoutPeriod, int pollingInterval, TimeUnit pollingPeriod ) {
return fluentWait(timeoutValue, timeoutPeriod, pollingInterval, pollingPeriod).until( new Function<WebDriver, WebElement>(){
public WebElement apply(WebDriver driver) {
try{
WebElement element = parentElement.findElement(by);
if ( element.isDisplayed() ) {
return element;
} else {
return null;
}
} catch( NoSuchElementException e ) {}
return null;
}
} );
}
现在的问题是,如果页面上不存在该元素,Selenium 会花费大量时间尝试在页面上找到它。有什么方法可以优化代码,以便在这种情况下不会花费很长时间?