一、摘要

本片博文主要展示在使用Selenium with java做web自动化时,一些不得不模拟鼠标操作、模拟键盘操作和控制滚动条的java代码

二、模拟鼠标操作

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

三、模拟键盘操作

package util;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent; public class KeyBoardUtil {
/**Tab键封装*/
public static void pressTabKey(){
Robot robot = null;
try{
robot = new Robot();
}catch (AWTException e){
e.printStackTrace();
}
//调用keypress方法来实现按下Tab键
assert robot != null;
robot.keyPress(KeyEvent.VK_TAB);
//调用keyrelease方法来实现释放Tab键
robot.keyRelease(KeyEvent.VK_TAB);
}
/**Enter键封装*/
public static void pressEnterKey(){
Robot robot = null;
try{
robot = new Robot();
}catch (AWTException e){
e.printStackTrace();
}
//调用keypress方法来实现按下Enter键
assert robot != null;
robot.keyPress(KeyEvent.VK_ENTER);
//调用keyrelease方法来实现释放Enter键
robot.keyRelease(KeyEvent.VK_ENTER);
}
/**将指定字符串设为剪切板内容,执行黏贴操作
*将页面焦点切换到输入框后,调用此函数,将指定字符串黏贴到输入框
*/
public static void setAndctrlVClipboardData(String string){
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Robot robot = null;
try{
robot = new Robot();
}catch (AWTException e){
e.printStackTrace();
}
assert robot != null;
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
} /**
* 键盘向下键封装
*/
public static void pressDownKey(){
Robot robot = null;
try{
robot = new Robot();
}catch (AWTException e){
e.printStackTrace();
}
assert robot != null;
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
}
}

四、控制滚动条

package util;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; public class ScrollBarUtil {
/**
* 控制滚动条向下拉到底
* @param driver 浏览器驱动
*/
public static void toBottom(WebDriver driver) {
JavascriptExecutor js = (JavascriptExecutor) driver;
//向下拉到底//
js.executeScript("document.documentElement.scrollTop=10000");
} /**
* 控制滚动条向上拉到顶
* @param driver 浏览器驱动
*/
public static void toTop(WebDriver driver) {
JavascriptExecutor js = (JavascriptExecutor) driver;
//向上拉到顶
js.executeScript("document.documentElement.scrollTop=0");
} /**
* 控制滚动条向下拉到底
* @param driver 浏览器驱动
*/
public static void scrolltoBottom(WebDriver driver) {
JavascriptExecutor js = (JavascriptExecutor) driver;
//向下拉到底
js.executeScript("window.scrollTo(0,100000)");
} /**
* 控制滚动条向上拉到顶
* @param driver 浏览器驱动
*/
public static void scrolltoTop(WebDriver driver) {
JavascriptExecutor js = (JavascriptExecutor) driver;
//向上拉到顶
js.executeScript("window.scrollTo(0,1)");
} /**
* 控制滚动条拉到中间
* @param driver 浏览器驱动
*/
public static void verticaltoMiddle(WebDriver driver) {
JavascriptExecutor js = (JavascriptExecutor) driver;
//上下拉到中间
js.executeScript("window.scrollBy(0, 0-document.body.scrollHeight *1/2)");
} /**
* 控制水平滚动条左右拉到中间
* @param driver 浏览器驱动
*/
public static void horizontaltoMiddle(WebDriver driver) {
JavascriptExecutor js = (JavascriptExecutor) driver;
//左右拉到中间
js.executeScript("window.scrollBy(0, 0-document.body.scrollWidht *1/2)");
} /**
* 控制滚动条拉到元素可见
* @param driver 浏览器驱动
* @param element 页面元素定位
*/
public static void scrolltoPresence(WebDriver driver, WebElement element) {
JavascriptExecutor js = (JavascriptExecutor) driver;
//移动到元素element对象的“顶端”与当前窗口的“顶部”对齐
//js.executeScript("arguments[0].scrollIntoView();", element);
js.executeScript("arguments[0].scrollIntoView(true);", element);
//移动到元素element对象的“底端”与当前窗口的“底部”对齐
//js.executeScript("arguments[0].scrollIntoView(false);", element);
} /**
* 使用JavaScript的ScrollTo函数和document.body.scrollHeight参数
* 将页面滚动到最下方
* @param driver 浏览器驱动
*/
public static void scrollingToBottomofPage(WebDriver driver){
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
try{
Thread.sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}
} /**
* 使用JavaScript的ScrollTo函数,使用0和800的横纵坐标参数
* 将页面的滚动条纵向下滑800个像素
* @param driver 浏览器驱动
*/
public static void scrollingByCoordinateofPage(WebDriver driver){ ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,200)");
try{
Thread.sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}
}
}

