Blog

How to Handle Popups in Selenium WebDriver?

Selenium WebDriver

Selenium WebDriver also known as Selenium2.0 has some complex scenarios to deal with, one of the very important is Handling Popups during automation of browsers. As we know, in different applications we have different business logic and work flows like there can be an application that doesn’t contain any popup window or alert or there can be an application that contains different popups at different levels. So, during automation it’s pretty hard to automate the content on popup windows. In this blog, I am sharing how to deal with popup windows using selenium WebDriver.

Problem & Solutions

Method 1:

Starting from the simplest way that popup window has name or id, so we can use name or id of popup window in our script to locate it and then further access the controls on that window to proceed our test case flow.

Below is the command used to handle popup window with its name.

driver.switchTo().window(“”);

Now let’s deal with the more complex scenario and that is popup window with no name or id, and Automation Engineer doesn’t want to hardcode the window name.

Here is the solution of this problem:
First of all, get the main window handle before opening the popup window, script command used to get the main window handle is as below:
String mainWindowHandle=driver.getWindowHandle();

Now open popup window by clicking the element which causes the popup window to open, command used to do this is: webElement.click();

The command written below will get all available open window handles

Set s = driver.getWindowHandles();

As we got all the open popup window handles, now we need to catch the latest/newly opened window and then switch the control to that popup as we already know the main window handler.

Set s = driver.getWindowHandles();

Iterator ite = s.iterator();

while(ite.hasNext())

{

String popupHandle=ite.next().toString();

if(!popupHandle.contains(mainWindowHandle))

{

driver.switchTo().window(popupHandle);

}

}

Using this script to get the control of the popup window and now we can perform our required actions in popup window.

After performing this we can easily switch back to main window using the following command

driver.switchTo().window( mainWindowHandle );

Method 2:

The second method is almost same but the difference is that after getting all the open popup windows, we will use a command to remove all the handles after the popup window appears, by doing this only one window handle will be left i.e. required popup window.

Script commands used to perform this method is mentioned below:

// get all the window handles before the popup window appears

Set beforePopup = driver.getWindowHandles();

// click the link which creates the popup window

driver.findElement(by).click();

// get all the window handles after the popup window appears

Set afterPopup = driver.getWindowHandles();

// remove all the handles from before the popup window appears

afterPopup.removeAll(beforePopup);

// there should be only one window handle left

if(afterPopup.size() == 1) {

driver.switchTo().window((String)afterPopup.toArray()[0]);