Skip to main content

How to Open a New Tab using Selenium WebDriver with Java?

How to Open a New Tab using Selenium WebDriver with Java?

Key takeaways

  • In Selenium 4, driver.switchTo().newWindow(WindowType.TAB) opens a blank tab and moves the driver to it in one call.
  • To open an existing link in a new tab, Ctrl+click it (Command+click on macOS) with the Actions class, then switch to the new window handle.
  • Find the new tab by comparing handles before and after; the WebDriver spec says the order of handles is arbitrary.
  • Close the tabs you open and switch back to a handle you saved, or later steps run against a closed window.

To open a new tab with Selenium WebDriver in Java, call driver.switchTo().newWindow(WindowType.TAB). It has been part of the API since Selenium 4: it opens a blank tab, switches the driver to it, and you then call driver.get(url). When this post was first written in 2017, Selenium 3 had no such method and the only option was to make the browser open a link in a new tab. That trick still works for links on the page, so both are below, followed by how to switch between tabs and close them cleanly. Every Java snippet compiles against Selenium 4.51.0, the current release on Maven Central in October 2026.

How to open a new tab in Selenium 4 (Java)

Call driver.switchTo().newWindow(WindowType.TAB). The driver opens an empty tab and is already pointing at it, so the next driver.get() loads the page there. Use WindowType.WINDOW instead for a separate browser window. Save the original handle first so you can return to it.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;

WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
String originalTab = driver.getWindowHandle();

// Opens a blank tab and switches the driver to it
driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://www.selenium.dev");
System.out.println(driver.getTitle());

// Close the new tab and go back to the first one
driver.close();
driver.switchTo().window(originalTab);

new ChromeDriver() needs no manual chromedriver download: Selenium Manager, bundled with Selenium since 4.6, finds or downloads the matching driver. WindowType.TAB is a hint to the browser. The W3C WebDriver spec says the new browsing context “should” share the current OS window when the browser supports tabs, so in Chrome, Edge and Firefox you get a tab.

To open a link that is already on the page in a new tab, hold Ctrl (Command on macOS) while clicking it with the Actions class. The browser treats it like a user’s Ctrl+click and opens the link’s target in a background tab. The driver does not follow it, so wait for the extra handle and switch to it yourself.

import java.time.Duration;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

String originalTab = driver.getWindowHandle();
Set<String> handlesBefore = driver.getWindowHandles();

WebElement link = driver.findElement(By.cssSelector("a.docs-link"));

// Command on macOS, Control everywhere else
Keys modifier = Platform.getCurrent().is(Platform.MAC) ? Keys.COMMAND : Keys.CONTROL;

new Actions(driver)
    .keyDown(modifier)
    .click(link)
    .keyUp(modifier)
    .perform();

// The new tab opens asynchronously, so wait for it
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.numberOfWindowsToBe(handlesBefore.size() + 1));

// Switch to the handle that was not there before
for (String handle : driver.getWindowHandles()) {
    if (!handlesBefore.contains(handle)) {
        driver.switchTo().window(handle);
        break;
    }
}

Platform.getCurrent() reads the operating system of the machine running the test, which is the browser’s machine only for a local driver. On Selenium Grid or a cloud grid, take the platform from the session instead: ((RemoteWebDriver) driver).getCapabilities().getPlatformName(). If you need a refresher on picking the right locator for link, see how to locate web elements with Selenium WebDriver or clicking a link by its text. The wait above is an explicit wait; the explicit and implicit waits guide explains why it beats a fixed Thread.sleep().

Shift+click opens a link in a new browser window instead of a tab. The pattern is the same as above, with Keys.SHIFT as the modifier, and the same handle comparison afterwards to switch to it.

WebElement link = driver.findElement(By.cssSelector("a.docs-link"));

new Actions(driver)
    .keyDown(Keys.SHIFT)
    .click(link)
    .keyUp(Keys.SHIFT)
    .perform();

The Actions class is the same one used for mouse hover; the hover tutorial covers it in more detail. perform() builds the action chain for you, so the older .build().perform() form is not needed.

