Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium C#DefaultWait IgnoreExceptionTypes不工作_C#_Selenium - Fatal编程技术网

Selenium C#DefaultWait IgnoreExceptionTypes不工作

Selenium C#DefaultWait IgnoreExceptionTypes不工作,c#,selenium,C#,Selenium,我在等待WebElement可单击时使用DefaultWait。虽然TargetInvocationException是我等待期间要忽略的异常列表中的一个异常,但在达到超时时间之前,仍有测试因该异常而失败。这不是我所期望的 public static void WaitAndClick(this IWebDriver driver, IWebElement webelement) { DefaultWait<IWebDriver> fluentWait =

我在等待WebElement可单击时使用DefaultWait。虽然TargetInvocationException是我等待期间要忽略的异常列表中的一个异常,但在达到超时时间之前,仍有测试因该异常而失败。这不是我所期望的

public static void WaitAndClick(this IWebDriver driver, IWebElement webelement)
    {

        DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(driver)
        {
            Timeout = TimeSpan.FromSeconds(Configuration.WaitTime.TotalSeconds),
            PollingInterval = TimeSpan.FromMilliseconds(500)
        };
        fluentWait.IgnoreExceptionTypes(typeof(TargetInvocationException),typeof(NoSuchElementException),typeof(InvalidOperationException));
        fluentWait.Until(ExpectedConditions.ElementToBeClickable(webelement));
            webelement.Click();

    }
publicstaticvoidwaitandclick(此IWebDriver驱动程序,IWebElement webelement)
{
DefaultWait fluentWait=新的DefaultWait(驱动程序)
{
超时=TimeSpan.FromSeconds(Configuration.WaitTime.TotalSeconds),
PollingInterval=TimeSpan.From毫秒(500)
};
fluentWait.IgnoreExceptionTypes(typeof(targetingException)、typeof(NoSuchElementException)、typeof(InvalidOperationException));
fluentWait.till(ExpectedConditions.elementtobelickable(webelement));
webelement.Click();
}

尝试使用
WebDriverWait
而不是
DefaultWait
,这基本上是一样的

public static void WaitAndClick(this IWebDriver driver, IWebElement webelement)
{
    WebDriverWait fluentWait = new WebDriverWait(driver,Configuration.WaitTime.TotalSeconds);
    //No need to set PollingInterval, default is already 500ms
    fluentWait.IgnoreExceptionTypes(typeof(TargetInvocationException),typeof(NoSuchElementException),typeof(InvalidOperationException));
    fluentWait.Until(ExpectedConditions.ElementToBeClickable(webelement));

    webelement.Click();
}
我认为当存在预定义的具体类时,没有必要使用接口,原因完全相同(与webDriver一起等待)。如果问题仍然存在,请报告

更新:如果无法解决问题,请使用lamba表达式删除Until()所需的函数(
public TResult Until(Func条件);


多亏了Thodoris Koskinopoulos,这个解决方案对我来说运行良好:

    public static void WaitAndClick(this IWebDriver driver, IWebElement webelement)
    {
        var fluentWait = new WebDriverWait(driver, Configuration.WaitTime);
        fluentWait.Until(webDriver =>
        {
            try
            {
                webelement.Click();
            }
            catch (Exception ex)
            {
                if (ex is TargetInvocationException ||
                    ex is NoSuchElementException ||
                    ex is InvalidOperationException)
                {
                    return false; //By returning false, wait will still rerun the func.
                }
                {
                    throw; //Rethrow exception if it's not ignore type.
                }
            }
            return true;
        });
    }   

正如您可能已经预料到的那样,使用具体的类WebDriverWait而不是接口DefaultWait没有任何区别。我的一些测试仍然失败,因为在此更改后出现了InvalidOperationException。另一方面,更新中给出的使用lambda表达式的解决方案运行良好(经过一些小的修改,使其在我原始方法的上下文中语法正确)。@Frank很高兴我能提供帮助!
    public static void WaitAndClick(this IWebDriver driver, IWebElement webelement)
    {
        var fluentWait = new WebDriverWait(driver, Configuration.WaitTime);
        fluentWait.Until(webDriver =>
        {
            try
            {
                webelement.Click();
            }
            catch (Exception ex)
            {
                if (ex is TargetInvocationException ||
                    ex is NoSuchElementException ||
                    ex is InvalidOperationException)
                {
                    return false; //By returning false, wait will still rerun the func.
                }
                {
                    throw; //Rethrow exception if it's not ignore type.
                }
            }
            return true;
        });
    }