There are many cases, where a application displays multiple windows when you open a website. Those are may be advertisements or may be a kind of information showing on popup windows. We can handle multiple windows using Windows Handlers in selenium webdriver.
Step 1: After opening the website, we need to get the main window handle by using driver.getWindowHandle();
The window handle will be in a form of lengthy alpha numeric
Step 2: We now need to get all the window handles by using driver.getWindowHandles();
Step 3: We will compare all the window handles with the main Window handles and perform the operation the window which we need.
Click here to view Performing operations on multiple windows using reusable methods.
The below example shows how to handle multiple windows and close all the child windows which are not need. We need to compare the main window handle to all the other window handles and close them.
package com.pack;
import java.util.Set;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class WindowExamples {
static WebDriver driver;
@Test
public void test_CloseAllWindowsExceptMainWindow() {
driver = new FirefoxDriver();
// It will open Naukri website with multiple windows
driver.get("http://www.naukri.com/");
// To get the main window handle
String windowTitle= getCurrentWindowTitle();
String mainWindow = getMainWindowHandle(driver);
Assert.assertTrue(closeAllOtherWindows(mainWindow));
Assert.assertTrue(windowTitle.contains("Jobs - Recruitment"), "Main window title is not matching");
}
public String getMainWindowHandle(WebDriver driver) {
return driver.getWindowHandle();
}
public String getCurrentWindowTitle() {
String windowTitle = driver.getTitle();
return windowTitle;
}
//To close all the other windows except the main window.
public static boolean closeAllOtherWindows(String openWindowHandle) {
Set<String> allWindowHandles = driver.getWindowHandles();
for (String currentWindowHandle : allWindowHandles) {
if (!currentWindowHandle.equals(openWindowHandle)) {
driver.switchTo().window(currentWindowHandle);
driver.close();
}
}
driver.switchTo().window(openWindowHandle);
if (driver.getWindowHandles().size() == 1)
return true;
else
return false;
}
}
The below image will show you the multiple windows that open in the application. It has now open total of three windows (One is main window and other two are child windows)
The below image will show the multiple window handlers for child windows and main window. We will have all the window handles in one set and we use each of them to compare and perform operation on the required window.
The below is the output of the program:
{2b2577c4-bf92-4392-a93f-b0428a3d9aab}
Naukri.com – Jobs – Jobs in India – Recruitment – Job Search – Employment – Job Vacancies
it is the main window
Naukri.com – Jobs – Jobs in India – Recruitment – Job Search – Employment – Job Vacancies
Barclays
Naukri.com – Jobs – Jobs in India – Recruitment – Job Search – Employment – Job Vacancies
HCL
Naukri.com – Jobs – Jobs in India – Recruitment – Job Search – Employment – Job Vacancies
Comments
webdriver window handel
very helpful code to handle windows thanx
Nice
Nice
Thank you
Great tutorial. It gives very clear sample code and explanation. I love it.
Thank you
will u tell me in place of
will u tell me in place of open window handle argument which string value i have to give?"
Thanks
Nice post, keep posting please. I was facing the same problem in my site. But I got the solution. Thanks again
Modal Dialog Box
Hi,
I have one question.
A have modal dialog box that sudenly pop up on the screen
Not i want to click Skip link on the modal dialog box
but i am not switch to modal dialog box.
what i observed is the modal dialog box and main window both have the same id.
i.e the below condition never happens
if (!currentWindowHandle.equals(openWindowHandle))
can you give some direction... i
regards,
venkat
This is absolutely soo good
This is absolutely soo good example for closing the dynamic pop up window
SELENIUM
HOW TO IDENTIFY AND AUTOMATE AUTHENTICATION REQUIRED FIELD USING SELENIUM WEDDRIVER
AUTOIT
IT will depemds on the type of authenticaion you are uisng i mean window or web based
if is window based use autoit or robot class
for webbased one use Alert class
Great Article
Hi, This is a wonderful article and exactly solves my problem. But I have a different problem - website opens, and a pop up comes on which is a tour guide with options to skip, next and previous. I am able to get the div of the pop up but not the skip or next or previous buttons inside it. It doesnt show up as an alert or confirmation box. Please help.
Add new comment