How to use newWindow API In Java
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.
// 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.w3wiki.org/");
// Maximize the screen
driver.manage().window().maximize();
// Open a new tab using newWindow API
driver.switchTo().newWindow(WindowType.TAB);
}
}
Output:
How to open a new tab using Selenium WebDriver in Java?
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.
Table of Content
- Opening a New tab using Selenium WebDriver in Java
- Using newWindow API
- Using JavascriptExecutor
- Frequently Asked Questions on How to Open a New Tab using Selenium WebDriver in Java?