javascript - Opening a new tab using Ctrl + click combination in Selenium Webdriver - Stack Overflow

I am attempting to use ctrl + click on a link to open it in a new tab. This is working fine in Chrome 5

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 mand

Help related to any one browser is also appreciated.

Thanks

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 mand

Help related to any one browser is also appreciated.

Thanks

Share Improve this question edited Aug 30, 2019 at 12:17 undetected Selenium 194k44 gold badges303 silver badges380 bronze badges asked Sep 13, 2017 at 15:09 Lal AnsariLal Ansari 1394 silver badges14 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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., verifies the Page Title, uses Actions class to click on the Gmail link to open https://accounts.google. in a new tab.

String URL="http://www.google.";
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

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

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

            System.out.println("lanuched 1st url in tab1");
            Thread.sleep(30000);
    ((JavascriptExecutor) driver).executeScript(
                    "window.open('http://ebay.');");
            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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744199102a4562798.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信