c# - IgnoreExceptionTypes 不起作用(C# Webdriver)

标签 c# selenium selenium-webdriver webdriver

我发现在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/

相关文章:

google-chrome - Chrome 用 selenium 打开 "Data;"

javascript - 如何使用 selenium 获取所有元素的直接子元素和 css 选择器?

c# - 从列表中返回单个对象

c# - 双重 dispatch 和备选方案

python - 如何在python中进行网络抓取

java - 如何扩展具有参数化@BeforeMethod的类

c# - 如何使用 C# 在运行时在 MS Access 中创建存储查询

c# - 在 C# 中将对象转换为字节数组,通过套接字发送它,然后转换回对象

java - 是否在 Selenium 中查找元素检查标签是否正确关闭?

java - 使用反射时出现空指针异常