Window Handling in Selenium
Selenium is an open source tool for automation across many web browsers. When automating a website or web application there will be scenario where a button click navigates to opening of new browser window and performing some actions in the newly opened browser window and transfer the control back to Initial browser window. Now let’s see how window handling is done in Selenium Webdriver in this blog.
What is Window in Selenium
Window in Selenium is the main page the user lands on after clicking the URL/link. This window is considered as Parent Window or Main Window which opens when the Selenium Webdriver session is created and has all the focus of the Webdriver.
Parent Window - The User first clicks a URL and a webpage Opens. This main page where user lands after clicking the URL is called Parent Window or main window. Then the actions will be performed in this main page. For Example we can take the https://demoqa.com/browser-windows website. After entering the URL the webpage displayed as shown below is the Main or Parent Window.

Child Window - All windows that opens inside the main window is termed as Child Window. In the Tools QA website, when we click New Window or New Window Message buttons, new window is opened. These new windows are considered as Child Windows. When the New Window Button is clicked, a new window with URL is opened like this,

But when we click the New Window Message, a new child window will be opened with no Explicit URL. A Child window may or may not have URL.

A Parent Window can have single or multiple Child Windows. When we manually test the application, we can validate the behavior of the child windows as they are visible in the context of the main window. But when we try to automate the same using selenium, we need to know how to handle multiple windows.
Why Do We Need to Handle Windows in Selenium
In a Web application, there may be scenario where User lands on the main page and performs certain operation, which then lead to a new child window. Now the action needs to be done in the child window.
Selenium Webdriver will not be able to locate the elements in the child window, as it was not in the same context in which Selenium Webdriver is executing. Selenium works in a specific context only, and each of the child windows will have a separate context. So, for automating such scenarios using Selenium WebDriver, we have to direct the WebDriver accordingly, to get the context of the specific window and perform the needed actions in that window. For doing this, Selenium Webdriver makes use of a specific ID of the window known as handle of the window.
What is Window Handle
A window handle stores the unique address of the browser windows. It is just a pointer to a window, whose return type is alphanumeric. The window handle in Selenium helps in handling multiple windows and child windows. Each browser will have a unique window handle value with which we can identify them.
In below Example https://demoqa.com/browser-windows website, there are 3 windows opened, one Parent Window and 2 Child Window. All the three have unique ID or window handle.

Selenium has methods which we can use to get the window handle/ID to switch context back and forth.
Methods Used for Window Handling
Listed below are the various methods used by Selenium Webdriver for handling windows.
getWindowHandle() - This method gives a unique ID of the current window which will identify it within this driver instance. This method will return the value of the String type.
getWindowHandles() - This method gives the IDs of all the windows opened by the web driver. Its return type is Set .
switchto() - Using this method, we can perform switch operations within windows.
action - This method helps in performing certain actions on the windows.
How to Handle Child window
In a Web Application with child window, we need to handle the window to make everything work properly. We can use getWindowHandle() and getWindowHandles() method for window handling and swtichTo() method to switch between the windows.
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class childWindow {
public static void main(String[] args) {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://demoqa.com/browser-windows");
// Open new child window within the main window
driver.findElement(By.id("windowButton")).click();
// Get handles of the windows
String mainWindowHandle = driver.getWindowHandle();
Set<String> allWindowHandles = driver.getWindowHandles();
Iterator<String> iterator = allWindowHandles.iterator();
// Here we will check if child window has other child windows and will fetch the
// heading of the child window
while (iterator.hasNext()) {
String ChildWindow = iterator.next();
if (!mainWindowHandle.equalsIgnoreCase(ChildWindow)) {
driver.switchTo().window(ChildWindow);
WebElement text = driver.findElement(By.id("sampleHeading"));
System.out.println("Heading of child window is: " + text.getText());
}
}
}
}
The method getWindowHandles() return a Set of Strings, so we used Set<String> to store all the child window handles. The String iteration is done through Iterator<String>.
Console Output:

Handle Multiple Windows
In a Web Application, where we have multiple windows to work on, we may need to switch control from one window to another to perform the required action. We can achieve this using window handle to store the unique value of the windows and switchTo() method to transfer control between windows.
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class multipleChildWindows {
public static void main(String[] args) throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://demoqa.com/browser-windows");
// Opening all the child window
driver.findElement(By.id("windowButton")).click();
driver.findElement(By.id("messageWindowButton")).click();
String MainWindow = driver.getWindowHandle();
System.out.println("Parent/Main window handle is " + MainWindow);
// To handle all new opened window
Set<String> s1 = driver.getWindowHandles();
System.out.println("Child window handle is" + s1);
Iterator<String> i1 = s1.iterator();
// Here we will check if child window has other child windows and when child window
//is the main window it will come out of loop.
while (i1.hasNext()) {
String ChildWindow = i1.next();
if (!MainWindow.equalsIgnoreCase(ChildWindow)) {
driver.switchTo().window(ChildWindow);
driver.close();
System.out.println("Child window closed");
}
}
}
}
In the above code, we are storing all the window handles in Set data type and switching to the child windows.
Console Output:

Switching Between Windows
Once the webdriver has switched to the child window, Webdriver has control of only the child window and cannot perform any operation in the parent window. So to switch back to parent window, we need to use the function switchTo().
// Switch back to the main window which is the parent window.
driver.switchTo().window(mainwindow);
Close all the Windows
To close windows 2 methods exist, they are
driver.close() - This method is used to close the Current window that Selenium Webdriver has focus. When we are working on multiple windows it is important to close windows simultaneously when we finish the action.
driver.quit() – This method is used to close all the windows opened in a particular session. It basically stops the driver instance and any further actions to WebDriver may result in an exception. It is generally the last statement of any code.
//Close a tab or window
driver.close();
driver.switchTo().window(mainwindow);
//Close all windows
driver.quit();
We may get “nosuchwindow” exception when we use any Webdriver command on closed windows.