1
package javaapplication1;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class JavaApplication1 {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Aca\\desktop\\chromedriver.exe");
        // Initialize driver
        WebDriver driver = new ChromeDriver();
        //Maximize browser window
        driver.manage().window().maximize();
        //Go to URL
        driver.get("http://www.google.com");
        //Set timeout
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        // Open new tab  – May be you are stuck here
        driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
        //Go to URL
        driver.get("http://www.gmail.com");
        //Set new tab timeout
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);        
    }
}

I am trying to open a new tab, leaving the previous tab opened .. I can't get a new tab opened. It keeps opening URL`s in the same tab.. I also tried using Actions.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • 1
    check this if it helps - http://qaperspective.blogspot.in/2016/09/open-new-tab-using-Selenium-WebDriver.html – Amol Chavan Dec 05 '16 at 06:16
  • Yeah Thanks, this works : ((JavascriptExecutor) driver).executeScript("window.open('','_blank');"); – Aleksandar Stojanovic Dec 05 '16 at 18:06
  • Glad it worked, It will work on all modern browsers as they support javascript out-of-box. For headless browser, you can refer this link http://stackoverflow.com/questions/814757/headless-internet-browser/814929#814929 – Amol Chavan Dec 05 '16 at 18:26
  • Can you use any other hotkeys? For example Ctrl+N? I have the same issue, but for me no hotkeys are working :/ – monami Dec 05 '16 at 21:45

4 Answers4

1

You need to switch the driver to the new tab. In Chrome its done like switching windows

String tabHandle = driver.getWindowHandle();

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

// switch to the new window
for (String handle : driver.getWindowHandles()) {
    if (!handle.equals(tabHandle))
    {
        driver.switchTo().window(handle);
    }
}

driver.get("http://www.gmail.com");

// close the new tab and switch back to the old one
driver.close();
driver.switchTo().window(tabHandle);

As a side note, implicitlyWait is defined for the driver, not tab/window. No need to define it again after opening the new tab.

Guy
  • 46,488
  • 10
  • 44
  • 88
1

This is an issue with chromedriver itself. See the related bug submitted

Lucas Tierney
  • 2,503
  • 15
  • 13
1

Please try this to open new tab in Chrome with Selenium Java.

     package com.sample;



        import org.openqa.selenium.chrome.ChromeDriver;
        import org.openqa.selenium.chrome.ChromeOptions;

        public class NewWindow {

            public static void main (String[] args){
                System.setProperty ("webdriver.chrome.driver", "C:\\Chrome Driver\\chromedriver.exe");

                ChromeOptions options = new ChromeOptions(); 
                options.addArguments("disable-arguments");


                WebDriver driver = new ChromeDriver();
                driver.get("https://www.google.com");

((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");

        }
    }
monkrus
  • 1,470
  • 24
  • 23
0

You are forgetting to change the focus of the driver to the new tab before navigating.

Try this after opening a new tab:

driver.sendKeys(Keys.chord(Keys.CONTROL,"t"));
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles()); //Get tabs
driver.switchTo().window(tabs.get(1)); //Navigate to new tab
driver.get("http://www.gmail.com"); //Navigate within new tab
driver.close(); //Close the tab when done
driver.switchTo().window(tabs.get(0)); //Navigate to original tab
Pseudo Sudo
  • 1,402
  • 3
  • 16
  • 34