title: Alert handling in Selenium WebDriver (selenium 2) with Java
body: <p>I want to detect whether an alert is popped up or not. Currently I am using the following code:</p>
  
  <pre><code>    try {
          Alert alert = webDriver.switchTo().alert();
  
          // check if alert exists
          // TODO find better way
          alert.getText();
  
          // alert handling
          log().info("Alert detected: {}" + alert.getText());
          alert.accept();
      } catch (Exception e) {
      }
  </code></pre>
  
  <p>The problem is that if there is no alert on the current state of the web page, it waits for a specific amount of time until the timeout is reached, then throws an exception and therefore the performance is really bad.</p>
  
  <p>Is there a better way, maybe an alert event handler which I can use for dynamically occurring alerts?</p>