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();// 释放鼠标
}

最新文章

  1. Mongodb在Linux下的安装和启动和配置
  2. AngularJs的UI组件ui-Bootstrap分享(八)——Tooltip和Popover
  3. 从AutoCAD和.NET开始
  4. TCP发消息续传文件
  5. 如何进行SCCM中客户端记录信息维护
  6. C# 之 AES加密源码
  7. 设置将 Microsoft Azure 的网络基础结构以支持设置为灾难恢复站点
  8. MySQL常用命令大全(转)
  9. 关于twitter的GIF变mp4的测试
  10. Ubuntu16.04下安装mysql
  11. 使用phpMyAdmin批量修改Mysql数据表前缀的方法
  12. 【Unity3D】Unity3D开发《我的世界》之七、番外篇
  13. java 实现登录验证码 (kaptcha 验证码组件)
  14. codeforces 983B XOR-pyramid
  15. AttributeError: 'NoneType' object has no attribute 'split' 报错处理
  16. MySQL高级知识(五)——索引分析
  17. 多线程读者写者问题--用QT实现
  18. ASP.NET 实现重启系统或关机
  19. git-搭建企业git服务器
  20. 015 反射中的 Class.forName() 与 ClassLoader.loadClass() 的区别

热门文章

  1. 码云clone提示“you do not have permission to pull from the repository”
  2. eNSP——STP配置和选路规则
  3. poj2185(kmp算法next数组求最小循环节,思维)
  4. 封装一个Model或者Vender类
  5. layer弹出层,结合art-template实现弹出编辑
  6. springboot集成elk 一: springboot + Elasticsearch
  7. sql query skill
  8. Fabric中的节点类型
  9. app实现长按出现弹窗 或者 出现 删除
  10. echarts的基本使用以及如何使用官方实例的方法