Java&Selenium 鼠标键盘及滚动条控制相关方法封装的更多相关文章

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

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

  2. selenium 鼠标,键盘操作

    1.打开和关闭网页 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #!/usr/bin/env python # -*- coding:u ...

  3. python selenium鼠标键盘操作(ActionChains)

    用selenium做自动化,有时候会遇到需要模拟鼠标操作才能进行的情况,比如单击.双击.点击鼠标右键.拖拽等等.而selenium给我们提供了一个类来处理这类事件--ActionChains sele ...

  4. 自动化测试基础篇--Selenium鼠标键盘事件

    摘自https://www.cnblogs.com/sanzangTst/p/7477382.html 前面几篇文章我们学习了怎么定位元素,同时通过实例也展示了怎么切换到iframe,怎么输入用户名和 ...

  5. Java selenium web页面的滚动条操作

    摘录自:http://blog.csdn.net/iceryan/article/details/8162703 //移动到元素element对象的"顶端"与当前窗口的" ...

  6. java+selenium使用JS、键盘滑动滚动条

    本篇文章介绍如何使用JS和键盘对象对页面进行滑动滚动条-------------主要针对java做自动化测试的同学 一:使用键盘对象操作滚动条 //导包 import org.openqa.selen ...

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

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

  8. 用鼠标键盘来控制你的Android手机——同屏显示简单教程

    今天在微博上看到有人用电脑鼠标操作iPhone手机玩打飞机游戏,非常炫,虽然自己用的不是iPhone,但相信Android手机肯定也能实现这样的功能,于是网上各种搜索方法,终于看到了一篇试用成功的帖子 ...

  9. 【Unity3D】使用鼠标键盘控制Camera视角(即时战略类游戏视角):缩近,拉远,旋转

    今天写一个demo,要用到鼠标键盘控制三维视角,因此写了个脚本用于控制. 该脚本可以用于即时战略类游戏的视角,提供了缩进,拉伸,旋转.同时按住鼠标右键不放,移动鼠标可以实现第一人称视角的效果. usi ...

随机推荐

  1. Python Elasticsearch

    以下所用版本为Elasticsearch 7.2.0 1.安装 pip3 install elasticsearch -i https://pypi.tuna.tsinghua.edu.cn/simp ...

  2. Java High Level REST Client 之 创建索引

    1. 创建索引请求 CreateIndexRequest request = new CreateIndexRequest("twitter"); 2.设置 2.1 分别设置 2. ...

  3. 前端JS之HTML利用XMLHttpRequest()和FormData()进行大文件分段上传

    用于网页向后端上传大文件 ### 前端代码<body> <input type="file" name="video" id="fi ...

  4. jquery设置input不可编辑,背景变灰,鼠标变禁止

    先看效果 $("#id").attr("onfocus", "this.blur()"); $("#id").css(& ...

  5. css设置滚动条并显示或隐藏

    看效果,没有滚动条,超出div,开发中肯定不行. 有滚动条 最后就是想隐藏滚动条 代码 有滚动条并显示 <!DOCTYPE html> <html lang="en&quo ...

  6. (二)mybatis框架原理(图解)

    目录 mybatis 框架原理图(粗略版) mybatis 框架原理图(粗略版)

  7. A司入职面试宝典

    =公司介绍============================= 世界500强,每股股票2000刀. 面试难度:**** 加班程度:* =面试-流程介绍====================== ...

  8. pb相关小技巧或用法

    1.动态post window lwlw = w_main lw.dynamic post event ue_all(ls_no,ls_table) 2.打开隐藏窗口 IF NOT IsValid(w ...

  9. Myatis中的OGNL和bind标签的结合用法

    1.MyBatis常用的OGNL e1 or e2 e1 and e2 e1 == e2,e1 eq e2 e1 != e2,e1 neq e2 e1 lt e2:小于 e1 lte e2:小于等于, ...

  10. Spring在Thread中注入Bean无效的解决方式

    在Spring项目中,有时需要新开线程完成一些复杂任务,而线程中可能需要注入一些服务.而通过Spring注入来管理和使用服务是较为合理的方式.但是若直接在Thread子类中通过注解方式注入Bean是无 ...