How to close the pop up window in selenium running?

susantjs picture susantjs · Jan 18, 2011 · Viewed 27.9k times · Source

I wanna close the pop up window (known window name), and back to the original window. What shall I do? If I can't get a constant of the close button in window. so is there any general behavior to reach the goal?

Answer

stefan picture stefan · Sep 12, 2011

Using WebDriver (shown with Java) you could do something like this:

// instantiate your driver
...

// get window handle
String baseWindowHdl = driver.getWindowHandle();

// navigate to pop-up
...

// close pop-up
driver.close();

// switch back to base window
driver.switchTo().window(baseWindowHdl);