Opening a new tab using Ctrl + click combination in Selenium Webdriver

Viewed 5291

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

3 Answers

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