0
0
Selenium Javatesting~5 mins

Window handles (getWindowHandles) in Selenium Java

Choose your learning style9 modes available
Introduction

Window handles help you switch between different browser windows or tabs during a test. This lets you control and test multiple windows easily.

When your test opens a new browser tab or window and you need to switch to it.
When you want to verify content in a popup window.
When you need to close one window and continue testing in another.
When testing multi-window workflows like login popups or external links.
When automating scenarios that involve multiple browser windows.
Syntax
Selenium Java
Set<String> windowHandles = driver.getWindowHandles();

getWindowHandles() returns a set of unique IDs for all open windows.

You can use these IDs to switch control between windows using driver.switchTo().window(handle).

Examples
This prints all window handles currently open.
Selenium Java
Set<String> handles = driver.getWindowHandles();
for (String handle : handles) {
    System.out.println(handle);
}
This switches to the new window that is not the main one.
Selenium Java
String mainWindow = driver.getWindowHandle();
Set<String> allWindows = driver.getWindowHandles();
for (String window : allWindows) {
    if (!window.equals(mainWindow)) {
        driver.switchTo().window(window);
        break;
    }
}
Sample Program

This test opens a website, then opens a new tab with Google. It gets all window handles, switches to the new tab, and prints its title.

Selenium Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;

public class WindowHandlesExample {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();

        driver.get("https://www.example.com");
        String mainWindow = driver.getWindowHandle();

        // Simulate opening a new window (for example purposes, open a new URL in new tab)
        ((org.openqa.selenium.JavascriptExecutor) driver).executeScript("window.open('https://www.google.com','_blank');");

        Set<String> allWindows = driver.getWindowHandles();
        System.out.println("Number of windows: " + allWindows.size());

        for (String window : allWindows) {
            if (!window.equals(mainWindow)) {
                driver.switchTo().window(window);
                System.out.println("Switched to new window with title: " + driver.getTitle());
                break;
            }
        }

        driver.quit();
    }
}
OutputSuccess
Important Notes

Always store the main window handle before opening new windows to switch back later.

Window handles are unique strings identifying each open window or tab.

Use driver.switchTo().window(handle) to change focus to a specific window.

Summary

Window handles let you manage multiple browser windows or tabs in Selenium tests.

getWindowHandles() returns all open window IDs as a set.

Switch between windows using their handles to test multi-window scenarios.