Akash Chavan
Akash Chavan

Reputation: 335

Not able to open new tab in Browser using Selenium Webdriver

I am trying to open new tab in selenium using below line of code

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

But tabs is not opening up. Can anyone tell whats wrong in this command? Also can any one explain "driver.findElement(By.cssSelector("body"))" used in this command for ? I tried searching but not proper answers

Below complete is not working. It is opening up both gmail and stack overflow in same tab in chrome not opening up new tab

package TestCode;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Chrome {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver","C:\\Akash\\Drivers\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.gmail.com");
        String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL+"t"); 
        driver.findElement(By.tagName("body")).sendKeys(selectLinkOpeninNewTab);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://stackoverflow.com/");
        System.out.println("Site open");
    }

}

Upvotes: 0

Views: 4932

Answers (2)

Ankur Singh
Ankur Singh

Reputation: 1289

You can use javaScripts to open new tab in chrome.

try below line of code

  JavascriptExecutor js = (JavascriptExecutor) driver; 
 js.executeScript("window.open('https://www.google.com','_blank');");

Refer this link :- link

Upvotes: 3

iamsankalp89
iamsankalp89

Reputation: 4739

Instead of chord use control

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

For chrome use this

first opened the tab and then hit the URL

String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL+"t"); 
driver.findElement(By.tagName("body")).sendKeys(selectLinkOpeninNewTab);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://stackoverflow.com/");
System.out.println("Site open");

Upvotes: 0

Related Questions