How can I open a new tab in the existing Firefox browser using Selenium WebDriver (a.k.a. Selenium 2) in Java?
-
2I use java. I got one solution "m_Driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");" and its working totally fine. – Bhakti Shah Jul 09 '13 at 12:24
-
Are you then able to control the new tab? Selenium has no support for new tabs (it tries to open new windows instead). See my answers http://stackoverflow.com/questions/14550360/selenium-ide-for-firefox-ctrl-tab/14552367#14552367 and http://stackoverflow.com/questions/17225911/robot-framework-verify-a-new-browser-tab-was-opened/17233198#17233198. What exactly are you trying to achieve? – Petr Janeček Jul 09 '13 at 21:48
-
I wanted to copy some text and then open a new tab and paste the same text in the new tab. I have not tried to control the new tab but it works fine for what i want to achieve. – Bhakti Shah Jul 10 '13 at 04:48
-
I did the same thing by using GetText() of that element and then did Driver.get(text). – Bhakti Shah Jul 10 '13 at 04:49
-
Do you want to open empty Tab? or, do you want to open a Tab with clicking any link or button? – Ripon Al Wasim Feb 25 '16 at 13:03
-
Note that the Top 20 or so answers as of now are out of date. I've added an example of WebDrivers new(ish) native tabs below: https://stackoverflow.com/a/60731604/4494 – Matthias Winkelmann Mar 17 '20 at 23:30
-
I'll post the link to my previous answer, [link](https://stackoverflow.com/a/72373996/8766115) – V1rus_Falcon May 26 '22 at 08:32
36 Answers
Just for anyone else who's looking for an answer in Ruby, Python, and C# bindings (Selenium 2.33.0).
Note that the actual keys to send depend on your OS. For example, Mac uses CMD + T, instead of Ctrl + T.
Ruby
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox
driver.get('http://stackoverflow.com/')
body = driver.find_element(:tag_name => 'body')
body.send_keys(:control, 't')
driver.quit
Python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://stackoverflow.com/")
body = driver.find_element_by_tag_name("body")
body.send_keys(Keys.CONTROL + 't')
driver.close()
C#
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace StackOverflowTests {
class OpenNewTab {
static void Main(string[] args) {
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://stackoverflow.com/");
IWebElement body = driver.FindElement(By.TagName("body"));
body.SendKeys(Keys.Control + 't');
driver.Quit();
}
}
}

- 30,738
- 21
- 105
- 131

- 32,020
- 13
- 97
- 125
-
3
-
13WebDriver can handle more than one tab - it simple treats them as seperate windows. So you only need to `driver.switchTo().window(windowName);` to access any tab or window. Obviously, you'd need to keep track of the window name(s) as normal to allow switching between them. – Mark Rowlands Jul 16 '13 at 15:42
-
1Could you explain why you're retrieving an element and Sending the Ctrl-t to that element? That doesn't make any sense ... you can't "Ctrl+t on a web element"? Why not just run new Actions(WebDriver) .SendKeys(Keys.Control + "w") .Perform(); Please could someone explain? – Brondahl Oct 01 '14 at 17:37
-
2This doesn't work for me. I have tried several different ways of pressing ctrl+t, none of which succeed in opening a new tab. – Thayne Nov 24 '14 at 21:26
-
4@Thayne a [ChromeDriver bug](https://code.google.com/p/chromedriver/issues/detail?id=903) prevents CTRL+T from working. You can use CTRL+LMB as a workaround. See https://code.google.com/p/chromedriver/issues/detail?id=903#c2 for sample code. – Gili Nov 28 '14 at 16:14
-
1Just add the example of python code to use CTRL+LMB. `ActionChains(driver).key_down(Keys.CONTROL).click(element_to_be_clicked).key_up(Keys.CONTROL).perform()`. The *ActionChains* is imported from `selenium.webdriver.common.action_chains`. – JavaNoScript Aug 01 '16 at 16:28
-
If you need more details about how to use switchTo() - https://www.guru99.com/handling-iframes-selenium.html – Mauricio Gracia Gutierrez Aug 25 '22 at 20:02
-
The code below will open the link in a new tab.
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);
The code below will open an empty new tab.
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t");
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);

- 30,738
- 21
- 105
- 131

