How to open a new tab using Selenium WebDriver in Java?
Last Updated :
23 Jul, 2025
An object-oriented platform-independent language that is used for creating applications is known as Java. Sometimes the applications are so robust that we need some automation to test their application. This can be done using various automation tools such as Selenium. While doing automation, there also occurs the need to open a new tab using the Selenium web driver in Java.
In this article, we will discuss the same.
Opening a New tab using Selenium WebDriver in Java
We can open a new tab using Selenium WebDriver in Java by 2 methods given below:
- Using newWindow API
- Using JavascriptExecutor
Using newWindow API
Selenium allows the web driver to open a new tab and switch to that tab automatically using switchTo and newWindow functions. Here, we will use both functions simultaneously to open a new tab and switch to that.
- switchTo: The function which allows the webdriver to switch from one tab to another is known as ttswitchTo function.
- newWindow: The function that lets the user create a new window in the current browser session is known as newWindow.
Syntax
driver.switchTo().newWindow(WindowType.TAB);
Example of newWindow API
In this example, we have imported the WebDriver, ChromeDriver, and WindowType modules. Further, we have opened the Geeks For Geeks website (link) and then we have opened a new tab using the newWindow API.
Java
// Import selenium libraries
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
public class selenium2 {
public static void main(String[] args)
{
// State the chromedriver URL
System.setProperty(
"webdriver.chrome.driver",
"C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
// Define and initiate the chrome driver
WebDriver driver = new ChromeDriver();
// Open the Geeks For Geeks website
driver.get("https://www.geeksforgeeks.org/");
// Maximize the screen
driver.manage().window().maximize();
// Open a new tab using newWindow API
driver.switchTo().newWindow(WindowType.TAB);
}
}
Output:

Using JavascriptExecutor
The module that allows the web driver to execute Javascript code within the current browser is known as JavascriptExecutor. The new tab can be opened using executeScript and window. open functions respectively.
- executeScript: The function that allows the user to execute JavaScript code within the context of the current browser window or frame is known as executeScript.
- window.open(): The function that is used to open a new browser window is known as a window.open() function.
There are Two Ways:
- Open a new tab
- Open a new tab with a specific URL
1. Open a new tab
When the user doesn't provide any arguments to the window.open() function, then just a new tab is opened.
Syntax
((JavascriptExecutor) driver).executeScript("window.open()");
Example of Open a new tab
In this example, we have imported the WebDriver, ChromeDriver, and JavascriptExecutor modules. Further, we have opened the Geeks For Geeks website (link) and then we have opened a new tab using JavascriptExecutor.
Java
//Import selenium libraries
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class selenium2 {
public static void main(String[] args) {
// State the chromedriver URL
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
// Define and initiate the chrome driver
WebDriver driver = new ChromeDriver();
// Open the Geeks For Geeks website
driver.get("https://www.geeksforgeeks.org/");
// Maximize the screen
driver.manage().window().maximize();
// Open a new tab using Javascript
((JavascriptExecutor) driver).executeScript("window.open()");
}
}
Output

2. Open a new tab with a specific URL
When the user doesn't provide a URL as the argument to the window.open() function, then a new tab with that specific URL is opened.
Syntax
((JavascriptExecutor) driver).executeScript("window.open('URL_of_webpage')");
Here, URL_of_webpage: It is the URL of the webpage which the user wants to open in a new tab.
Example of Open a new tab with a specific URL
In this example, we have imported the WebDriver, ChromeDriver, and JavascriptExecutor modules. Further, we have opened the Geeks For Geeks website (link) and then we have opened a new tab with a specific URL (link) using JavascriptExecutor.
Java
//Import selenium libraries
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class selenium2 {
public static void main(String[] args) {
// State the chromedriver URL
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Vinayak Rai\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");
// Define and initiate the chrome driver
WebDriver driver = new ChromeDriver();
// Open the Geeks For Geeks website
driver.get("https://www.geeksforgeeks.org/");
// Maximize the screen
driver.manage().window().maximize();
// Open a new tab with specific URL using Javascript
((JavascriptExecutor) driver).executeScript("window.open('https://www.geeksforgeeks.org/explore?page=1&sortBy=submissions&itm_source=geeksforgeeks&itm_medium=main_header&itm_campaign=practice_header')");
}
}
Output

Conclusion
The opening of a new tab is a very important feature during automation and enhances the capability of automated testing. If the user wants to open a new tab with a specific URL, then opening a new tab with a specific URL using the JavascriptExecutor method is preferred. I hope after reading the above article, you will able to open a new tab using Selenium webdriver in Java.
Similar Reads
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING