How to open a new tab using Selenium WebDriver?

How to open a new tab in an existing Firefox browser using Selenium WebDriver (aka Selenium 2)?

+87
java firefox selenium selenium-webdriver browser-tab
Jul 09 '13 at 11:47 on
source share
26 answers

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); 

In the code below, an empty new tab will open.

 String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,"t"); driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab); 
+42
Oct 18 '13 at 4:54 on
source share

Just for those looking for an answer in Ruby / Python / C # bindings (Selenium 2.33.0).

Note that the actual keys to send depend on your OS, for example, Mac uses COMMAND + t instead of CONTROL + 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() 

FROM#

 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(); } } } 
+78
Jul 09 '13 at 21:50
source share

Why not do it

 driver.ExecuteScript("window.open('your url','_blank');"); 
+24
Nov 29 '14 at
source share

To open a new window in the 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); 

To switch between tabs, read here.

+7
Mar 26 '17 at 7:43 on
source share

You can use the following code using Java with Selenium WebDriver:

 driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t"); 

Using JavaScript:

 WebDriver driver = new FirefoxDriver();//FF or any other Driver JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("window.open()"); 
+6
Jun 06 '14 at 6:00 a.m.
source share

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)); } 
+5
Aug 01 '14 at 14:22
source share

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); 
+3
Oct 18 '13 at 5:00
source share

To open a new tab using the JavascriptExecutor,

 ((JavascriptExecutor) driver).executeScript("window.open()"); ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles()); driver.switchTo().window(tabs.get(1)); 

Will control on the tab according to the index:

 driver.switchTo().window(tabs.get(1)); 

Driver management on the main tab:

 driver.switchTo().window(tabs.get(0)); 
+3
Aug 08 '18 at 10:02
source share

To open a new tab in an 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); 
+2
Nov 24 '15 at 7:24
source share

I was not able to open a new tab in chrome. Even driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t"); Not working for me.

I learned that it is not enough for selenium to focus on the driver, Windows should also have a window in front.

My solution was to trigger a warning on chrome that would bring the window forward 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"); 
+2
Mar 22 '16 at 8:16
source share
 //to open new tab in existing window driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t"); 
+1
Aug 12 '14 at 11:20
source share

I am using Selenium 2.52.0 in Java and Firefox 44.0.2. Unfortunately, none of the above solutions worked for me. The problem is that if I call driver.getWindowHandles (), I always get one single descriptor. Somehow it makes sense to me, since Firefox is the only process, and each tab is not a separate process. But maybe I'm wrong. In any case, I'm trying 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 combination Ctrl + t to open a new tab, Ctrl + w to close it, and to return to the original tab, I used Ctrl + 1 (first tab). I know that my solution is not perfect or even good, and I would also like to switch using switch switch to call, but as I wrote, this was not possible, since I had only one descriptor. Perhaps this will be useful for someone with the same situation.

+1
Mar 05 '16 at 22:02
source share

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); 

Above the code will disable the first extensions and a new tab of the robot class will open.

+1
Aug 17 '16 at 13:46 on
source share
  Actions at=new Actions(wd); at.moveToElement(we); at.contextClick(we).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform(); 
0
Oct 28 '14 at 11:21
source share

To open a new tab in an existing Firefox browser using Selenium WebDriver

 FirefoxDriver driver = new FirefoxDriver(); driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL,"t"); 
0
May 7 '15 at 6:41
source share

Same example for nodejs:

 var webdriver = require('selenium-webdriver'); ... driver = new webdriver.Builder(). withCapabilities(capabilities). build(); ... driver.findElement(webdriver.By.tagName("body")).sendKeys(webdriver.Key.COMMAND + "t"); 
0
Aug 15 '15 at 17:04
source share

How to open new, but more importantly, how do you make material on this new tab? Webdriver does not add a new WindowHandle for each tab and only has control over the first tab. Thus, after selecting a new tab (Control + Tab Number), install the .DefaultContent () driver in the driver to determine the visible tab as the one you are going to 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() 
0
Sep 02 '15 at 23:31
source share
 driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t"); ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles()); driver.switchTo().window(tabs.get(0)); 
0
Dec 06 '16 at 12:31 on
source share

check out this complete example to understand how to open multiple tabs and switch between tabs and close all tabs at the end

 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 1st tab switchToTab(); //Call switchToTab() method to switch to 2nd 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 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 represented by this web page.

0
Jan 11 '17 at 8:17 on
source share

Due to an error in https://bugs.chromium.org/p/chromedriver/issues/detail?id=1465 , although webdriver.switchTo actually switches tabs, the focus remains on the first tab. You can confirm this by doing driver.get after switchWindow and seeing that the second tab is actually navigating to the new URL and not to the original tab.

Now work around is what @ yardening2 suggested. Use js to open a warning, and then use webdriver to accept it.

0
Apr 07 '17 at 15:03
source share

This code works for me (selenium 3.8.1, chromedriver = 2.34.522940, 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(); } 
0
Jan 12 '18 at 16:35
source share

Question : How to open a new tab using Selenium WebDriver with Java?

Answer : After clicking on any link, open a new tab.

If we want to process a newly opened tab, we need to process the tab using the .switchTo () command. Window ().

Switch to a specific tab, then perform the operation and return to 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)); } } 
0
Apr 04 '18 at 11:25
source share

Selenium does not support opening new tabs, it only supports opening new windows. In any case, the new window is in any case functionally equivalent to the new tab.

There are various ways to solve this problem, but they can cause other problems in the long run.

0
Mar 09 '19 at 19:50
source share

This line of code will open a new browser tab using selenium webdriver

 ((JavascriptExecutor)getDriver()).executeScript("window.open()"); 
0
May 22 '19 at 10:04
source share
 driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");// open in new tab driver.get("ur link"); 
-one
Dec 30 '15 at 8:02
source share

Processing 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 
-one
Jan 11 '17 at 8:31 on
source share



All Articles