0
votes

Whenever click on the link using web driver, IE opens a new window instead of new tab.

Quick Question - How to close the pop up present on the new window ??

Example - Window 1 opened in IE 9 and then click on the link. Clicking on the link it is opening new window (i.e Window 2) I have to close the pop up present on the Window 2.

Can some one help me in this.. Is there any way to close the pop up ?? I have tried to use some code to close the pop up but it is closing the complete Window (i.e Window 2)

Thanks.

1

1 Answers

1
votes

This code will work with any number of pop ups

 // Get current window handle
 String parentWindow = driver.getWindowHandle();
 // Get all windows handles
 Set<String> windowHandles = driver.getWindowHandles();
 Iterator<String> iterator = windowHandles.iterator();
 while (iterator.hasNext()) {
    String handle = iterator.next();
    if (!handle.contains(parentWindow)) {
         // If not current window, close it
         driver.switchTo().window(handle);
         driver.close();   
    }
 }
 driver.switchTo().window(parentWindow);