Java&Selenium 模拟鼠标方法封装

package util;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.WebDriver; public class MouseUtil {
/**
* 模拟鼠标左键单击
* @param driver
* @param xpathExpression
*/
public void lefClick(WebDriver driver, String xpathExpression) {
Actions actions = new Actions(driver);
// 鼠标左键在当前停留的位置做单击操作
actions.click();
// 鼠标左键点击指定的元素
actions.click(driver.findElement(By.xpath(xpathExpression)));
} /**
* 模拟鼠标右键单击
* @param driver
* @param xpathExpression
*/
public void rightClick(WebDriver driver, String xpathExpression) {
Actions actions = new Actions(driver);
// 鼠标右键在当前停留的位置做单击操作
actions.contextClick();
// 鼠标右键点击指定的元素
actions.contextClick(driver.findElement(By.xpath(xpathExpression)));
} /**
* 模拟鼠标左键双击
* @param driver
* @param xpathExpression
*/
public void doubleClick(WebDriver driver, String xpathExpression) {
Actions actions = new Actions(driver);
// 鼠标在当前停留的位置做双击操作
actions.doubleClick();
// 鼠标双击指定的元素
actions.doubleClick(driver.findElement(By.xpath(xpathExpression))); } /**
* 模拟鼠标拖拽
* @param driver
* @param source
* @param target
*/
public void dragAction(WebDriver driver, WebElement source, WebElement target) {
Actions actions = new Actions(driver);
// 鼠标拖拽动作,将 source 元素拖放到 target 元素的位置
actions.dragAndDrop(source, target);
} /**
* 模拟鼠标拖拽到某坐标
* @param driver
* @param source
* @param xOffset
* @param yOffset
*/
public void dragtoXY(WebDriver driver, WebElement source, int xOffset, int yOffset) {
Actions actions = new Actions(driver);
// 鼠标拖拽动作,将 source 元素拖放到 (xOffset, yOffset) 位置,其中 xOffset 为横坐标,yOffset 为纵坐标
actions.dragAndDropBy(source, xOffset, yOffset); } /**
* 模拟鼠标拖拽从元素A到元素B
* @param driver
* @param source
* @param target
*/
public void dragActionReleaseMouse(WebDriver driver, WebElement source, WebElement target) {
Actions actions = new Actions(driver);
// 鼠标拖拽动作,将 source 元素拖放到 target 元素的位置
actions.clickAndHold(source).moveToElement(target).perform();
actions.release();
} /**
* 模拟鼠标单击并不释放
* @param driver
* @param element
*/
public void clickAndHole(WebDriver driver, WebElement element) {
Actions actions = new Actions(driver);
//action.clickAndHold();鼠标悬停在当前位置,既点击并且不释放
// 鼠标悬停在 onElement 元素的位置
actions.clickAndHold(element);
} /**
* 模拟鼠标拖拽
* @param driver
* @param xOffset
* @param yOffset
*/
public void moveToXY(WebDriver driver, int xOffset, int yOffset){
Actions actions = new Actions(driver);
/**将鼠标移到元素 toElement 的 (xOffset, yOffset) 位置,这里的 (xOffset, yOffset) 是以元素 toElement 的左上角为 (0,0) 开始的 (x, y) 坐标轴
*action.moveToElement(toElement,xOffset,yOffset)
*以鼠标当前位置或者 (0,0) 为中心开始移动到 (xOffset, yOffset) 坐标轴*/
actions.moveByOffset(xOffset, yOffset);
actions.release();// 释放鼠标
}

Java&Selenium 模拟鼠标方法封装的更多相关文章

  1. Java&Selenium 模拟键盘方法封装

    Java&Selenium 模拟键盘方法封装 package util; import java.awt.AWTException; import java.awt.Robot; import ...

  2. Java&Selenium智能等待方法封装

    Java&Selenium智能等待方法封装 ExpectedConditions方法还有很多,自然也可以继续扩展很多 package util; import org.openqa.selen ...

  3. Java&Selenium控制滚动条方法封装

    Java&Selenium控制滚动条方法封装 package util; import org.openqa.selenium.JavascriptExecutor; import org.o ...

  4. selenium模拟鼠标操作

    Selenium提供了一个类ActionChains来处理模拟鼠标事件,如单击.双击.拖动等. 基本语法: class ActionChains(object): """ ...

  5. selenium模拟鼠标键盘操作

    简单操作: 1.点击(鼠标左键)页面按钮:click() 2.清空输入框:clear() 3.输入字符串:send_keys()submit提交表单: 1.一般情况可以点击搜索按钮来搜索 2.也可以用 ...

  6. Python&Selenium智能等待方法封装

    摘要:本篇博文用几行代码展示Python和Selenium做自动化测试时常见的显示等待和封装 # 用于实现智能等待页面元素的出现 # encoding = utf-8 ""&quo ...

  7. python+selenium模拟鼠标操作

    from selenium.webdriver.common.action_chains import ActionChains #导入鼠标相关的包 ------------------------- ...

  8. java script 模拟鼠标事件

    try { var selector1 = "._3-8y:first-child"; var evt = document.createEvent("MouseEven ...

  9. 模拟鼠标操作(ActionChains)(转 侵删)

    在日常的测试中,经常会遇到需要鼠标去操作的一些事情,比如说悬浮菜单.拖动验证码等,这一节我们来学习如何使用webdriver模拟鼠标的操作 首页模拟鼠标的操作要首先引入ActionChains的包 f ...

随机推荐

  1. 【搬运】Visual Studio vs2017 vs2019 中文离线安装包下载,替代ISO镜像

    原文地址[侵删]:https://blog.csdn.net/fromfire2/article/details/81104648 以下为搬运内容: 官档地址 https://docs.microso ...

  2. java基础系列(一):Number,Character和String类及操作

    这篇文章总结了Java中最基础的类以及常用的方法,主要有:Number,Character,String. 1.Number类 在实际开发的过程中,常常会用到需要使用对象而不是内置的数据类型的情形.所 ...

  3. 谷歌插件:右键搜使用指南.md

    基本功能看插件,说几个自定义搜索的实现 自定义技巧:%s选择的文字或页面或链接,%g转换成GBK字符集,%t转换成BIG5字符集,%p剪贴板内容,%u当前页面域名. 注:这部分内容来自谷歌插件介绍.使 ...

  4. for i in range()

    for i in range()就是python中的循环语句 有以下三种常见用法: 1.range(3) [0,3)即0,1,2 2.range(1,3) [1,3)即1,2 3.range(1,5, ...

  5. NPM的安装和使用权限问题

    npm之前在默认情况下装过@angular/cli, 安装和使用都没有任何问题, 但是有的包全局安装的时候会提示权限不足, 于是网上搜索了修复此问题的方法, 就是将npm的全局安装目录搬到有权限的文件 ...

  6. [转帖]18W喂不饱有必要买30W充电器吗?iPhone 11 Pro Max充电评测

    18W喂不饱有必要买30W充电器吗?iPhone 11 Pro Max充电评测 https://www.cnbeta.com/articles/tech/895237.htm 改天买一个 设备玩一玩 ...

  7. fiddler手机抓包1

    1.手机抓包配置教程:https://www.jianshu.com/p/724097741bdf 2.

  8. Feign【替换默认的feign client】

    说明: feign默认情况下使用的是JDK原始的URLConnection发送的HTTP请求,没有使用到连接池,但是对每个地址会保持长连接,即HTTP的persistence connection.我 ...

  9. TIM—基本定时器代码

    使用目的:使用TIM定时器让小灯每0.5秒翻转一次亮灭 编程过程: 1-配置时基初始化结构体 2-开启定时器更新中断(即定时时间到了) 3-配置中断优先级 4-使能定时器 5-编写中断服务函数 6-编 ...

  10. springboot项目在IDEA根据不同的开发人员读取不同的配置文件

    IDEA启动项目打开项目的配置文件,修改Program argument为--spring.profiles.active=developerName启动项目,即可 命令行方式启动项目 java -j ...