在 Selenium Webdriver 中使用操作 class

Use of Actions class in Selenium Webdriver

我正在学习selenium,当我在探索Handling Windows和文本中的框架主题时,我找到了这段代码。是否有必要实例化 Actions class,如果我们永远不会在代码中使用它,为什么我们需要这样做?

public class SwitchBetweenFrames {

   public static void main(String... args) {
      WebDriver driver = new FirefoxDriver();
      driver.get("file://C:/Frames.html");

      Actions action = new Actions(driver);

      driver.switchTo().frame(0);
      WebElement txt = driver.findElement(By.name("1"));
      txt.sendKeys("I'm Frame One");

      driver.switchTo().defaultContent();

      driver.switchTo().frame(1);
      txt = driver.findElement(By.name("2"));
      txt.sendKeys("I'm Frame Two");
   }

}

不,实例化 Actions class 不是必要步骤,除非有实际需要。对于您的代码,根本不需要它。如果您将该部分注释掉,您的代码仍然有效。
通常,Actions class 可用于模拟某些用户手势,例如:拖放、单击并按住等,按照正常标准,这有点棘手实施。

不,我们不需要 Action if nor required。切换到 frame 或 alert 是不同的,使用 Action 是不同的概念。

你可以使用Action的不同功能class

http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/interactions/Actions.html

而 switch 仅用于将 webdriver 的控制从一页转移到另一页或从一帧转移到另一帧

不,没有必要使用操作 class 将控制权传递给 window。对于框架,我们只是将控制传递给另一个框架。所以使用了SwitchTo。 动作 classes 通常用于鼠标悬停动作、拖放、单击并按住等。 如果您想了解更多关于操作 class 的信息,请查看 link, Actions class