How to switch between tabs in Selenium Java

Every tab and window has a string handle. driver.getWindowHandle() returns the current one and driver.getWindowHandles() returns all of them. Save the original handle before you open anything, find the new tab as the handle that was not there before, and switch with driver.switchTo().window(handle). Do not rely on the position in the list.

The 2017 version of this post read the handles into a list and switched to tabs.get(1). That often works, but the W3C WebDriver spec states that “the order in which the window handles are returned is arbitrary”, so a suite that opens several tabs can end up in the wrong one. The handle comparison in the example above avoids it.

To go back to the first tab, switch to the handle you saved:

driver.close();                          // closes the current tab only
driver.switchTo().window(originalTab);   // the driver does not move by itself

The old post used driver.switchTo().defaultContent() for this, which is wrong: defaultContent() leaves an iframe and returns to the main document of the current window. It never changes tabs. After driver.close() you must switch to another handle yourself; any command sent before that fails with NoSuchWindowException. At the end of the test, driver.quit() closes every tab and ends the session.

Why Ctrl+T with sendKeys does not open a new tab

A common Selenium 3 answer was to send Ctrl+T to the page:

String openTab = Keys.chord(Keys.CONTROL, "t");
driver.findElement(By.tagName("body")).sendKeys(openTab);

It usually does nothing. WebDriver’s key actions are delivered to the web page, not to the browser’s own menus and shortcuts, so Ctrl+T, Ctrl+W and Ctrl+Tab never reach the part of the browser that manages tabs. Ctrl+click on a link works because opening a link in a new tab is the page’s own link-handling behaviour. On Selenium 4, use newWindow(WindowType.TAB) and drop the shortcut.

Opening a new tab in Selenium with Python

The Python bindings have the same Selenium 4 method, driver.switch_to.new_window('tab'), and 'window' for a new window. Switching works the same way: save driver.current_window_handle, compare against driver.window_handles, and call driver.switch_to.window(handle).

Once your UI tests pass, you can check how fast the same pages load with the free LoadFocus website speed test.

Frequently Asked Questions

Why do my commands still go to the old tab?

Opening a tab with Ctrl+click or Shift+click does not move the driver. You have to switch to the new window handle explicitly, otherwise every later command runs against the original page. switchTo().newWindow() is the exception: it switches for you.

How do I find the new tab’s handle?

Record driver.getWindowHandles() before opening the tab, wait until the count goes up by one, then switch to the handle that is not in the old set. Relying on the order of the handles is unreliable, because the WebDriver spec leaves that order to the browser.

Does newWindow(WindowType.TAB) work in Selenium 3?

No. switchTo().newWindow() and the WindowType enum arrived in Selenium 4. On Selenium 3 the only options were Ctrl+click on a link or JavaScript. Upgrading to Selenium 4 is the better fix: the last Selenium 3 release, 3.141.59, came out in November 2018.

How do I switch back to the original tab?

Save driver.getWindowHandle() before opening the new tab, then call driver.switchTo().window(originalTab) when you are done. switchTo().defaultContent() does not do this; it only leaves iframes.

Do I need to close the tabs I open?

Yes, or later steps run against the wrong window. Call driver.close() on the tab, then switch back to a saved handle. Leftover tabs also accumulate across a suite and use memory in the browser.

Sources

Bogdan

Founder at LoadFocus

Bogdan builds and runs the tools this blog is about. He writes from what the products actually do in production, including the parts that break.

Weekly tips right in your inbox.

Don’t miss out on the latest testing tips and tricks, tools and news related to software testing.

Already +20,000 members in the list.

The Load Testing Platform with AI That Explains Your Results

Load test websites and APIs from 25 cloud regions, monitor page speed and uptime, and get AI analysis that explains your results in plain English.

Cloud load testing, website speed and API monitoring in one place.

Hi there! Can I share a cool thing I'm working on with you?

A software testing newsletter that sparks joy.

Our goal is to create helpful content for software developers and testers.

If you're not into it, you can unsubscribe instantly.