title: How do I check if an alert is present quickly?
body: <p>When trying to check if an alert is present, I call an extension method on <code>IWebDriver</code> called <code>AlertIsDisplayed</code> which is this:</p>
  
  <pre><code>try
  {
      Driver.Instance.SwitchTo().Alert();
  
      return true;
  }
  catch (NoAlertPresentException)
  {
      return false;
  }
  finally
  {
      Driver.Instance.SwitchTo().DefaultContent();
  }
  </code></pre>
  
  <p>However, due to the fact I am catching an exception, this is quite costly taking 2-3 seconds to return a result. With this being used in hundreds of tests it adds up to several minutes of extra execution time.</p>
  
  <p>So in order to speed it up I tried changing the same method to this:</p>
  
  <pre><code>return ExpectedConditions.AlertIsPresent()(driver) != null;
  </code></pre>
  
  <p>Where <code>driver</code> is the <code>IWebDriver</code> the extension method was called on.</p>
  
  <p>However, this takes the same amount of time. Looking at the <a href="https://github.com/SeleniumHQ/selenium/blob/ceaf3da79542024becdda5953059dfbb96fb3a90/dotnet/src/support/UI/ExpectedConditions.cs#L576" rel="nofollow noreferrer">source code of <code>ExpectedConditions.AlertIsPresent</code></a> reveals why - it is exactly what I was doing before, but just in a wrapper..</p>
  
  <p>I have set my <code>ImplicitWait</code> to 0.</p>
  
  <p>This is running on Selenium version 2.53.1. My driver is an <code>EventFiringWebDriver</code> with a <code>WrappedDriver</code> of <code>FirefoxDriver</code>. The Firefox version this is running on is 47.0.1.</p>
  
  <p>Are there any alternatives to checking if an alert is present which take less time?</p>