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
- Open a new tab with
newWindow(WindowType.TAB). The driver is now in it. - Load the Buttons page in the new tab.
- Print the number of handles after the label
Windows open:and the title after the labelNew tab title:. - Switch back with
switchTo().window()and the saved handle, then print the title after the labelOriginal title:.
Common mistakes
Reading the title before loading a page
A new tab is blank, so
getTitle()returns an empty string until you callget().Saving the handle too late
Calling
getWindowHandle()afternewWindow()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 throwsNoSuchWindowExceptionuntil 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.
Related lessons
- Get Window Handle and TitleWindow handles and how to count them.
- Navigate Between PagesLoad a page into the new tab with
get(). - Switch Between Frames and Default ContentFrames, the other context the driver can switch into.
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/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();
}
}