windows handling using selenium webdriver

Reading Time: 3 minutes

Selenium is one of the most loved UI automation tools. Selenium makes tester’s life easier. And for those who don’t know this beautiful tool can start with this baby steps in exploring this tool. windows handling is one of the basic problems when we are talking about UI Automation.


  1. Java:- As programming language.
  2. Eclipse:- As an IDE where we’ll write code.
  3. Selenium plugin:- we’ll use selenium standalone server.
  4. Browser Driver:- we’ll use ‘geckodriver’ and ‘chromedriver’ for running the UI test case.


  1. Initialize the wedriver and create the object.
  2. create an instance of the driver(geckodriver/chromedriver).
  3. Get the URL of the UI page to test.
  4. Find the element using any locator.

Why we need to handle multiple window handling:-

The first question comes in our mind why we need to handle multiple windows? So, the answer to this is to mimic the real user behavior. Often, the application architecture involves managing multiple windows. Whatever the explanation, but to continue executing our test cases, we need to provide an exact application flow because the new window would hamper the driver’s background.

A window handle is a unique identifier that holds the address of all the opened windows. This is a pointer or a placeholder to a window. This identifier is a string value. Every browser is having a unique window handle.

Useful methods:-

  1. get.windowhandle(): This mehod will be used to get the window handle of the current window.
  2. get.windowhandles(): This method will be used to get all the opened window.
  3. set: This is used to set the window handles which is in the form of a string. set<string> set = driver.get.windowhandles().
  4. switchTo(): It helps in switching between the windows.
  5. action: we’ll action method to perform certain actions on the windows.

Multiple window opening scenario:-

Let’s see one scenario where we’ll open multiple windows. In this example, we’ll open one web page by clicking one web element. Below is the code for opening the multiple browser windows:-

WebElement clickelement = driver.findElement("button1"));;
opening multiple windows

Printing all the window handles:-

In this example, we’ll print all the window handles and see how every window have their unique id.

Multiple windows handling

Window handling and switching to desired window:-

We’ll see how to implement Window handling in this example. We’ll open several windows first. Now we will switch back to the parent window. After this, we will close this window. And in the last, we’ll open a new web page.

package com.knoldus.selenium;

import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class WindowHandlingDemo1 {
	public static void main(String[] args) throws InterruptedException {
		String pathToChrome =  "/home/knoldus/Downloads/chromedriver/chromedriver";
		System.setProperty("", pathToChrome);
		WebDriver driver = new ChromeDriver();
		String parentWindowHandle = driver.getWindowHandle();
		WebElement clickelement = driver.findElement("button1")); 
		for(int i =0; i<3;i++) {;
		Set<String> allWindowHandles = driver.getWindowHandles();
		String lastwindowhandle = "";
		for(String handle: allWindowHandles) {
			System.out.println("Switching to windoow::" +handle);
			System.out.println("Navigating to");
			lastwindowhandle = handle;
		// switch to the parent window
		// now there is no focused window so we are explictly switching some window 


In this blog, we implemented windows handling for the chrome browser. We can use this feature to automate various UI automation flow and can achieve end to end testing.

References: – ,

Written by 

QA Consultant