How to Handle Multiple Windows in Selenium with getWindowHandles and switchTo
Selenium is One of the most widely used web automation tools that is employed by QA engineers and developers worldwide. It allows simulating user interactions with the web application. So tasks like button clicks, filling of forms, or navigation between pages can be automated. However, in the majority of real-world testing scenarios, a web application might not be confined to a single browser window or tab only. Thus, handling multiple windows in Selenium is required.
For a QA engineer, the art of working with multiple windows is critical, given that the most commonly found scenarios in modern web applications are pop-ups, authentication windows, or even new tabs being opened in response to user actions. These situations require the ability to switch between windows or tabs, perform operations on each, and return to the original window without disrupting the flow of automation. In cases not handled in the right way, test processes can become inconsistent and lead to failing tests or missing defects.
This blog shall outline how one can switch between multiple windows with Selenium by using different methods such as getWindowHandles and switchTo. Understanding these techniques is really important in case you are building a reliable, scalable test automation framework which shall ensure smooth execution of many test cases involving multiple browser windows or tabs.
What is a Window Handle?
In Selenium, a window handle is a unique identifier given by Selenium to each window or tab opened during a test session. The window handle is basically a string value that serves to make it possible for Selenium to distinguish between different open windows or tabs and act on them separately.
Normally, when a Selenium WebDriver instance is opened, only one window will be available by default. Whenever further windows or tabs are opened after that-for example, from clicking a link or from a pop-up-each one will have a window handle of its own. The latter allows Selenium to distinguish between each of the windows and switch between them by direct references inside the automation script.
Key Methods for Window Handling in Selenium
While working with multiple browser windows or tabs in Selenium, a range of key methods perform to handle and control such windows. Below is a systematic introduction of the key ones:
getWindowHandle()
This method retrieves the handle of the currently active window. This is particularly useful when you have to keep track of the original window for getting back into it after switching to other windows.
Syntax:
String currentWindowHandle = driver.getWindowHandle();
getWindowHandles()
It returns the set of all the handles of the various open windows or tabs during the current session. It enables access to the collection of handles available of all the windows.
Syntax:
Set<String> allWindowHandles = driver.getWindowHandles();
Set<String>
These window handles are put into a set of strings, Set, from getWindowHandles(). A collection of this type organizes the window handles and makes them easier to loop through or keep track of.
Syntax:
Set<String> handles = driver.getWindowHandles();
switchTo().window()
This method is used to switch the WebDriver’s focus to a specific window, identified by its handle. After switching, any actions performed by the driver will target the selected window.
Syntax:
driver.switchTo().window(windowHandle);
These methods form the core of Selenium’s ability to handle multiple windows, allowing testers to control, switch, and interact with different windows or tabs during test automation.
How do we identify parent windows and child windows?
When implementing the automated testing of web applications, it is often required to interact with various browser windows: possibly one or more pop-up-type windows, or simply new browser tabs. The additional window you open from the initial window would typically be called the child window, whereas the window that caused the action is recognized as the parent window. Selenium offers a unique way of identifying and switching between the various windows available within a browser through window handles.
Here’s how you can identify and differentiate between the parent and child windows:
Identifying the Parent Window
The main window opened by WebDriver is the parent window.
You can use the getWindowHandle() method to retrieve the handle (unique identifier) of the parent window.
Example:
// Get the parent window handle
String parentWindowHandle = driver.getWindowHandle();
System.out.println("Parent Window Handle: " + parentWindowHandle);
Identifying the Child Window
Once a new window or tab (child window) is opened, use the getWindowHandles() method to obtain a handle for all open windows. This returns a Set containing the handles of all the currently active windows (parent window and child windows).
Iterating through the set of window handles will help you identify the child window by constant checking against the parent window handle.
Example:
// Get the parent window handle
String parentWindowHandle = driver.getWindowHandle();
// Get all window handles
Set<String> allWindowHandles = driver.getWindowHandles();
// Loop through all window handles
for (String handle : allWindowHandles) {
if (!handle.equals(parentWindowHandle)) {
// Switch to the child window
driver.switchTo().window(handle);
System.out.println("Switched to Child Window with Handle: " + handle);
}
}
Example of handling multiple windows using Window handles in Selenium
So here is how window handling in Selenium is simplified with an example: Let us say you want to log in to LinkedIn, one of the most popular social networking sites, using the “Login with Google” option. This process will open up another window or tab for you to sign in to your Google account. After you log in successfully, control returns to LinkedIn.
Here’s how we can automate this process using window handles:
Scenario: Automating Login with Google on LinkedIn
In this example, we’ll:
Open the LinkedIn login page.
Click the “Sign in with Google” button, which opens a new window for Google login.
Switch to the Google login window.
Perform actions in the new window (e.g., entering credentials).
Switch back to the original LinkedIn window.
Step-by-Step Code Example:
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class selenium { public static void main(String[] args) throws Exception
{
// Step 1: Open LinkedIn login page
driver.get("https://www.linkedin.com/login");
// Step 2: Store the parent window handle
String parentWindow = driver.getWindowHandle();
System.out.println("Parent Window Handle: " + parentWindow);
// Step 3: Click on the "Sign in with Google" button
driver.findElement(By.xpath("//button[text()='Sign in with Google']")).click();
// Step 4: Get all window handles
Set<String> allWindowHandles = driver.getWindowHandles();
System.out.println("All Window Handles: " + allWindowHandles);
// Step 5: Iterate through the window handles and switch to the child window (Google login window)
for (String windowHandle : allWindowHandles) {
if (!windowHandle.equals(parentWindow)) {
// Switch to Google login window
driver.switchTo().window(windowHandle);
System.out.println("Switched to Google Login Window");
// Step 6: Perform actions in the Google login window (e.g., enter email and password)
driver.findElement(By.id("identifierId")).sendKeys("your-email@gmail.com");
driver.findElement(By.xpath("//span[text()='Next']")).click();
// Assuming you have a wait to handle loading and next steps, you would perform further login steps here
break;
}
}
// Step 7: After completing the Google login, switch back to the LinkedIn window
driver.switchTo().window(parentWindow);
System.out.println("Switched back to Parent Window");
}
Code Explanation:
Storing the Parent Window Handle: The script first stores the parent window handle (LinkedIn login page) using getWindowHandle().
Clicking the Login Button: The “Sign in with Google” button is clicked, which opens a new window for Google login.
Retrieving All Window Handles: getWindowHandles() returns the set of all window handles. By iterating through this set, the script can identify the child window (Google login page).
Switching to the Child Window: The script switches focus to the Google login window using switchTo().window(windowHandle).
Performing Actions in the Child Window: After switching to the Google window, the script enters the Google account credentials.
Switching Back to the Parent Window: Once the actions in the Google window are completed, the script switches back to the LinkedIn window using the stored parent window handle.
How to close all windows in Selenium?
In Selenium, you may encounter situations where you need to close multiple browser windows or tabs after your test completes. By default, Selenium provides two main methods for closing browser windows: driver.close() and driver.quit(). Each serves a different purpose:
driver.close(): Closes the currently active window or tab.
driver.quit(): Closes all open browser windows or tabs, terminating the WebDriver session entirely.
Closing All Windows Using driver.quit()
The simplest way to close all open browser windows or tabs is by using the driver.quit() method. This will close every window opened during the test session, effectively ending the WebDriver session.
Closing All Windows Individually Using driver.close()
If you need more granular control over which windows to close (e.g., close all windows except the main one), you can iterate through all open windows using getWindowHandles() and close them individually with driver.close().
Conclusion
Working between multiple windows is a must-learn skill for automation in modern web applications that are pushed to involve interactions with pop-ups, new tabs, or authentication windows. QA engineers learn such skills as getWindowHandle(). getWindowsHandles(), and switchTo(). window(); combining and switching between different browser windows to contribute to a smooth and well-testing process due to test automation.
This blog has explained fundamental window-handling techniques, including identifying parent and child windows, switching between them, and performing actions on each window. By being fluent in applying them, scope will be easy to create test scenarios for third-party authentication, work with multiple windows, and those dynamic elements found on web pages.
Source: This blog was originally published at https://testgrid.io/blog/window-handling-in-selenium/
Subscribe to my newsletter
Read articles from Ronika Kashyap directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ronika Kashyap
Ronika Kashyap
Experienced Software Tester with 7+ years of ensuring product excellence. Proficient in automation, API testing, and Agile. Achieved 30% test coverage increase. Dedicated to delivering top-notch software.