How to open a link in new tab of chrome browser using Selenium WebDriver?


We can open a link in the new tab of Chrome browser using Selenium webdriver using the methods Keys.chord and sendKeys. The method Keys.chord is used to send multiple keys simultaneously as parameters.

To open a new tab, the Keys.CONTROL and Keys.ENTER are passed as parameters to the Keys.chord. Finally, the Keys.chord is passed as a parameter to the sendKeys.

Let us click on the Jobs links in a new tab, highlighted in the below image −

Syntax

String l = Keys.chord(Keys.CONTROL,Keys.ENTER);
driver.findElement(By.xpath ("//a[@title='Job @ Tutorials Point']")).sendKeys(l);

Example

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Keys;
public class ElementLocator{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
         "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      driver.get("https://www.tutorialspoint.com/index.htm");
      //Keys.chord string
      String l = Keys.chord(Keys.CONTROL,Keys.ENTER);
      //open in a new tab
      driver.findElement(By.xpath ("//a[@title='Job @ Tutorials Point']")).sendKeys(l);
   }
}

Output

Updated on: 06-Apr-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements