【问题标题】:Try Catch WebDriverTimeoutException does not work尝试 Catch WebDriverTimeoutException 不起作用
【发布时间】:2016-12-24 01:42:36
【问题描述】:

我的 try catch 块中有 2 个捕获,但是 WebDriverTimeoutException 根本没有被捕获。另一个异常被正确捕获。测试因超时异常“OpenQA.Selenium.WebDriverTimeoutException :Timed out after 20 seconds”而失败

那么为什么 WebDriverTimeoutException try catch 根本没有被捕获?

    public IWebElement FindElement(By howBy)
    {

        TimeSpan _elementTimeOut = TimeSpan.FromSeconds(20);


        IWebElement elementfound = null;

        WebDriverWait wait = new WebDriverWait(WebDriver, _elementTimeOut);
        wait.Until<IWebElement>(d =>
        {
            try
            {
                elementfound = WebDriver.FindElement(howBy);
            }
            catch (WebDriverTimeoutException f)
            {
                Console.WriteLine("Please fail WebDriverTimeoutException");
            }
            catch (NoSuchElementException e)
            {
                Console.WriteLine("Please fail NoSuchElementException");
            }

            return elementfound;
        });

        return elementfound;
  }

【问题讨论】:

    标签: c# selenium-webdriver


    【解决方案1】:

    WebDriverTimeoutException 未在 Until() 块内的匿名方法中捕获的原因是超时异常不是由您的匿名方法抛出。您需要在Until() 方法外部捕获超时。也就是说:

    public IWebElement FindElement(IWebDriver driver, By howBy)
    {
        TimeSpan elementTimeOut = TimeSpan.FromSeconds(20);
        IWebElement elementfound = null;
    
        try
        {
            WebDriverWait wait = new WebDriverWait(driver, elementTimeOut);
            elementFound = wait.Until<IWebElement>(d =>
            {
                try
                {
                    elementfound = driver.FindElement(howBy);
                }
                catch (NoSuchElementException e)
                {
                    Console.WriteLine("Please fail NoSuchElementException");
                }
    
                return elementfound;
            });
        }
        catch (WebDriverTimeoutException)
        {
            Console.WriteLine("Please fail WebDriverTimeoutException");
        }
    
        return elementfound;
    }
    

    请进一步注意,WebDriverWait 已经将NoSuchElementException 作为其正常操作的一部分,因此您在示例中重新发明了轮子。执行相同操作的更紧凑和更有效的方法如下所示:

    public IWebElement FindElement(IWebDriver driver, By howBy)
    {
        TimeSpan elementTimeOut = TimeSpan.FromSeconds(20);
        IWebElement elementfound = null;
    
        try
        {
            WebDriverWait wait = new WebDriverWait(driver, elementTimeOut);
            elementFound = wait.Until<IWebElement>(d => driver.FindElement(howBy));
        }
        catch (WebDriverTimeoutException)
        {
            Console.WriteLine("Please fail WebDriverTimeoutException");
        } 
    
        return elementfound;
    }
    

    【讨论】:

    • 非常感谢 JimEvans,您的建议确实奏效了。现在我的方法看起来更小,效率更高。
    猜你喜欢
    • 2013-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 2021-08-04
    相关资源
    最近更新 更多