How to open link in new tab using Selenium WebDriver (Java)

with No Comments

Many users of Selenium Webdriver are faced with a problem to open link in a new tab . I faced this problem too, but this simple solution doesn’t work for me, I can’t use it in some of the scenarios:

 

driver.findElement(By.tagName(“body”)).sendKeys(Keys.CONTROL + “t”);

 

The above solution uses shortcut keys provided by the browser to open a new tab. Generally, all browsers use the same keys to open a new tab. But shortcut keys are OS specific.

So, you can use the another way to open link in a new window and check a scenario in a new tab.
((JavascriptExecutor)driver).executeScript(“window.open(‘http://gmail.com/’,’_blank’);”);

Before using this code you have to add line which is shown below to imports.   

import org.openqa.selenium.JavascriptExecutor;

 

See example of a code below for better understanding of how it works

 

package com.test;

 

import org.openqa.selenium.By;

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.Assert;

import org.testng.annotations.AfterClass;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.Test;

 

public class test {

 

  private WebDriver driver;

 

  @BeforeClass // Runs this method before the first test method in the current class is invoked

  public void setUp() {

      // Create a new instance of the Firefox driver

      driver = new FirefoxDriver();

  }

 

  @Test // Open browser and go to the Wikipedia to find element

  public void gotoAppleWikiPage() {

      // Go to the Wikipedia home page

      driver.get(“https://en.wikipedia.org/”);

      // Find the text input element by its id and type “Apple”

      driver.findElement(By.id(“searchInput”)).sendKeys(“Apple”);

      // Click search button

      driver.findElement(By.id(“searchButton”)).click();

      // Get text from header of the page

      String header = driver.findElement(By.id(“firstHeading”)).getText();

      // Verify that header equals “Apple”

      Assert.assertEquals(header, “Apple”);

  }

 

  @Test // Marking this method as part of the test

  public void gotoSeleniumWikiPageNewTab() {

      // Go to the Wikipedia home page

      ((JavascriptExecutor)driver).executeScript(“window.open(‘https://en.wikipedia.org/’);”);

 

      // driver.get(“https://en.wikipedia.org/”);

      // Find the text input element by its id and type “Selenium”

      driver.findElement(By.id(“searchInput”)).sendKeys(“Selenium”);

      // Click search button

      driver.findElement(By.id(“searchButton”)).click();

      // Get text from header of the Selenium page

      String header = driver.findElement(By.id(“firstHeading”)).getText();

      // Verify that header equals “Selenium WebDriver”

      Assert.assertEquals(header, “Selenium”);

  }

 

  @AfterClass // Runs this method after all the test methods in the current class have been run

  public void tearDown() {

      // Close all browser windows and safely end the session

      driver.quit();

  }

 

}

 

Leave a Reply