Skip to content
CodeKruPro

Handle Multiple Windows

49/70

Handle Multiple Windows

Selenium 4 provides driver.switchTo().newWindow(WindowType.TAB) to open a new tab.

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Save the original window handle
  3. Open a new tab using Selenium 4's newWindow API
  4. Navigate the new tab to https://testkru.com/Elements/Buttons
  5. Print the new tab's title
  6. Switch back to the original window and print its title

Expected output:

Windows open: 2
New tab title: Buttons
Original title: Text Fields

About this lesson

Open and switch to a new tab in Selenium Java with newWindow(WindowType.TAB)

Learn Selenium JavaAdvancedWindow Management

How it works

Selenium 4 added driver.switchTo().newWindow(WindowType.TAB), which opens a new tab and switches the driver to it in one step. WindowType.WINDOW opens a separate window instead. The new tab starts at about:blank with an empty title, so you load a page into it with get() as usual.

Getting back is your job. The driver stays in the new tab until you call driver.switchTo().window(handle) with the handle you saved before opening it. Saving that handle first is the habit that makes multi-window tests reliable.

Tabs opened by the page itself, through a link with target="_blank" or a button such as openNewTab, are different: the driver does not follow them. You find their handle in getWindowHandles() and switch yourself.

About the assignment

The starter saves the Text Fields tab's handle. You open a second tab, load the Buttons page in it, report the tab count and the new title, then switch back and report the original tab's title.

What you will do

  1. Open a new tab with newWindow(WindowType.TAB). The driver is now in it.
  2. Load the Buttons page in the new tab.
  3. Print the number of handles after the label Windows open: and the title after the label New tab title:.
  4. Switch back with switchTo().window() and the saved handle, then print the title after the label Original title:.

Common mistakes

  • Reading the title before loading a page

    A new tab is blank, so getTitle() returns an empty string until you call get().

  • Saving the handle too late

    Calling getWindowHandle() after newWindow() gives you the new tab's handle, and "switching back" leaves you where you are.

  • Closing the tab without switching

    After close() on the new tab, any command throws NoSuchWindowException until you switch to the original handle.

In a real test suite

Tests open extra tabs to check a link target, to use a second account side by side, or to compare two pages. Most close the extra tab and switch back at the end so later steps start from a known window.

Lesson 48 introduced handles. Lesson 50 sets the window's size, which decides whether pages show their desktop or mobile layout.

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/TextFields");
        
        String originalWindow = driver.getWindowHandle();
        
        // TODO: Open a new tab with driver.switchTo().newWindow(WindowType.TAB)
        // TODO: Navigate new tab to Buttons page
        // TODO: Print window count and new tab title
        // TODO: Switch back to original window and print title
        
        driver.quit();
    }
}