我正在使用最新的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)));
代码在try / catch中,我尝试将它移到try / catch之外并收到同样的问题。我不知道从哪里开始,任何帮助都将不胜感激。
答案 0 :(得分: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.>);
如果这不能解决您的问题,请告诉我。
答案 1 :(得分:0)
对于 C#,不同的等待是 -
` //Implicit Wait - Once set it remains till the life of the session
Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
//Explicit Wait - Polling interval is 250ms
//using OpenQA.Selenium.Support.UI;
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(30));
//this wait utility ignores no such element errors by default
IWebElement webElement = wait.Until(e => e.FindElement(By.Id("value")));
//Fluent wait - Polling interval is set by us
WebDriverWait fluentWait = new WebDriverWait(Driver, TimeSpan.FromSeconds(30))
{
PollingInterval = TimeSpan.FromSeconds(2)
};
fluentWait.IgnoreExceptionTypes(typeof(AccessViolationException), typeof(NoSuchElementException));
IWebElement element = wait.Until(e => e.FindElement(By.Id("value")));`