
Web applications often rely on multiple browser windows and tabs to complete workflows. A user might log in through a third-party authentication service, complete a payment on a separate page, or interact with pop-up elements during navigation.
For automation engineers, failing to control these windows properly leads to brittle test cases that either hang or throw errors. Selenium WebDriver provides window handles, a feature designed to identify and manage each open window or tab uniquely.
Mastering window handling ensures automated tests can accurately simulate real-world user interactions.
By default, when Selenium WebDriver launches a browser, it operates in a single window. However, any user action—like clicking a link with target="_blank" or invoking JavaScript that spawns a pop-up—may generate new windows or tabs.
Without explicit handling, the driver remains bound to the first (parent) window, making subsequent actions ineffective in newly opened contexts. To address this, Selenium requires testers to:
This sequence allows reliable navigation between multiple windows during test execution.
Each browser window or tab receives a unique string identifier called a window handle. These identifiers remain valid for the duration of the browser session.
Why they matter:
For example, when a banking portal redirects to a third-party verification window, capturing and switching handles guarantees the test completes the transaction without interruption.
Below is a complete Java example showing how to interact with parent and child windows.
public class MultiWindowDemo {
WebDriver driver;
@BeforeMethod
public void setup() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://example.com");
}
@Test
public void handleMultipleWindows() {
// Store parent window handle
String parentWindow = driver.getWindowHandle();
// Trigger new window
driver.findElement(By.id("openWindow")).click();
// Get all available window handles
Set<String> windowHandles = driver.getWindowHandles();
for (String handle : windowHandles) {
if (!handle.equals(parentWindow)) {
driver.switchTo().window(handle);
System.out.println("Child window title: " + driver.getTitle());
// Perform actions in the child window
driver.close(); // Close child
}
}
// Switch back to parent window
driver.switchTo().window(parentWindow);
System.out.println("Parent window title: " + driver.getTitle());
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}
This script ensures proper navigation between multiple windows, avoids confusion, and guarantees test stability.
Selenium offers two methods that form the backbone of window handling:
String parentHandle = driver.getWindowHandle();
Set<String> handles = driver.getWindowHandles();
Together, these methods provide precise control, ensuring test cases can both target and return to specific windows as needed.
Switching between windows involves a structured process:
This disciplined sequence ensures clean, repeatable automation across windows.

While window handling seems straightforward, test engineers often encounter pitfalls:
By anticipating these challenges, teams can create more resilient test suites.
Testing locally often masks issues that appear on real devices and diverse browser versions. BrowserStack Automate allows execution of window-handling tests in environments that mirror real-world usage.
Key benefits include:
Example integration:
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("browserName", "Chrome");
caps.setCapability("browserVersion", "latest");
HashMap<String, Object> bstackOptions = new HashMap<>();
bstackOptions.put("os", "Windows");
bstackOptions.put("osVersion", "11");
caps.setCapability("bstack:options", bstackOptions);
WebDriver driver = new RemoteWebDriver(
new URL("https://hub-cloud.browserstack.com/wd/hub"), caps);
driver.get("https://example.com");
// Continue with window handling logic here
This setup ensures robust validation of multi-window workflows under real-world conditions.
Window handling in Selenium is an indispensable skill for testing modern web applications. From authentication flows to payment gateways, handling multiple windows accurately ensures automation reflects real user journeys.
By leveraging getWindowHandle() and getWindowHandles(), adopting a disciplined switching sequence, and addressing common pitfalls, teams can stabilize their test suites. Executing these scenarios on BrowserStack further strengthens test reliability by validating across real devices and browsers at scale.
Run Selenium Tests on Cloud
Get visual proof, steps to reproduce and technical logs with one click
Continue reading
Try Bird on your next bug - you’ll love it
“Game changer”
Julie, Head of QA
Try Bird later, from your desktop