【问题标题】:Not able to perform multiple Keys action in Selenium无法在 Selenium 中执行多个键操作
【发布时间】:2017-01-15 20:40:58
【问题描述】:

以下是按 CONTROL 键并选择 HTML 文件上的多个图块的代码。它没有执行应有的操作。

有人可以帮我吗?

public class ActionBuildPerform {

    public static void main(String... args) {

        WebDriver driver = new FirefoxDriver();

        driver.get("file://C:/selectable.html");

        WebElement one = driver.findElement(By.name("one"));
        WebElement three = driver.findElement(By.name("three"));
        WebElement five = driver.findElement(By.name("five"));

        // Add all the actions into the Actions builder.
        Actions builder = new Actions(driver);

        builder.keyDown(Keys.CONTROL)
                .click(one)
                .click(three)
                .click(five)
                .keyUp(Keys.CONTROL);
        // Generate the composite action.

        Action compositeAction = builder.build();
        // Perform the composite action.
        compositeAction.perform();
    }
}

【问题讨论】:

  • 这不起作用吗? builder.keyDown(Keys.CONTROL) .click(一) .click(三) .click(五) .keyUp(Keys.CONTROL).perform();
  • 是的,它不起作用。以下代码在我的网页上没有单击选择多个对象:builder.keyDown(Keys.CONTROL) .click(one) .click(three) .click(five) .keyUp(Keys.CONTROL);
  • 为什么不尝试使用 WebElement.click() 函数单击元素。像这样的东西。动作生成器 = 新动作(驱动程序); builder.keyDown(Keys.CONTROL).perform(); one.click();三.click();五.click(); builder.keyUp(Keys.CONTROL).perform();
  • 如果我单击一个 Web 元素作为 WebElement.click(),那么它工作正常。我在尝试按下 CONTROL 键并单击多个 Web 元素时遇到问题。
  • 试试上面的代码。它将选择多个元素。

标签: selenium selenium-webdriver selenium-chromedriver selenium-grid


【解决方案1】:

使用 Java Robot 类

 try {
              Robot robot = new Robot();

     //ctrl TAB  

            robot.keyPress(KeyEvent.VK_CONTROL);

          .click(one)
          .click(three)
          .click(five)

          robot.keyRelease(KeyEvent.VK_CONTROL);

          } catch (AWTException e) {
                  e.printStackTrace();
          }
}

【讨论】:

    【解决方案2】:

    感谢大家的帮助和回复。我能够通过使用 Selenium 2.53.0 和 Firefox 46.0 解决这个问题。看来我没有使用与我的 Selenium 版本兼容的浏览器版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-29
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多