I am currently working on Automation project for E-commerce site. In some pages unexpected popup's(ex:ad's) are displaying. Because of that my script was failing by throwing exceptions like ElementNotFoundException and currently Element is not Interactable etc.. . Here these ad's are not specific to particular page. It may come at any page.

Please let me know, Is there any way to handle these type of scenario?

  • 1
    is that ad comes in iframe? can you please share that web page URL? Commented Nov 6, 2015 at 6:49
  • No, it includes in Website
    – QAMember
    Commented Nov 6, 2015 at 6:57
  • can you please share website URL? Commented Nov 6, 2015 at 7:08
  • it is local url
    – QAMember
    Commented Nov 6, 2015 at 7:23
  • 1
    you should share that page screenshot and html code of that page ad. part. Commented Nov 6, 2015 at 7:38

3 Answers 3


To provide an exact solution to the question screenshot of the popup is necessary (I am just gonna state out all possible scenarios). A popup can be an alert or an ad in a iframe or an ad in a new window and all these are treated in different ways.

  1. Assuming its an Alert, log_file has already provided you the solution, i.e. to use alertIsPresent(), and accept or dimiss it as per your requirements.
  2. Assuming the ad is in iframe, follow the below approach:

    driver.findElement(By.id(locatorToCloseAd)).click(); //Close Ad
    driver.switchTo().defaultContent(); // Return to main window
  3. Assuming the ad in the window, follow the below approach:

    String mainWinHandle = driver.getWindowHandle(); // Get your main window
    String subWinHandle = null;
    Set<String> allHandle = driver.getWindowHandles(); // Fetch all handles
    Iterator<String> iterator = allHandle.iterator();
    while (iterator.hasNext()){
        subWindowHandler = iterator.next();
    driver.switchTo().window(subWindowHandler); // switch to popup 
    //Write code to close Ad or skip                                            

So, you need to use ExpectedConditions in all the three scenarios and based on your need, please put them into a function and call them in your @BeforeTest or @Test (not all but atleast in those scenarios where you suspect an unexepected ad might appear).


Please let me know, Is there any way to handle these type of scenario's

Depending on your toolset, you could check for the pop-up and close it if it exists (with each page load). If it's a modal (and not an alert or new window) that will be a bit easier, especially in Selenium (or Selenium IDE with the SideFlow plugin).


Try to control the JS Alert in the @BeforeTest, if you are using TestNG (or in the def setUp( ) if using PyUnit).

The Steps would be something like this :

  1. Initialize your test (getting the URL and maximize the window etc.)
  2. Wait for 30 seconds to check for alertIsPresent( ) using ExpectedConditions.
  3. If alert is present, then accept the alert and continue.
  4. If wait fails, then do something (print something like alert not present) and carry on with your work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.