user3080885
user3080885

Reputation: 63

I can't open the NEW TAB in google Chrome Using selenium

this is my line to open the new tab

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

Upvotes: 2

Views: 14514

Answers (5)

nur
nur

Reputation: 527

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

Upvotes: 1

user7181273
user7181273

Reputation: 51

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

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

Upvotes: 5

Dhiraj
Dhiraj

Reputation: 481

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

Upvotes: 0

Andrii
Andrii

Reputation: 357

Try this code:

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

Hope it will help.

Upvotes: 0

Sitam Jana
Sitam Jana

Reputation: 3129

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

Upvotes: 4

Related Questions