
- Selenium 教程
- Selenium - 首页
- Selenium - 概述
- Selenium - 组件
- Selenium - 自动化测试
- Selenium - 环境搭建
- Selenium - 远程控制
- Selenium IDE 教程
- Selenium - IDE 介绍
- Selenium - 特性
- Selenium - 限制
- Selenium - 安装
- Selenium - 创建测试
- Selenium - 创建脚本
- Selenium - 控制流
- Selenium - 存储变量
- Selenium - 警报和弹出窗口
- Selenium - Selenese 命令
- Selenium - Actions 命令
- Selenium - Accessors 命令
- Selenium - Assertions 命令
- Selenium - Assert/Verify 方法
- Selenium - 定位策略
- Selenium - 脚本调试
- Selenium - 验证点
- Selenium - 模式匹配
- Selenium - JSON 数据文件
- Selenium - 浏览器执行
- Selenium - 用户扩展
- Selenium - 代码导出
- Selenium - 代码输出
- Selenium - JavaScript 函数
- Selenium - 插件
- Selenium WebDriver 教程
- Selenium - 介绍
- Selenium WebDriver vs RC
- Selenium - 安装
- Selenium - 第一个测试脚本
- Selenium - 驱动程序会话
- Selenium - 浏览器选项
- Selenium - Chrome 选项
- Selenium - Edge 选项
- Selenium - Firefox 选项
- Selenium - Safari 选项
- Selenium - 双击
- Selenium - 右键单击
- Python 中的 HTML 报告
- 处理编辑框
- Selenium - 单个元素
- Selenium - 多个元素
- Selenium Web 元素
- Selenium - 文件上传
- Selenium - 定位器策略
- Selenium - 相对定位器
- Selenium - 查找器
- Selenium - 查找所有链接
- Selenium - 用户交互
- Selenium - WebElement 命令
- Selenium - 浏览器交互
- Selenium - 浏览器命令
- Selenium - 浏览器导航
- Selenium - 警报和弹出窗口
- Selenium - 处理表单
- Selenium - 窗口和标签页
- Selenium - 处理链接
- Selenium - 输入框
- Selenium - 单选按钮
- Selenium - 复选框
- Selenium - 下拉框
- Selenium - 处理 iframe
- Selenium - 处理 Cookie
- Selenium - 日期时间选择器
- Selenium - 动态 Web 表格
- Selenium - Actions 类
- Selenium - Action 类
- Selenium - 键盘事件
- Selenium - 键上/下
- Selenium - 复制和粘贴
- Selenium - 处理特殊键
- Selenium - 鼠标事件
- Selenium - 拖放
- Selenium - 笔事件
- Selenium - 滚动操作
- Selenium - 等待策略
- Selenium - 显式/隐式等待
- Selenium - 支持特性
- Selenium - 多选
- Selenium - 等待支持
- Selenium - 选择支持
- Selenium - 颜色支持
- Selenium - ThreadGuard
- Selenium - 错误和日志记录
- Selenium - 异常处理
- Selenium - 其他
- Selenium - 处理 Ajax 调用
- Selenium - JSON 数据文件
- Selenium - CSV 数据文件
- Selenium - Excel 数据文件
- Selenium - 跨浏览器测试
- Selenium - 多浏览器测试
- Selenium - 多窗口测试
- Selenium - JavaScript 执行器
- Selenium - 无头执行
- Selenium - 截取屏幕截图
- Selenium - 录制视频
- Selenium - 页面对象模型
- Selenium - 页面工厂
- Selenium - 录制和回放
- Selenium - 框架
- Selenium - 浏览上下文
- Selenium - DevTools
- Selenium Grid 教程
- Selenium - 概述
- Selenium - 架构
- Selenium - 组件
- Selenium - 配置
- Selenium - 创建测试脚本
- Selenium - 测试执行
- Selenium - 端点
- Selenium - 自定义节点
- Selenium 报告工具
- Selenium - 报告工具
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & 其他技术
- Selenium - Java 教程
- Selenium - Python 教程
- Selenium - C# 教程
- Selenium - Javascript 教程
- Selenium - Kotlin 教程
- Selenium - Ruby 教程
- Selenium - Maven & Jenkins
- Selenium - 数据库测试
- Selenium - LogExpert 日志记录
- Selenium - Log4j 日志记录
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash 测试
- Selenium - Apache Ant
- Selenium - Github 教程
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
- Selenium 其他概念
- Selenium - IE 驱动程序
- Selenium - 自动化框架
- Selenium - 关键字驱动框架
- Selenium - 数据驱动框架
- Selenium - 混合驱动框架
- Selenium - SSL 证书错误
- Selenium - 替代方案
- Selenium 有用资源
- Selenium - 问答
- Selenium - 快速指南
- Selenium - 有用资源
- Selenium - 自动化实践
- Selenium - 讨论
Selenium WebDriver - 鼠标事件
Selenium Webdriver 可用于执行鼠标事件操作,例如使用 Actions 类执行右键单击、左键单击、双击、鼠标悬停和移动。contextClick()、doubleClick()、click() 和 moveToElement() 方法用于执行这些操作。
Actions 类中鼠标事件的基本方法
Actions 类中有多种方法可以执行鼠标事件。
click() − 此方法用于在鼠标当前位置执行单击。
build() − 此方法用于创建一个包含所有要执行的操作的动作组合。
perform() − 此方法用于在不首先调用 build() 的情况下执行操作。
release() − 此方法用于在鼠标当前位置释放鼠标操作。
release(WebElement e) − 此方法用于在作为参数传递的 webElement e 的中间释放鼠标操作。
click(WebElement e) − 此方法用于在作为参数传递的 webElement e 的中间执行单击。
doubleClick() − 此方法用于在鼠标当前位置执行双击。
doubleClick(WebElement e) − 此方法用于在作为参数传递的 webElement e 的中间执行双击。
contextClick() − 此方法用于在鼠标当前位置执行右键单击。
contextClick(WebElement e) − 此方法用于在作为参数传递的 webElement e 的中间执行右键单击。
moveToElement(WebElement e) − 此方法用于将鼠标移动到作为参数传递的 webElement e 的中间。
moveToElement(WebElement e, int x-offset, int y-offset) − 此方法用于将鼠标移动到视点中元素的偏移位置。webelement e、x 和 y 偏移值作为参数传递。
clickAndHold() − 此方法用于在鼠标当前位置执行单击(不释放)。
clickAndHold(WebElement e) − 此方法用于在作为参数传递的 webElement e 的中间执行单击(不释放)。
示例 1 - 单击、右键单击、双击
让我们以以下页面为例,我们将在其中分别通过单击网页上的单击我、右键单击我和双击我按钮来执行单击、右键单击和双击操作。

