WebDriverWait不会忽略异常

hhc*_*cib 7 java selenium-webdriver

我正在使用最新的Chrome和Webdriver 2.33并且遇到了一些问题IgnoreExceptionTypes.在下面的代码中,webdriver会像我期望的那样等待,但它实际上不会忽略异常:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(8));
wait.IgnoreExceptionTypes(
    typeof(WebDriverTimeoutException),
    typeof(NoSuchElementException)
);  
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(firstResultX)));
Run Code Online (Sandbox Code Playgroud)

代码在try/catch中,我尝试将它移到try/catch之外并收到相同的问题.我不知道从哪里开始,任何帮助将不胜感激.

Mru*_*sar 1

您可以使用 FluentWaits。

Wait<WebDriver> wait = new FluentWait<WebDriver>(getDriverInstance())
                .withTimeout(timeoutSeconds, TimeUnit.SECONDS)
                .pollingEvery(sleepMilliSeconds, TimeUnit.SECONDS)
                .ignoring(NoSuchElementException.class);

wait.until(<Your expected condition clause.>);
Run Code Online (Sandbox Code Playgroud)

如果这不能解决您的问题,请告诉我。