Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:


1. Introduction

Selenium WebDriver is a popular tool for automating web testing with many functionalities. One of the common actions when automating the web is to open a new tab in the browser window.

Opening a new tab can be useful in various scenarios, including testing multi-page workflows, verifying external links that open in a new tab, interacting with pop-up windows, and simulating multiple users interacting with different parts of the application simultaneously when running tests in parallel.

Earlier solutions were custom scripts like sending a combination of keys, such as “Ctrl” + “T,” which usually led to different results depending on the browser and operating system.

In this tutorial, we’ll explore stable approaches for opening a new tab – the newWindow() API introduced in Selenium 4 and JavaScript code execution.

2. Using the newWindow() API

Selenium 4 introduced a powerful and flexible API method newWindow(), for creating a new window in the current browser session. It allows us to open a new browser tab and switch to it automatically. This method takes a WindowType parameter WINDOW or TAB and creates it. The syntax is pretty straightforward:


3. Using JavaScript

Another approach to open a new tab with Selenium WebDriver is executing JavaScript code. It involves using the executeScript() method of the JavascriptExecutor interface, which allows us to run JavaScript code directly within the browser. The script is useful when we want more control over the new tab, such as specifying the URL to open.

Here’s how to use this approach to open a new tab:

((JavascriptExecutor) driver).executeScript("");

And the how to open a new tab with URL:

((JavascriptExecutor) driver).executeScript("'')");

It’s important to keep in mind that the driver will still be focused on the original tab after the method is executed. In order to interact with elements on the new tab, we need to switch the driver’s focus to that tab using the driver.switchTo().window() method.

Here’s an example of switching to the new tab after opening it with JavaScript:

String newTab = driver.getWindowHandles()
  .filter(handle -> !handle.equals(originalTab))

4. Conclusion

In this article, we explored two approaches to open a new tab with Selenium: the newWindow() method introduced in Selenium 4 and the method executed with JavaScript.

The newWindow() method is a new API introduced in Selenium 4 that makes creating a new tab or window simple and intuitive. On the other hand, executing with JavaScript provides greater control over how the new tab is opened and can be used with earlier versions of Selenium. However, it may require more code and be more difficult to use, particularly for beginners.

As always, the code examples are available on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.