- 756
- 6
- 2
-
9
-
3This solution seems less-than-optimal since it depends on OS-specific (and perhaps browser-specific) shortcuts... At least, if you use something like JS `window.open()`, you can expect it to work on many platforms/browsers. – mkasberg May 30 '18 at 21:45
-
2Problem is `window.open()` might open a new window instead of ne tab. It does so on Firefox in test mode. At least when run from Katalon (which uses Selenium under the hood). – Nux Sep 03 '20 at 09:06
-
@Nux same for me in Firefox. Is there any workaround for that to open a tab? – excelsiorious Feb 08 '22 at 14:41
Why not do this
driver.ExecuteScript("window.open('your url','_blank');");

- 5,736
- 1
- 35
- 36
-
1
-
1probably safe for testing where you are in control of your browser settings – Jay Byford-Rew Aug 25 '15 at 14:21
-
1would probably work great. chromedriver seems to disable all extensions(ie. adblock) – Josh O'Bryan Aug 31 '15 at 20:17
-
Works great. In chromedriver this is actually opening new tabs for me, which I like better. It's probably because of the settings of my real Chrome. – gligoran Oct 27 '15 at 11:20
-
4The send keys method no longer work for me after updated to Chrome 49.0.2623.75m, and this method works on that Chrome version. – Steven Mar 03 '16 at 17:20
-
7If you want to open a blank page in the tab, you could `driver.ExecuteScript("window.open('about:blank','_blank');");` – Steven Mar 03 '16 at 18:13
-
The above code shows ExecuteScript- not found symbol. so pls **update** - ((JavascriptExecutor)driver).executeScript("window.open('https://www.google.com/','_blank');"); – Rohan Khude Oct 16 '16 at 18:02
To open new tab using JavascriptExecutor,
((JavascriptExecutor) driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
Will control on tab as according to index:
driver.switchTo().window(tabs.get(1));
Driver control on main tab:
driver.switchTo().window(tabs.get(0));

- 3,955
- 2
- 27
- 51
-
How about launching 3 URL's, ? `driver.switchTo().window(tabs.get(1)); driver.get("https://www.stackoverflow.com"); Thread.sleep(2000); driver.switchTo().window(tabs.get(2)); driver.get("https://www.flipkart.com"); Thread.sleep(2000); driver.close(); driver.switchTo().window(tabs.get(1)); Thread.sleep(2000); driver.close(); driver.switchTo().window(tabs.get(0));` , I have tried this but getting array out of bound exception, If u know any solution please let me know. – cruisepandey Dec 27 '18 at 10:32
-
It work similarly for 3rd tab also. May I know where exactly exception throws(On driver close)? – Ishita Shah Dec 27 '18 at 11:12
-
No, at this line `driver.switchTo().window(tabs.get(2));`, This works fine `((JavascriptExecutor) driver).executeScript("window.open('https://www.stackoverflow.com','_blank');"); Thread.sleep(3000); ((JavascriptExecutor) driver).executeScript("window.open('https://www.flipkart.com','_blank');");` but I will not have any control to switch to windows. – cruisepandey Dec 27 '18 at 13:03
As of selenium >= 4.0, there is no need for javascript or send_keys
workarounds. Selenium 4 provides a new API called newWindow
that lets you create a new window (or tab) and automatically switches to it. Since the new window or tab is created in the same session, it avoids creating a new WebDriver object.
Python
Open new tab
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.window import WindowTypes
driver.switch_to.new_window(WindowTypes.TAB)
Open new window
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.window import WindowTypes
driver.switch_to.new_window(WindowTypes.WINDOW)
Java
Open new window
driver.get("https://www.google.com/");
// Opens a new window and switches to new window
driver.switchTo().newWindow(WindowType.WINDOW);
// Opens LambdaTest homepage in the newly opened window
driver.navigate().to("https://www.lambdatest.com/");
Open new tab
driver.get("https://www.google.com/");
// Opens a new window and switches to new window
driver.switchTo().newWindow(WindowType.TAB);
// Opens LambdaTest homepage in the newly opened tab
driver.navigate().to("https://www.lambdatest.com/")

- 57,267
- 35
- 174
- 180
You can use the following code using Java with Selenium WebDriver:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
By using JavaScript:
WebDriver driver = new FirefoxDriver(); // Firefox or any other Driver
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.open()");
After opening a new tab it needs to switch to that tab:
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));