分别执行单击我、右键单击我和双击我按钮后,我们将分别在页面上获得消息您进行了一次动态点击和您进行了双击。

代码实现
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://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
在上面的示例中,我们执行了单击、双击和右键单击,然后在控制台中收到消息 - 单击后的文本:您进行了一次动态点击和双击后的文本:您进行了双击。
最后,收到消息进程已完成,退出代码为 0,表示代码已成功执行。
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
示例 2 - 将鼠标悬停在元素上
让我们来看另一个例子,其中菜单Navbar最初为黑色。

但是,悬停并按住它时,菜单 Navbar 的颜色会发生变化,如下面的图像所示。

代码实现
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://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
示例 3 - 单击元素的菜单和子菜单
让我们来看另一个例子,我们将单击菜单下拉菜单。单击后,将显示所有子菜单 - 操作、另一个操作和其他内容。

代码实现
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://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
在上面的示例中,我们单击了一个菜单,之后我们得到了所有子菜单,并在控制台中显示消息 - 子菜单是:操作,子菜单是:另一个操作,子菜单是:和子菜单是:此处还有其他内容。
最后,收到消息进程已完成,退出代码为 0,表示代码已成功执行。
本教程到此结束,本教程全面介绍了 Selenium Webdriver - 鼠标事件。我们首先介绍了 Actions 类中鼠标事件的基本方法,并通过示例说明了如何在 Selenium Webdriver 中处理鼠标事件。这使您能够深入了解 Selenium Webdriver - 鼠标事件。明智的做法是继续练习您所学的内容,并探索与 Selenium 相关的其他内容,以加深您的理解并拓宽您的视野。