我发现在C#
中,无论使用WebDriverWait类
还是DefaultWait类
,在任何一种情况下IgnoreExceptionTypes
code> 方法似乎不起作用。
即无论哪种情况,当针对我的页面运行时,都会抛出 StaleElementReferenceException
,尽管我指示代码忽略这些异常。
WebDriverWait
示例:
public void WaitElementToBeClickable(IWebElement element)
{
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(60));
wait.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(StaleElementReferenceException));
wait.Until(ExpectedConditions.ElementToBeClickable(element));
}
默认等待示例:
public IWebElement SafeWaitForDisplayed(IWebElement webElement) {
var w = new DefaultWait<IWebElement>(webElement);
w.Timeout = TimeSpan.FromSeconds(30);
w.IgnoreExceptionTypes(typeof(NoSuchElementException), typeof(StaleElementReferenceException));
return w.Until(ctx =>
{
var elem = webElement;
if (elem.Displayed)
return elem;
else
return null;
});
}
非常感谢您提出的任何建议。网络上似乎很少有关于此特定方法的使用的信息,其他人发现它也不起作用,并且没有建议任何解决方法。
最佳答案
IgnoreExceptionTypes
只会在整个等待过程中持续,直到超时。我正在使用 DefaultWait
并且就像您期望它返回 null 一样。它不是。当达到超时时,它将抛出异常。因此,我将其包含在 try catch 中,以便在超时时适本地处理异常。
关于c# - IgnoreExceptionTypes 不起作用(C# Webdriver),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31721303/