this is my line to open the new tab
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
this is my line to open the new tab
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
Use Actions class in WebDriver to do this. Below is a sample code:
WebDriver driver = new ChromeDriver();
driver.navigate().to("<URL>");
WebElement element = driver.findElement(By.cssSelector("body"));
Actions actionOpenLinkInNewTab = new Actions(driver);
actionOpenLinkInNewTab.moveToElement(element).keyDown(Keys.CONTROL).click(element).keyUp(Keys.CONTROL).perform();
this following code works for me in selenium 3 and chrome 58.
WebDriver driver = new ChromeDriver();
driver.get("http://yahoo.com");
((JavascriptExecutor)driver).executeScript("window.open()");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
driver.get("http://google.com");
Your code works in Firefoxdriver but not in Chromedriver.
One solution is that you can open any link on current page into new tab.
Following is the Python code for it.
actions = ActionChains(driver)
home_link = driver.find_element_by_class_name("logo")
actions.move_to_element(home_link)
actions.send_keys(Keys.CONTROL+ Keys.SHIFT)
actions.click(home_link)
actions.perform()