Skip to content
CodeKruPro

Get Window Handle and Title

48/70

Get Window Handle and Title

Every browser window/tab has a unique handle. Use getWindowHandle() to get the current window's handle and getWindowHandles() to get all window handles.

Task:

  1. Navigate to https://testkru.com/Elements/Buttons
  2. Get and verify the current window handle is not empty
  3. Verify there is exactly 1 window open
  4. Print the page title

Expected output:

Handle exists: true
Window count: 1
Title: Buttons

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

  1. Get the current handle and print whether it is non-empty after the label Handle exists:.
  2. Print the size of the set from getWindowHandles() after the label Window count:.
  3. Print the page title after the label Title:.

Common mistakes

  • Relying on set order

    getWindowHandles() returns a Set. 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 throws NoSuchWindowException: no such window: target window already closed until 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.

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.

Main.java
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();
    }
}