How To Handle Unexpected Alerts In Selenium WebDriver

Some times when we browsing software web application, Display some unexpected alerts due to some error or some other reasons. This kind of alerts not display every time but they are displaying only some time. If you have created webdriver software automation test case for such page and not handled this kind of alerts In your code then your script will fail Immediately If such unexpected alert pop up displayed.

We have already learnt, How to handle expected alert popups In selenium webdriver software testing tool In THIS EXAMPLE. Now unexpected alert appears only some times so we can not write direct code to accept or dismiss that alert. In this kind of situation, we have to handle them specially.

To handle this kind of unexpected alerts, You must at least aware about on which action such unexpected alert Is generated. Sometimes, They are generated during software web application page load and sometime they are generated when you perform some action. So first of all we have to note down the action where such unexpected alert Is generated and then we can check for alert after performing that action. We need to use try catch block for checking such unexpected alters because If we will use direct code(without try catch) to accept or dismiss alert and If alert not appears then our test case will fail. try catch can handle both situations.

I have one example where alert Is displaying when loading software web page. So we can check for alert Inside try catch block after page load as shown In bellow given example. After loading page, It will check for alert. If alert Is there on the page then It will dismiss It else It will go to catch block and print message as shown In bellow given example.

Run bellow given example In eclipse with testng and observe the result.
package Testng_Pack;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class unexpected_alert {
 WebDriver driver = null;

 public void setup() throws Exception {
  System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
  driver = new FirefoxDriver();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

 public void tearDown() throws Exception {

 public void Text() throws InterruptedException {
  //To handle unexpected alert on page load.
  }catch(Exception e){ 
   System.out.println("unexpected alert not present");   

Above given webdriver code Is just for example. It Is just explaining the way of using try catch block to handle unexpected alert. You can use such try catch block In that area where you are facing unexpected alerts very frequently.


  1. Can u please tell how to handle unexpected popup window as well.
    Thank you

  2. Great! This works for me. Thank u :)

  3. In our mobile application i started some process which is happening in Background mean while my test execution should continue once the background process completes popup comes with cancel or delete option.I dont know at what step of my execution this popup comes?.So how can i handle this popup.Right now i am handling with thread concept but it sometimes not perfect.

  4. This program is not working .. WE have to either put Thread.sleep(6) or explicit or fluent wait before driver.switchTo().alert().dismiss(); , then only the above program works..

    thanks for the share

  5. Here you knows the action that some where you may get the alert, so you can do accept/dismiss. But In over all application, you doesn't anything that when or after which action you may find any popup/alert, how you will handle that type of situation