- 30,738
- 21
- 105
- 131

- 36,924
- 42
- 155
- 176
-
The manual operation Ctrl+T to open a new tab has been implemented here – Ripon Al Wasim Apr 27 '16 at 11:15
-
To open a new window in Chrome Driver.
// The script that will will open a new blank window
// If you want to open a link new tab, replace 'about:blank' with a link
String a = "window.open('about:blank','_blank');";
((JavascriptExecutor)driver).executeScript(a);

- 30,738
- 21
- 105
- 131

- 520
- 6
- 15
Almost all answers here are out of date.
(Ruby examples)
WebDriver now has support for opening tabs:
browser = Selenium::WebDriver.for :chrome
new_tab = browser.manage.new_window
Will open a new tab. Opening a window has actually become the non-standard case:
browser.manage.new_window(:window)
The tab or window will not automatically be focussed. To switch to it:
browser.switch_to.window new_tab

- 57
- 1
- 9

- 15,870
- 7
- 64
- 76
-
1Is this usable with Selenium 4+ only? The [docs](https://www.selenium.dev/documentation/en/webdriver/browser_manipulation/#switching-windows-or-tabs)) mention that "Selenium 4 provides a new api NewWindow which creates a new tab (or) new window and automatically switches to it.". I is not entirely obvious from that quote whether opening a new window / tab alone (without automatically switching to it) existed as part of the pre-v4 API. – lolotobg Dec 02 '20 at 13:54
-
Also bare in mind that there are [APIs for getting a handle](https://www.selenium.dev/documentation/en/webdriver/browser_manipulation/#get-window-handle) to the current window / tab (`original_window = driver.window_handle` in Ruby) before opening the new one, as well as [restoring the focus](https://www.selenium.dev/documentation/en/webdriver/browser_manipulation/#closing-a-window-or-tab) to the original tab after you are done (`driver.close` followed by a `driver.switch_to.window original_window`). – lolotobg Dec 02 '20 at 13:59
Try this for the Firefox browser.
/* Open new tab in browser */
public void openNewTab()
{
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(0));
}

- 30,738
- 21
- 105
- 131

- 137
- 1
- 8
To open a new tab in the existing Chrome browser using Selenium WebDriver you can use this code:
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
string newTabInstance = driver.WindowHandles[driver.WindowHandles.Count-1].ToString();
driver.SwitchTo().Window(newTabInstance);
driver.Navigate().GoToUrl(url);

- 1,494
- 1
- 13
- 27

- 41
- 1
-
Yes this works nice open a new url in same tab of existing url, good for lot's of link tests because it doesn't use a lot of resources. – JWP May 16 '16 at 16:07
-
An explanation would be in order. How is it different from previous answers? – Peter Mortensen Dec 05 '20 at 12:03
The below code will open the link in a new window:
String selectAll = Keys.chord(Keys.SHIFT, Keys.RETURN);
driver.findElement(By.linkText("linkname")).sendKeys(selectAll);

- 30,738
- 21
- 105
- 131

- 756
- 6
- 2
I had trouble opening a new tab in Google Chrome for a while.
Even driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
didn't work for me.
I found out that it's not enough that Selenium has focus on driver. Windows also has to have the window in the front.
My solution was to invoke an alert in Chrome that would bring the window to front and then execute the command. Sample code:
((JavascriptExecutor)driver).executeScript("alert('Test')");
driver.switchTo().alert().accept();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");

- 30,738
- 21
- 105
- 131

- 61
- 4
// To open a new tab in an existing window
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");

- 30,738
- 21
- 105
- 131

- 21
- 1
How can we open a new, but more importantly, how do we do stuff in that new tab?
Webdriver doesn't add a new WindowHandle for each tab, and only has control of the first tab. So, after selecting a new tab (Control + Tab Number) set .DefaultContent() on the driver to define the visible tab as the one you're going to do work on.
Visual Basic
Dim driver = New WebDriver("Firefox", BaseUrl)
' Open new tab - send Control T
Dim body As IWebElement = driver.FindElement(By.TagName("body"))
body.SendKeys(Keys.Control + "t")
' Go to a URL in that tab
driver.GoToUrl("YourURL")
' Assuming you have m tabs open, go to tab n by sending Control + n
body.SendKeys(Keys.Control + n.ToString())
' Now set the visible tab as the drivers default content.
driver.SwitchTo().DefaultContent()

- 30,738
- 21
- 105
- 131

- 1,900
- 2
- 20
- 34
I am using Selenium 2.52.0 in Java and Firefox 44.0.2. Unfortunately none of the previous solutions worked for me.
The problem is if I a call driver.getWindowHandles() I always get one single handle. Somehow this makes sense to me as Firefox is a single process and each tab is not a separate process. But maybe I am wrong. Anyhow, I try to write my own solution:
// Open a new tab
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
// URL to open in a new tab
String urlToOpen = "https://url_to_open_in_a_new_tab";
Iterator<String> windowIterator = driver.getWindowHandles()
.iterator();
// I always get handlesSize == 1, regardless how many tabs I have
int handlesSize = driver.getWindowHandles().size();
// I had to grab the original handle
String originalHandle = driver.getWindowHandle();
driver.navigate().to(urlToOpen);
Actions action = new Actions(driver);
// Close the newly opened tab
action.keyDown(Keys.CONTROL).sendKeys("w").perform();
// Switch back to original
action.keyDown(Keys.CONTROL).sendKeys("1").perform();
// And switch back to the original handle. I am not sure why, but
// it just did not work without this, like it has lost the focus
driver.switchTo().window(originalHandle);
I used the Ctrl + T combination to open a new tab, Ctrl + W to close it, and to switch back to original tab I used Ctrl + 1 (the first tab).
I am aware that mine solution is not perfect or even good and I would also like to switch with driver's switchTo call, but as I wrote it was not possible as I had only one handle. Maybe this will be helpful to someone with the same situation.

- 30,738
- 21
- 105
- 131

- 1,276
- 2
- 15
- 30
How to open a new tab using Selenium WebDriver with Java for Chrome:
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-extensions");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.navigate().to("https://google.com");
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_T);
The above code will disable first extensions and using the robot class, a new tab will open.

