3

Below is my Selenium Webdriver script. When I run this script my website is opened but the popup window is also opened. How do I close this popup window so the script can continue? Take a look on screenshot also.

package com.angara;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestAngaraWeb {

   public static void main(String[]args){

      WebDriver driver = new FirefoxDriver();
      driver.get("http://www.angara.com");
      driver.findElement(By.xpath("/html/body/div[6]/div/div[2]/div/div/div/ul/li/a")).click();


   }
}

enter image description here

4
  • 1
    I can see no screenshot
    – dzieciou
    Dec 7, 2012 at 12:03
  • I attached a screenshot (it's a public website). As you can see from the screenshot, this is not a new browser window.
    – user246
    Dec 7, 2012 at 15:05
  • 3
    I think the solution is a simple as locating the "X" at the top of that "window" and clicking it.
    – user246
    Dec 7, 2012 at 15:08
  • 1
    Knowing common terminology for browser windows can help you in the future. Popup generally refers to a completely new browser window and is what people will assume if you use that term. This is an inline add or popover within your existing web page.
    – Sam Woods
    Dec 8, 2012 at 2:56

8 Answers 8

5

As user246 commented, you can write a method to locate the 'X' on the popup and click it if found. Then you can define a custom method to locate an element on the page, something like this (kind of pseudo code):

myFindElement(String xpath)
{
    try{
        driver.findElement(By.xpath(path))
    }
    catch (ElementNotFoundException e){
        if !closethepopup(){print ('Element not found')}
    }
    catch (GeneralException ge){
    }
}

Within your closethepopup() method, you can choose to add some logic to return false if the popup is not found

4

Try this :

Driver.SwitchTo().frame("Fancybox-frame");

Driver.findElement("enter your xpath to close popup").click();

Driver.SwitchTo().defaultContent();
4
  • To close popup- first you need to check which one is the active window in your application (Popup or main window).

  • If popup is active then you can use driver.close(); OR

  • If the main window is active (Focused), then you need to use window handler to close this popup.

2
System.setProperty("webdriver.gecko.driver", "D:\\all\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.get("https://www.angara.com/");
Thread.sleep(5000);

for (String winhandle: driver.getWindowHandles()) {
    driver.switchTo().window(winhandle);
    System.out.println("Window Switch");        
    Thread.sleep(2000);

    driver.findElement(By.xpath("(//button[span[contains(text(),'Close')]])[1]")).click();
}

Try this out, this should help you out.

1
  • Could you please add some details about the code, what is it doing and how could it help the OP? Thanks.
    – alecxe
    Feb 28, 2018 at 18:57
2

this really worked for me-

driver.findElement(By.xpath("//*[@class='_2AkmmA _29YdH8']")).click();
0
WebDriver driver = new FirefoxDriver();
driver.get("http://www.angara.com/");
driver.findElement(By.xpath("//*[@id='popsubform']/a/img")).click();

Try this out... hope this will help you out..

0

You can refer this code to close a pop window by clicking on [X].

driver.findElement(By.xpath("//*[@class='_2AkmmA _29YdH8']")).click();

change this thing as per your use @class

0
  driver.findElement(By.xpath("//*[@id="popsubform"]/a/img")).click();

This should look for the "X" on the pop up and close it.

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.