top of page
  • Writer's picture9to5 Technologies

Open new link in a new tab or window and switch to it with Selenium WebDriver

Sometimes we need to open links in new Tab ow Window in selenium, here is the code for your help:

1. Open 2. Open a new tab and switch to it 3. Open in the new tab

Here is the solution for Chrome:


public void openNewTabInChrome() {

WebDriver driver = new ChromeDriver();


WebElement element = driver.findElement(By.linkText(“Gmail”));

Actions actionOpenLinkInNewTab = new Actions(driver);


ArrayList tabs = new ArrayList(driver.getWindowHandles()); driver.switchTo().window(tabs.get(1)); driver.get(“”");

driver.close(); driver.switchTo().window(tabs.get(0)); driver.get(“http: //”);



Here is the solution for Firefox:


public void openNewTabInFirefox() {

WebDriver driver = new FirefoxDriver();

driver.get(“http: //”");

WebElement body = driver.findElement(By.cssSelector(“body”));

String newTabAction = Keys.chord(Keys.COMMAND, “t”);


String chooseTab = Keys.chord(Keys.COMMAND, “2”);



1 view0 comments
bottom of page