- 30,738
- 21
- 105
- 131

- 101
- 6
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(0));

- 7,968
- 13
- 40
- 42

- 15
- 4
Check this complete example to understand how to open multiple tabs and switch between the tabs and at the end close all tabs.
public class Tabs {
WebDriver driver;
Robot rb;
@BeforeTest
public void setup() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Anuja.AnujaPC\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://qaautomated.com");
}
@Test
public void openTab() {
// Open tab 2 using CTRL + T keys.
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
// Open URL In 2nd tab.
driver.get("http://www.qaautomated.com/p/contact.html");
// Call switchToTab() method to switch to the first tab
switchToTab();
// Call switchToTab() method to switch to the second tab.
switchToTab();
}
public void switchToTab() {
// Switching between tabs using CTRL + tab keys.
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
// Switch to current selected tab's content.
driver.switchTo().defaultContent();
}
@AfterTest
public void closeTabs() throws AWTException {
// Used Robot class to perform ALT + SPACE + 'c' keypress event.
rb = new Robot();
rb.keyPress(KeyEvent.VK_ALT);
rb.keyPress(KeyEvent.VK_SPACE);
rb.keyPress(KeyEvent.VK_C);
}
}
This example is given by this web page.

- 30,738
- 21
- 105
- 131

- 1,367
- 13
- 19
This line of code will open a new browser tab using Selenium WebDriver:
((JavascriptExecutor)getDriver()).executeScript("window.open()");

- 30,738
- 21
- 105
- 131

- 31
- 3
-
When I ran above code , in chrome its opened a new tab. However in firefox a new window is opened. In both the cases window handler is different for newly opened ones. So I think a tab is more or less same as a window. Of course, incognito window is definitely different from regular tab of a window. – MamathaMacherla Sep 20 '19 at 09:39
Java
I recommend using JavascriptExecutor
:
- Open new blank window:
((JavascriptExecutor) driver).executeScript("window.open()");
- Open new window with specific URL:
((JavascriptExecutor) driver).executeScript("window.open('https://google.com')");
Following import:
import org.openqa.selenium.JavascriptExecutor;

- 30,738
- 21
- 105
- 131

