Opening a new tab using Ctrl + click combination in Selenium Webdriver

As you are trying to click on a link which is within a <a> tag, instead of xpath you can use the linkText locator. Here is the sample code with opens the url http://www.google.com, verifies the Page Title, uses Actions class to click on the Gmail link to open https://accounts.google.com in a new tab.

String URL="http://www.google.com";
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver =  new FirefoxDriver();
driver.get(URL);
System.out.println("Page Title is : "+driver.getTitle());
WebElement link = driver.findElement(By.linkText("Gmail"));
Actions newTab = new Actions(driver); 
newTab.keyDown(Keys.CONTROL).click(link).keyUp(Keys.CONTROL).build().perform();

You can find a relevant Python based solution in How to open a link embed in web element in the main tab, in a new tab of the same window using Selenium Webdriver

Leave a Comment