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

3 Answers 3

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

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

Comments

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

4 Comments

I want to test the functionality of the hyperlink as well. So, need to actually click the link to open it.
the why not using click?
click is opening it in the same tab. I want to test the entire flow in one go. Thanks
then just open new tab as above (with the same path) and click on this tab instead of the main, sort of "clone" tab
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);

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.