【问题标题】:Opening a new tab using Ctrl + click combination in Selenium Webdriver在 Selenium Webdriver 中使用 Ctrl + 单击组合打开一个新选项卡
【发布时间】:2018-02-22 08:54:46
【问题描述】:

我正在尝试使用 ctrl + 单击链接以在新选项卡中打开它。这在 Chrome 58 中运行良好。请在下面找到代码:

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

我在 IE、Firefox 和 Safari 上使用相同的代码,但收到以下错误:

Firefox 54:链接在同一个选项卡中打开。 IE 11:什么都没有发生..控件正在移动到下一行 Safari:action.keyDown-Unrecognized 命令异常

对于任何一种浏览器的帮助也很感激。

谢谢

【问题讨论】:

    标签: javascript java selenium selenium-webdriver action


    【解决方案1】:

    当您尝试单击位于<a> 标记内的链接时,您可以使用linkText 定位器而不是xpath。以下是打开 url http://www.google.com、验证 Page Title、使用 Actions 类单击 Gmail 链接以在新选项卡中打开 https://accounts.google.com 的示例代码。

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

    您可以在How to open a link embed in web element in the main tab, in a new tab of the same window using Selenium Webdriver中找到相关的基于Python的解决方案

    【讨论】:

      【解决方案2】:

      试试这个方法....

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

      【讨论】:

        【解决方案3】:

        另一种方法是使用javascript执行器:

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

        至于您的问题,我也遇到了,直到找到此解决方法后才发现任何有用的东西。 我什至试过:solution with ctrl + enter

        【讨论】:

        • 我也想测试超链接的功能。所以,需要实际点击链接才能打开它。
        • 为什么不使用click
        • 点击是在同一个标​​签中打开它。我想一次性测试整个流程。谢谢
        • 然后只需像上面一样打开新标签(使用相同的路径)并单击此标签而不是主标签,类似于“克隆”标签
        猜你喜欢
        • 1970-01-01
        • 2017-07-28
        • 1970-01-01
        • 2013-07-07
        • 1970-01-01
        • 2018-02-12
        • 2016-05-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多