4

I am attempting to use ctrl + click on a link to open it in a new tab. This is working fine in Chrome 58. Please find the code below:

action.keyDown(Keys.CONTROL).click(driver.findElement(By.xpath
("//section[@class='filmStrip__basic']//a[text()='En savoir 
plus']"))).keyUp(Keys.CONTROL).build().perform(); 

I am using the same code on IE, Firefox and Safari but getting the following error:

Firefox 54: The link is getting open in the same tab. IE 11: Nothing happening.. the control is moving to the next line Safari: exception on action.keyDown-Unrecognized command

Help related to any one browser is also appreciated.

Thanks

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Lal Ansari
  • 139
  • 4
  • 13

3 Answers3

3

As you are trying to click on a link which is within a <a> tag, instead of xpath you can use the linkText locator. Here is the sample code with opens the url http://www.google.com, verifies the Page Title, uses Actions class to click on the Gmail link to open https://accounts.google.com in a new tab.

String URL="http://www.google.com";
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver =  new FirefoxDriver();
driver.get(URL);
System.out.println("Page Title is : "+driver.getTitle());
WebElement link = driver.findElement(By.linkText("Gmail"));
Actions newTab = new Actions(driver); 
newTab.keyDown(Keys.CONTROL).click(link).keyUp(Keys.CONTROL).build().perform();

You can find a relevant Python based solution in How to open a link embed in web element in the main tab, in a new tab of the same window using Selenium Webdriver

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

Another way is to use javascript executor:

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.open('','_blank');");

As for your problem I had it too and didn't find anything usefull until I found this workaround. I even tried: solution with ctrl + enter

Edwin
  • 2,146
  • 20
  • 26
1

try this way....

// specify chromedriver.exe directory path and replace in "driverPath"

            String driverPath = "C:/Users......";
            WebDriver driver;
            System.setProperty("webdriver.chrome.driver", driverPath + "chromedriver.exe");
            driver = new ChromeDriver();

            System.out.println("lanuching 1st url in tab1");

            driver.navigate().to(
                    "https://amazon.com");

            System.out.println("lanuched 1st url in tab1");
            Thread.sleep(30000);
    ((JavascriptExecutor) driver).executeScript(
                    "window.open('http://ebay.com');");
            Thread.sleep(20000);
            Set<String> allwh = driver.getWindowHandles();
            System.out.println(allwh.size());
            for (String v : allwh) {
                System.out.println(v);
                driver.switchTo().window(v);
                String title = driver.getTitle();
                System.out.println("2nd url in tab2" + title);