title: Try/Catch not handling exceptions from Selenium2 FindElement
body: <p>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:</p>
  
  <pre><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 =&gt; drvr.FindElement(by));
          return true;
      }
      catch (NoSuchElementException)
      {
          return false;
      }
      catch (System.TimeoutException)
      {
          return false;
      }
  }
  </code></pre>
  
  <p>In some cases I'm testing against an element that I expect <em>not</em> to be present, so I catch the NoSuchElementException and return false; the calling code looks something like:</p>
  
  <pre><code>bool areYouThere = IsElementPresent(driver, By.CssSelector("li[name=elementThatsNotInTheDom");
  </code></pre>
  
  <p>The <code>wait.Until(drvr =&gt; drvr.FindElement(by));</code> call stops in the debugger with a "NoSuchElementException was unhandled by user code message", but I'm clearly handling that exception.</p>
  
  <p>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.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Interestingly enough, even if I add a generic</p>
  
  <pre><code>catch (Exception) {
      return false;
  }
  </code></pre>
  
  <p>to the method it's still uncaught.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Actually I misspoke above - the generic exception is eventually caught, but when it gets there it's as a WebDriverTimeoutException.</p>
  
  <p>And here's the Aha! moment:</p>
  
  <p>WebDriverWait.Until() only throws WebDriverTimeoutException; the <a href="http://selenium.googlecode.com/svn/trunk/docs/api/dotnet/html/M_OpenQA_Selenium_Support_UI_DefaultWait_1_Until__1.htm" rel="nofollow">.Net docs</a> are incomplete, but the <a href="http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Wait.html#until-instance_method" rel="nofollow">Ruby docs</a> 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:</p>
  
  <pre><code>wait.Until(drvr =&gt; {
      try {
          drvr.FindElement(by);
          return true;
      } catch (OpenQA.Selenium.NotFoundException) {
          return false;
      } catch (System.TimeoutException) {
          return false;
      } catch (Exception) {
          return false;
      }
  });
  </code></pre>
  
  <p>In this situation, the appropriate exception is caught. Mystery solved!</p>