【问题标题】:Opening a link in a new tab is working for Firefox but not working for Chrome browser using Selenium在新选项卡中打开链接适用于 Firefox,但不适用于使用 Selenium 的 Chrome 浏览器
【发布时间】:2015-07-23 02:17:02
【问题描述】:

我有一个测试需要在新标签中打开一个链接。这必须在 Firefox 和 Chrome 中有效。我首先尝试使用 Google 页面上的 Gmail 链接。

在 Firefox 上完美运行,Gmail 在新标签中打开。 但在 Chrome 上,Gmail 页面在同一窗口中打开,并且右键单击后菜单保持打开状态。有人遇到过这个问题吗?

下面是我的示例代码。

火狐代码:

FirefoxProfile myprofile;
ProfilesIni profile = new ProfilesIni();            
myprofile = profile.getProfile("SeleniumAuto");             
WebDriver driver = new FirefoxDriver(myprofile);
driver.get("http://www.google.com");    
driver.manage().window().maximize();

Actions a = new Actions(driver);
WebElement e = driver.findElement(By.xpath("/html/body/div/div[3]/div[1]/div/div/div/div[1]/div[2]/a"));
a.moveToElement(e);
a.contextClick(e).sendKeys(Keys.ARROW_DOWN)
 .sendKeys(Keys.ENTER).build().perform();            

Chrome 代码:

ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");    
driver.manage().window().maximize();*/    

Actions a = new Actions(driver);
WebElement e = driver.findElement(By.xpath("/html/body/div/div[3]/div[1]/div/div/div/div[1]/div[2]/a"));
a.moveToElement(e);
a.contextClick(e).sendKeys(Keys.ARROW_DOWN)
 .sendKeys(Keys.ENTER).build().perform();

【问题讨论】:

    标签: google-chrome firefox selenium selenium-webdriver


    【解决方案1】:

    对于 Chrome 试试这个:

    Actions a = new Actions(webdriver);
    WebElement e = webdriver.findElement(By.xpath(your_path));
    a.moveToElement(e).keyDown(Keys.CONTROL).click().build().perform();
    

    【讨论】:

      【解决方案2】:

      是的,您可以使用 Selenium 轻松做到这一点。使用键盘命令 (Ctrl +T) 打开一个新选项卡,然后使用 Ctrl +Tab (Ctrl +\t) 命令切换到新打开的选项卡并执行任何必要的操作。会是这样的

      //open a  new tab
      WebElement e= driver.findElement(By.cssSelector(abc)).sendKeys(Keys.Control + "t");
      //switch control to new tab
      e.sendKeys(Keys.Control + "\t");
      {
          perform some function here
          enter code here
      }
      //switch back to old tab
      e.sendKeys(Keys.Control + "\t");
      

      如果浏览器只打开了两个选项卡,这将起作用,因为这样它将使用 Tab 键在两个打开的选项卡之间移动。

      希望这会有所帮助。

      【讨论】:

        【解决方案3】:

        我也遇到过同样的问题。显然, ARROW_DOWN 不起作用,所以我尝试使用组合键,它对我有用。代码如下:

        1) 在新标签中打开,焦点仍在当前标签上

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(your_path)));
        actions.keyDown(Keys.CONTROL).perform();
        driver.findElement(By.xpath(your_path)).click();
        actions.keyUp(Keys.CONTROL);
        

        2) 在新标签页中打开并移动到新标签页

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(your_path)));
        actions.keyDown(Keys.CONTROL).perform();
        actions.keyDown(Keys.SHIFT).perform();
        driver.findElement(By.xpath(your_path)).click();
        actions.keyUp(Keys.SHIFT);
        actions.keyUp(Keys.CONTROL);
        

        希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 2019-09-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多