try / catch语句不是从Selenium2 FindElement处理异常是从、语句、异常、catch

2023-09-08 08:34:17 作者:星空下的忧伤

我有包装在一些辅助方法的一些Selenium2 API调用玩耍,但预期的异常没有被处理。这里的code:

I'm playing around with wrapping some Selenium2 API calls in some helper methods, but expected exceptions aren't being handled even though I am catching them. Here's the code:

public static bool IsElementPresent(this IWebDriver driver, By by)
{
    var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 5));
    wait.IgnoreExceptionTypes(new Type[] { typeof(WebDriverException) });
    try
    {
        wait.Until(drvr => drvr.FindElement(by));
        return true;
    }
    catch (NoSuchElementException)
    {
        return false;
    }
    catch (System.TimeoutException)
    {
        return false;
    }
}

在某些情况下,我测试针对的元素,我期望的没有的present,让我赶上了NoSuchElementException异常并返回false;调用code看起来是这样的:

In some cases I'm testing against an element that I expect not to be present, so I catch the NoSuchElementException and return false; the calling code looks something like:

bool areYouThere = IsElementPresent(driver, By.CssSelector("li[name=elementThatsNotInTheDom");

wait.Until(DRVR => drvr.FindElement(所)); 通话将停止在调试器与NoSuchElementException异常是未处理由用户code消息,但我清楚地处理该异常。

The wait.Until(drvr => drvr.FindElement(by)); call stops in the debugger with a "NoSuchElementException was unhandled by user code message", but I'm clearly handling that exception.

这是不是因为我不打算使用这种模式在我的最终解决方案的一个关键问题 - 我大多只是玩弄不同的想法,有这样做的更好的办法,但我很好奇至于为什么这个异常是未处理的时候,我清楚地处理它。

This isn't a critical issue because I'm not going to use this pattern in my final solution - I'm mostly just playing around with different ideas and there are better ways of doing this, but I'm very curious as to why this exception is unhandled when I'm clearly handling it.

修改

有趣的是,即使我添加了一个通用的

Interestingly enough, even if I add a generic

catch (Exception) {
    return false;
}

的方法,它仍然未捕获的。

to the method it's still uncaught.

编辑2

其实我misspoke以上 - 通用的异常最终被捉住了,但是当它到达那里它是作为一个WebDriverTimeoutException

Actually I misspoke above - the generic exception is eventually caught, but when it gets there it's as a WebDriverTimeoutException.

和这里的啊哈!时刻:

WebDriverWait.Until()只抛出WebDriverTimeoutException;在文档网是不完整的,但红宝石文档稍微有启发的。因此,我认为正在发生的事情是,在lambda是扔我期望异常类,以及相应的等待期后,WebDrierWait.Until()抛出的WebDriverTimeoutException。我可以移动我的try / catch块到拉姆达,正是如此证实了这一点:

WebDriverWait.Until() only throws WebDriverTimeoutException; the .Net docs are incomplete, but the Ruby docs are slightly more instructive. So what I think is happening is that the lambda is throwing the exception class I expect, and after the appropriate wait period, WebDrierWait.Until() throws the WebDriverTimeoutException. I can confirm this by moving my try/catch block into the lambda, thusly:

wait.Until(drvr => {
    try {
        drvr.FindElement(by);
        return true;
    } catch (OpenQA.Selenium.NotFoundException) {
        return false;
    } catch (System.TimeoutException) {
        return false;
    } catch (Exception) {
        return false;
    }
});

在这种情况下,适当的异常被捕获。解开了谜底!

In this situation, the appropriate exception is caught. Mystery solved!

推荐答案

这可能是一个长镜头,但我唯一的想法是,也许有两个 NoSuchElementException异常类和你正在处理您的命名空间中一个可访问的,而code抛出其他。

This may be a long shot, but the only idea that I have is that maybe there's a name conflict between two NoSuchElementException classes and you're handling the one accessible from your namespace while the code is throwing the other.