- 7,295
- 6
- 20
- 45
There are 3 ways to do this. In below example I am doing following steps to open the facebook in new tab,
- Launching
https://google.com
- Searching for
facebook
text and getting thefacebook
URL
- Opening
facebook
in different tab.
Solution#1: Using window handles
.
driver = new ChromeDriver(options);
driver.get("https://www.google.com/search?q=facebook");
String facebookUrl = driver.findElement(By.xpath("(//a[contains(@href,'facebook.com')])[1]")).getAttribute("href");
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get(facebookUrl);
Solution#2: By creating new driver
instance. It's not recommended but it is also a possible way to do this.
driver = new ChromeDriver(options);
driver.get("https://www.google.com/search?q=facebook");
String facebookUrl = driver.findElement(By.xpath("(//a[contains(@href,'facebook.com')])[1]")).getAttribute("href");
/*Create an another instance of driver.*/
driver = new ChromeDriver(options);
driver.get(facebookUrl);
Solution#3: Using Selenium 4
driver.get("https://www.google.com/search?q=facebook");
String facebookUrl = driver.findElement(By.xpath("(//a[contains(@href,'facebook.com')])[1]")).getAttribute("href");
driver.switchTo().newWindow(WindowType.TAB);
driver.navigate().to(facebookUrl);

- 2,702
- 1
- 12
- 23
If you need to open a specific link in a new tab, you can send the shortcut directly to the link like so:
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL, Keys.RETURN);
driver.findElement(By.xpath("//a[strong[.='English']]")).sendKeys(selectLinkOpeninNewTab);
If you need to open a new tab and want to choose which link is opened immediately in said tab, you can use JavaScriptExecutor like so:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.open('https://www.wikipedia.org/', '_blank');");

- 96
- 5
In new versions of selenium for java, they added a method to create a new tab:
driver.switchTo().newWindow(WindowType.TAB);
or a new window:
driver.switchTo().newWindow(WindowType.WINDOW);
here is the link to an example on the official repo

- 21
- 3
Actions at=new Actions(wd);
at.moveToElement(we);
at.contextClick(we).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();

- 2,317
- 3
- 31
- 44

- 340
- 1
- 3
- 12
-
In the future, provide some explanation text along with the code snippet to make your posts easier to understand. – Tyler Jandreau Apr 05 '16 at 14:35
To open a new tab in the existing Firefox browser using Selenium WebDriver
FirefoxDriver driver = new FirefoxDriver();
driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL,"t");

- 2,008
- 2
- 20
- 27
The same example for Node.js:
var webdriver = require('selenium-webdriver');
...
driver = new webdriver.Builder().
withCapabilities(capabilities).
build();
...
driver.findElement(webdriver.By.tagName("body")).sendKeys(webdriver.Key.COMMAND + "t");

- 30,738
- 21
- 105
- 131

- 1,753
- 4
- 23
- 31
Due to a bug in https://bugs.chromium.org/p/chromedriver/issues/detail?id=1465 even though webdriver.switchTo actually does switch tabs, the focus is left on the first tab.
You can confirm this by doing a driver.get after the switchWindow and see that the second tab actually go to the new URL and not the original tab.
A workaround for now is what yardening2 suggested. Use JavaScript code to open an alert and then use webdriver to accept it.

- 30,738
- 21
- 105
- 131

- 61
- 1
- 4
This code is working for me (Selenium 3.8.1, chromedriver 2.34.522940, and Chrome 63.0):
public void openNewTabInChrome() {
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.linkText("Gmail"));
Actions actionOpenLinkInNewTab = new Actions(driver);
actionOpenLinkInNewTab.moveToElement(element)
.keyDown(Keys.CONTROL) // MacOS: Keys.COMMAND
.keyDown(Keys.SHIFT).click(element)
.keyUp(Keys.CONTROL).keyUp(Keys.SHIFT).perform();
ArrayList<String> tabs = new ArrayList(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get("http://www.yahoo.com");
//driver.close();
}

- 30,738
- 21
- 105
- 131

