2

this is my line to open the new tab

 driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
2
  • 3
    I would not recommend using tabs. They aren't supported in selenium. It will work, but trying to run commands on different tabs isn't a good way to have multiple processes. Commented Apr 19, 2014 at 23:17
  • The above code works only in FF not in Chrome. For Chrome you can use JS Commented Jul 19, 2017 at 7:04

5 Answers 5

5
((JavascriptExecutor)driver).executeScript("window.open();");

This JavaScript code opens a new tab for the Chrome browser.

Sign up to request clarification or add additional context in comments.

Comments

4

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();

Comments

1

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");

1 Comment

Yes, the above code is worked well both in Chrome and FF
0

Try this code:

    Actions newTab = new Actions(webDriver);
    newTab.sendKeys(Keys.CONTROL + "t").perform();

Hope it will help.

Comments

0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.