2 minutes read

If you want to open a new tab in the browser with Selenium WebDriver and Java, below we’ve listed some working examples. Have in mind that at the moment, Selenium WebDriver has no build-in ability to open new tabs or new windows, and because of this we have to force the browser to open the links in new tabs or in new windows. This may be implemented in a later version of Selenium WebDriver.

1. Open an existent link a a new tab using Selenium WebDriver and Actions

More details on how to locate web elements with Selenium WebDriver.


//identify the link in the page, that you want to open in a new tab of your browser's instance
WebElement link = driver.findElement(By.cssSelector(linkLocator));

//this action will click the WebElement with the link identified above, hold CONTROL Key (or COMMAND if you're using Mac), click the WebElement and relese the Keys.CONTROL (use Keys.COMMAND if you're using Mac).
new Actions(driver)
.keyDown(Keys.COMMAND)
.click(link)
.keyUp(Keys.COMMAND)
.build()
.perform();

The code above works in all browsers.

2. Open an existent link a a new tab using Selenium WebDriver with CONTROL/COMMAND Key and T

There is also another option that may not work in Chrome browsers at the moment because of a limitation in the way keyboard inputs are simulated in ChromeDriver. Keys are sent directly to the render process, so any keyboard shortcut in the browser process will not be invoked by sendKeys() method.

See more details





String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");

Keys chord(CharSequence… value) method – Simulates pressing many keys at once in a “chord”. Takes a sequence of Keys.XXXX or strings; appends each of the values to a string, and adds the chord termination key (Keys.NULL) and returns the resultant string. Note: When the low-level webdriver key handlers see Keys.NULL, active modifier keys (CTRL/ALT/SHIFT/etc) release via a keyup event.

The code below will open the link in new Tab.


driver.findElement(By.cssSelector("body")).sendKeys(selectLinkOpeninNewTab);

Then when you open a new tab in your browser, you have to switch to it to be able to work with the newly opened tab’s elements:


List tabs = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));

In order to switch back to the first tab, Selenium WebDriver offers the ability to switch back to the first tab, by executing the following code:


driver.switchTo().defaultContent();

Quite often you may want to open a new tab in the same browser window that is running your Selenium WebDriver tests. Instead of opening a new browser, you can simply use the code below to open a new tab in the same browser:


String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);

You’d need to keep track of the window name(s) or ids to allow switching between them.

3. Open an existent link a a new window using Selenium WebDriver


//identify the link in the page, that you want to open in a new tab of your browser's instance
WebElement link = driver.findElement(By.cssSelector(linkLocator));

//this action will click the WebElement with the link identified above, hold SHIFT Key , click the WebElement and relese the Keys.SHIFT .
new Actions(driver)
.moveToElement(link)
.keyDown(Keys.SHIFT)
.click()
.keyUp(Keys.SHIFT)
.build()
.perform();

How fast is your website comparing to your competitor’s? Check now!

How fast is your website? Free Website Speed Test