Selenium 简明教程

Selenium WebDriver - Mouse Events

可以使用 Selenium Webdriver 来使用 Actions 类执行鼠标事件操作,如右键单击、左键单击、双击、鼠标悬停和移动等。方法 contextClick()、doubleClick()、click() 和 moveToElement() 用于执行这些操作。

Basic Methods of Mouse Events in Actions Class

Actions 类中提供了执行鼠标事件的多种方法。

  1. click() − 此方法用于在鼠标的当前位置执行单击操作。

  2. build() − 此方法用于创建动作组合,其中包含要执行的所有动作。

  3. perform() − 此方法用于在不首先调用 build() 的情况下执行动作。

  4. release() − 此方法用于在鼠标的当前位置释放鼠标操作。

  5. release(WebElement e) − 此方法用于在作为参数传递的 webElement e 的中间位置释放鼠标操作。

  6. click(WebElement e) − 此方法用于在作为参数传递的 webElement e 的中间位置执行单击操作。

  7. doubleClick() − 此方法用于在鼠标的当前位置执行双击操作。

  8. doubleClick(WebElement e) − 此方法用于在作为参数传递的 webElement e 的中间位置执行双击操作。

  9. contextClick() − 此方法用于在鼠标的当前位置执行右键单击操作。

  10. contextClick(WebElement e) − 此方法用于在作为参数传递的 webElement e 的中间位置执行右键单击操作。

  11. moveToElement(WebElement e) − 此方法用于将鼠标移动到作为参数传递的 webElement e 的中间。

  12. moveToElement(WebElement e, int x-offset, int y-offset) − 此方法用于将鼠标移动到视点中元素的偏移量。webElement e、x 和 y 偏移值作为参数传递。

  13. clickAndHold() − 此方法用于在鼠标的当前位置执行单击(不释放)。

  14. clickAndHold(WebElement e) − 此方法用于在作为参数传递的 webElement e 的中间执行单击(不释放)。

Example 1 - Click, Right Click, Double Click

让我们看下面这个页面的示例,我们通过分别单击网页上的 Click Me, Right Click MeDouble Click Me 按钮,来执行单击、右键单击和双击。

selenium mouse events 1

分别执行 Click Me, Right Click MeDouble Click Me 按钮后,我们将在页面上分别收到消息 You have a dynamic clickYou have double clicked

selenium mouse events 2

Code Implementation

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;

public class ActionsMouse {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage
      driver.get("https://www.tutorialspoint.com/selenium/practice/buttons.php");

      // identify element with xpath for click
      WebElement m = driver.findElement
         (By.xpath("/html/body/main/div/div/div[2]/button[1]"));

      // object of Actions class to move then click
      Actions a = new Actions(driver);
      a.moveToElement(m).click().build().perform();

      // get text after click
      WebElement t = driver.findElement(By.xpath("//*[@id='welcomeDiv']"));
      System.out.println("Text after click: " + t.getText());

      // identify element with xpath for double click
      WebElement n = driver.findElement
         (By.xpath("/html/body/main/div/div/div[2]/button[3]"));

      // double click
      a.moveToElement(n).doubleClick().build().perform();

      // get text after double click
      WebElement x = driver.findElement(By.xpath("//*[@id='doublec']"));
      System.out.println("Text after double click: " + x.getText());

      // identify element with xpath for right click
      WebElement y = driver.findElement
         (By.xpath("/html/body/main/div/div/div[2]/button[2]"));

      // right click
      a.moveToElement(y).contextClick().build().perform();

      // Closing browser
      driver.quit();
   }
}
Text after click: You have done a dynamic click
Text after double click: You have Double clicked

Process finished with exit code 0

在上面的示例中,我们执行了单击、双击和右键单击,然后在控制台中收到了消息 - Text after click: You have done a dynamic clickText after double click: You have Double clicked

最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。

Example 2 - Mouse Hover on Element

让我们来看另一个示例,最初菜单 Navbar 是黑色的。

selenium mouse events 3

但是,在悬停并按住它时,菜单栏会像下图所示那样改变颜色。

selenium mouse events 4

Code Implementation

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;

public class ActionsClickandHold {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage
      driver.get("https://www.tutorialspoint.com/selenium/practice/menu.php#");

      // identify element with xpath for click and hold
      WebElement m = driver.findElement
         (By.xpath("/html/body/main/div/div/div[2]/nav/div/a"));

      // get element color in rgba format
      String s = m.getCssValue("color");
      System.out.println("rgba code for color element: " + s );

      // object of Actions class to click and hold
      Actions a = new Actions(driver);
      a.clickAndHold(m).build().perform();

      // get element color in rgba format
      String c = m.getCssValue("color");
      System.out.println("rgba code for color for element after click and hold: " + c);

      // Closing browser
      driver.quit();
   }
}
rgba code for color element: rgba(51, 51, 51, 1)
rgba code for color for element after click and hold: rgba(64, 169, 68, 1)

Process finished with exit code 0

Example 3 - Click Menu and Sub-Menus of an Element

让我们再举一个例子,我们会在其中单击菜单下拉框。单击它时,会显示所有子菜单 - Actions, Another actionSomething else

selenium mouse events 5

Code Implementation

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class ActionsMenu {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage
      driver.get("https://www.tutorialspoint.com/selenium/practice/menu.php#");

      // identify element with xpath for menu
      WebElement m = driver.findElement
         (By.xpath("//*[@id='navbarSupportedContent']/ul/li[3]/a"));

      // object of Actions class to click and hold
      Actions a = new Actions(driver);
      a.moveToElement(m).click().build().perform();

      // identify  all sub-menu under main menu
      List<WebElement> l= driver.findElements
         (By.xpath("//*[@id='navbarSupportedContent']/ul/li[3]/ul/li"));
      for(int i = 0; i < l.size(); i++){
         System.out.println("Sub-menus are: " + l.get(i).getText());
      }

      // Closing browser
      driver.quit();
   }
}
Sub-menus are: Action
Sub-menus are: Another action
Sub-menus are:
Sub-menus are: Something else here

Process finished with exit code 0

在上面的示例中,我们单击了菜单,之后我们收到了带有控制台消息的子菜单 - Sub-menus are: Action, Sub-menus are: Another action, Sub-menus are:Sub-menus are: Something else here

最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。

这总结了我们对 Selenium Webdriver - 鼠标事件教程的全面看法。我们从描述 Actions 类中鼠标事件的基本方法开始,并通过示例来说明如何处理 Selenium Webdriver 中的鼠标事件。这使你对 Selenium Webdriver - 鼠标事件有了深入的了解。明智的做法是不断实践你所学到的知识并探索其他与 Selenium 相关的知识,以加深理解并拓宽视野。