Saturday, 19 May 2012

How to check the presence of Alert (isAlertPresent) using WebDriver?

Below method will return true if the alert got displayed:

public boolean isAlertPresent() {

  boolean presentFlag = false;

  try {

   // Check the presence of alert
   Alert alert = driver.switchTo().alert();
   // Alert present; set the flag
   presentFlag = true;
   // if present consume the alert

  } catch (NoAlertPresentException ex) {
   // Alert not present

  return presentFlag;

Note - Thank you Ganesh S for asking this query.


  1. Amazing, thanks a lot my friend, I was also siting like a your banner image when I was thrown into Selenium.When I started learning then I understood it has got really cool stuff.
    I can vouch webdriver has proved the best feature in Selenium framework.
    Thanks a lot for taking a time to share a wonderful article.
    Best Selenium Training Institute in Chennai |
    Selenium Training in Velachery

  2. Great post! I see the programming coding and step by step execute the outputs.I gather this coding more information. It's helpful for me my friend. Also great blog here with all of the valuable information you have.Selenium Training in Chennai | Selenium Training in Velachery | Selenium Training in Chennai with Placement