About this lesson
getWindowHandle and getWindowHandles in Selenium Java
Learn Selenium JavaAdvancedWindow Management
How it works
Each browser window or tab in a session has a handle, an opaque string that WebDriver uses to tell them apart. In Chrome it looks like 32 hexadecimal characters. getWindowHandle() returns the handle of the window the driver is currently controlling, and getWindowHandles() returns a Set<String> with the handles of every open window in the session.
Handles are what switchTo().window() needs, so the usual first step before anything that opens a new window is to save the current handle. Handles stay the same for the life of the window, but they mean nothing outside the session, so never hard-code one.
Opening a tab does not move the driver. On the Buttons page, openNewTab opens a second tab, the handle count goes up, but the driver keeps working in the original tab until you switch.
About the assignment
On the Buttons page in a fresh session there is only one window. You read its handle, count all handles, and print the page title, the three facts you check before working with several windows.
What you will do
- Get the current handle and print whether it is non-empty after the label
Handle exists:. - Print the size of the set from
getWindowHandles()after the labelWindow count:. - Print the page title after the label
Title:.
Common mistakes
Relying on set order
getWindowHandles()returns aSet. Chrome happens to list handles in opening order, but the API does not promise it, so find the new handle by removing the original from the set.Using a closed window
After
close()the driver still points at the closed window. The next command throwsNoSuchWindowException: no such window: target window already closeduntil you switch to another handle.Expecting a click to switch windows
A link or button that opens a tab leaves the driver where it was. Lookups keep running in the old tab.
In a real test suite
Handles drive every multi-window test: OAuth sign-in popups, "open in new tab" links, and PDF previews. Save the original handle, act, find the new handle, switch, check, close it and switch back.
Lesson 47 finished frames. Lesson 49 opens a second tab with Selenium 4's newWindow() and moves between the two.
Related lessons
- Handle Multiple WindowsOpen a new tab and switch between handles.
- Your First Selenium ScriptWhy
close()andquit()behave differently. - Window Size and PositionControl the size and position of the window a handle refers to.
To try one of these calls on any page without a task, open the Online Selenium Java Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
public static void main(String[] args) throws Exception {
WebDriver driver = new ChromeDriver();
driver.get("https://testkru.com/Elements/Buttons");
// TODO: Get window handle, verify it's not empty
// TODO: Get window handles count
// TODO: Print the title
driver.quit();
}
}