- 534
- 7
- 15
Question: How can I open a new tab using Selenium WebDriver with Java?
Answer: After a click on any link, open a new tab.
If we want to handle a newly open tab then we have need to handle tab using the .switchTo().window() command.
Switch to a particular tab, and then perform an operation and switch back to into the parent tab.
package test;
import java.util.ArrayList;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Tab_Handle {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "geckodriver_path");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
// Store all currently open tabs in Available_tabs
ArrayList<String> Available_tabs = new ArrayList<String>(driver.getWindowHandles());
// Click on link to open in new tab
driver.findElement(By.id("Url_Link")).click();
// Switch newly open Tab
driver.switchTo().window(Available_tabs.get(1));
// Perform some operation on Newly open tab
// Close newly open tab after performing some operations.
driver.close();
// Switch to old(Parent) tab.
driver.switchTo().window(Available_tabs.get(0));
}
}

- 30,738
- 21
- 105
- 131

- 125
- 1
- 5
Selenium doesn't support opening new tabs. It only supports opening new windows. For all intents and purposes a new window is functionally equivalent to a new tab anyway.
There are various hacks to work around the issue, but they are going to cause you other problems in the long run.

- 30,738
- 21
- 105
- 131

- 7,281
- 26
- 49
If you want to open the new tab you can use this
((JavascriptExecutor) getDriver()).executeScript("window.open()");
If you want to open the link from the new tab you can use this
With JavascriptExecutor:
public void openFromNewTab(WebElement element){
((JavascriptExecutor)getDriver()).executeScript("window.open('"+element.getAttribute("href")+"','_blank');");
}
With Actions:
WebElement element = driver.findElement(By.xpath("your_xpath"));
Actions actions = new Actions(driver);
actions.keyDown(Keys.LEFT_CONTROL)
.click(element)
.keyUp(Keys.LEFT_CONTROL)
.build()
.perform();

- 30,738
- 21
- 105
- 131

- 1,737
- 1
- 12
- 26
Use below snippet to open link in new tab:
public void openLinkInNewTab(String link){
String currentHandle = driver().getWindowHandle();
((JavascriptExecutor) driver()).executeScript("window.open()");
//getting all the handles currently available
Set<String> handles = driver().getWindowHandles();
for (String actual : handles) {
if (!actual.equalsIgnoreCase(currentHandle)) {
//switching to the opened tab
driver().switchTo().window(actual);
//opening the URL saved.
driver.get(link);
}
}
}

- 1,531
- 11
- 11
-
in selenium 4 we have simple way to navigate to new tab https://medium.com/@vasistatvn/selenium-4-new-features-with-practical-examples-2-a00751c0a978 – Vasista TVN Jun 16 '22 at 13:26
Java,
Hi you can try the following code for opening a new tab in selenium java.
public static void switchToWindow(WebDriver driver) {
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
}

- 11
- 3
-
1Welcome to Stack Overflow. This question is 8.5 years old and already has nearly 40 answers. Does this answer bring something new that the other answers don't? If so, please explain how it improves upon what is already here. – ChrisGPT was on strike Jan 17 '22 at 23:27
You can get around the Control-T keyboard shortcut bug in the selenium driver that was described in a comment above with this simple line of code to execute javascript to open a new tab, then you will have to switch the control to that one.
driver.execute_script("window.open();")
Here is a simple example:
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
time.sleep(0.5)
driver.execute_script("window.open();")
window_after = driver.window_handles[-1]
time.sleep(0.5)
driver.switch_to.window(window_after)
driver.get("https://google.com")

- 6,222
- 2
- 31
- 46
This is a purely selenium function from Selenium latest version. Try this if you are working with such specification.
For input, the function requires the current driver employed and current Window Handle string (This is just to make sure you have one window open before going to new tab.). The return string is for the new Window handle.
public static String switchToNewTab(WebDriver driver, String sCurrentWindowHandle) {
String newTabHandle = null;
try {
driver = driver.switchTo().newWindow(WindowType.TAB);
newTabHandle = driver.getWindowHandle();
} catch (Exception e) {
e.printStackTrace();
}
return newTabHandle;
}

- 55
- 2
- 9
Handling a browser window using Selenium WebDriver:
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()) // Switch to new opened window
{
driver.switchTo().window(winHandle);
}
driver.switchTo().window(winHandleBefore); // Move to previously opened window

- 30,738
- 21
- 105
- 131

- 2,603
- 5
- 18
- 29
-
Fails to address the question. You answer is about switching to an already existing window, OP asked how to open a new one. – Thomas Hirsch Mar 13 '18 at 12:28