How to open a URL in new tab using Selenium Webdriver?

Sometimes, we have to open a URL or link in a new tab but in the same window. This post will look at how we can open a new URL or a link in the same tab using Selenium Webdriver in Java.

We will talk about two versions of selenium and how we achieve this in those two versions using a few easy steps.

Let’s look at them one by one.

Open a URL in a new tab using selenium 4

We will use the selenium 4.1.1 version.

		<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-java</artifactId>
			<version>4.1.1</version>
		</dependency>

Selenium 4 introduced a new function, newWindow(), which makes it easier to open a new tab in the same window.

Below is the syntax to open a new tab using the newWindow() function

driver.switchTo().newWindow(WindowType.TAB);

So, we will first open the https://testkru.com/Elements/TextFields URL and then open the https://google.com/ in a new tab.

mport org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextFields");

		// opening a new tab
		driver.switchTo().newWindow(WindowType.TAB);
		
		// opening another url in the new tab
		driver.get("https://google.com/");
	}
}

The above code will open an URL in the new tab of the same window.

Open a URL in a new tab using selenium 3

selenium 3 doesn’t have the newWindow() function, so we have to do it differently.

We will use JavascriptExecutor to execute a javascript code that will open a link in the new tab, and the below code will do just that.

window.open("https://google.com", "_blank");

We only have to execute the above script using JavascriptExecutor in Selenium, which will open the URL in the new tab.

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextFields");

		// opening the url in the new tab
		JavascriptExecutor jse = (JavascriptExecutor)driver;
		jse.executeScript("window.open(\"https://google.com\", \"_blank\");");
	}
}

This is it. We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Related Articles –
Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *