WebDriver API——鼠标及键盘操作Actions
在自动化中我们可能需要用到鼠标或者是键盘操作,在webdriver中是Actions类进行这些操作的。
代码如下:
Actions action = new Actions(driver); //-------定义一个action对象
action.click();
action.click(searchBt); //-------单击操作
action.doubleClick().perform();
action.doubleClick(searchBt).perform(); //-------双击操作
action.clickAndHold().perform();
action.clickAndHold(searchBt).perform(); //-------悬停操作
action.contextClick().perform();
action.contextClick(searchBt).perform(); //-------右击操作
action.dragAndDrop(searchBt, searchBt).perform(); //-------拖拽操作 从一个元素拖拽到目标元素
这是几个常用操作的简单用法,老规矩,看下源码是怎么定义的action类的:

我们可以看到actons类有多种构造函数和方法,都是根据平时我们不同的需要来进行使用,我们也可以根据自己项目来封装这些操作,做个简单的例子:
package com.testngDemo; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions; public class Demo_ActionsHelper {
protected WebDriver driver;
public Demo_ActionsHelper(WebDriver driver)
{
this.driver = driver;
}
/**
* 单击操作
* @param by 定位元素
*/
public void click(By by)
{
driver.findElement(by).click();
}
/**
* 双击操作
* @param by
*/
public void doubleClick(By by)
{
new Actions(driver).doubleClick(driver.findElement(by)).perform();
}
/**
* 右击点开菜单
* @param by
*/
public void contextmenu(By by)
{
new Actions(driver).contextClick(driver.findElement(by)).perform();
} }
然后调用自己封装好的类

仅提供一个小的例子 ,具体的要根据自己项目的需要不断完善和维护脚本,丰富自己项目的脚本代码,提高自动化测试脚本开发的效率。
那么我们再来看下键盘是如何调用的?
键盘的调用我们是调用的Keys这个类
/*
Copyright 2007-2012 Selenium committers
Copyright 2013 Software Freedom Conservancy Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ package org.openqa.selenium; import java.util.Arrays; /**
* Representations of pressable keys that aren't text. These are stored in the Unicode PUA (Private
* Use Area) code points, 0xE000-0xF8FF.
*
* @see <a href="http://www.google.com.au/search?&q=unicode+pua&btnG=Search">http://www.google.com.au/search?&q=unicode+pua&btnG=Search</a>
*/
public enum Keys implements CharSequence { NULL ('\uE000'),
CANCEL ('\uE001'), // ^break
HELP ('\uE002'),
BACK_SPACE ('\uE003'),
TAB ('\uE004'),
CLEAR ('\uE005'),
RETURN ('\uE006'),
ENTER ('\uE007'),
SHIFT ('\uE008'),
LEFT_SHIFT (Keys.SHIFT),
CONTROL ('\uE009'),
LEFT_CONTROL (Keys.CONTROL),
ALT ('\uE00A'),
LEFT_ALT (Keys.ALT),
PAUSE ('\uE00B'),
ESCAPE ('\uE00C'),
SPACE ('\uE00D'),
PAGE_UP ('\uE00E'),
PAGE_DOWN ('\uE00F'),
END ('\uE010'),
HOME ('\uE011'),
LEFT ('\uE012'),
ARROW_LEFT (Keys.LEFT),
UP ('\uE013'),
ARROW_UP (Keys.UP),
RIGHT ('\uE014'),
ARROW_RIGHT (Keys.RIGHT),
DOWN ('\uE015'),
ARROW_DOWN (Keys.DOWN),
INSERT ('\uE016'),
DELETE ('\uE017'),
SEMICOLON ('\uE018'),
EQUALS ('\uE019'), // Number pad keys
NUMPAD0 ('\uE01A'),
NUMPAD1 ('\uE01B'),
NUMPAD2 ('\uE01C'),
NUMPAD3 ('\uE01D'),
NUMPAD4 ('\uE01E'),
NUMPAD5 ('\uE01F'),
NUMPAD6 ('\uE020'),
NUMPAD7 ('\uE021'),
NUMPAD8 ('\uE022'),
NUMPAD9 ('\uE023'),
MULTIPLY ('\uE024'),
ADD ('\uE025'),
SEPARATOR ('\uE026'),
SUBTRACT ('\uE027'),
DECIMAL ('\uE028'),
DIVIDE ('\uE029'), // Function keys
F1 ('\uE031'),
F2 ('\uE032'),
F3 ('\uE033'),
F4 ('\uE034'),
F5 ('\uE035'),
F6 ('\uE036'),
F7 ('\uE037'),
F8 ('\uE038'),
F9 ('\uE039'),
F10 ('\uE03A'),
F11 ('\uE03B'),
F12 ('\uE03C'), META ('\uE03D'),
COMMAND (Keys.META), ZENKAKU_HANKAKU ('\uE040'); private final char keyCode; Keys(Keys key) {
this(key.charAt(0));
} Keys(char keyCode) {
this.keyCode = keyCode;
} public char charAt(int index) {
if (index == 0) {
return keyCode;
} return 0;
} public int length() {
return 1;
} public CharSequence subSequence(int start, int end) {
if (start == 0 && end == 1) {
return String.valueOf(keyCode);
} throw new IndexOutOfBoundsException();
} @Override
public String toString() {
return String.valueOf(keyCode);
} /**
* Simulate pressing many keys at once in a "chord". Takes a sequence of Keys.XXXX or strings;
* appends each of the values to a string, and adds the chord termination key (Keys.NULL) and
* returns the resultant string.
*
* Note: When the low-level webdriver key handlers see Keys.NULL, active modifier keys
* (CTRL/ALT/SHIFT/etc) release via a keyup event.
*
* Issue: http://code.google.com/p/webdriver/issues/detail?id=79
*/
public static String chord(CharSequence... value) {
return chord(Arrays.asList(value));
} /**
* @see #chord(CharSequence...)
*/
public static String chord(Iterable<CharSequence> value) {
StringBuilder builder = new StringBuilder(); for (CharSequence seq : value) {
builder.append(seq);
} builder.append(Keys.NULL);
return builder.toString();
} /**
* Get the special key representation, {@link Keys}, of the supplied character if there is one. If
* there is no special key tied to this character, null will be returned.
*
* @param key unicode character code
* @return special key linked to the character code, or null if character is not a special key
*/
public static Keys getKeyFromUnicode(char key) {
for (Keys unicodeKey : values()) {
if (unicodeKey.charAt(0) == key) {
return unicodeKey;
}
} return null;
} }
它为我们提供了键盘上的绝大多数的键位,我们可以直接输入键位名称来实现键盘的调用

当然我们也可以根据自己的需要将特定的操作、组合操作封装起来供我们自己使用。
另外在actions类中的keyup keydown修饰键方法,参数参数只能是修饰键:Keys.SHIFT、Keys.ALT、Keys.CONTROL, 否者将抛出 IllegalArgumentException 异常。 其次对于 action.keyDown(theKey) 方法的调用,如果没有显示的调用 action.keyUp(theKey) 或者 action.sendKeys(Keys.NULL) 来释放的话,这个按键将一直保持按住状态。
WebDriver API——鼠标及键盘操作Actions的更多相关文章
- MFC--响应鼠标和键盘操作
一个程序最重要的部分之一是对鼠标和键盘操作的响应. 一. 理解鼠标事件.之前对鼠标事件的认识仅仅局限于处理控件的单击与双击事件.但实际鼠标的操作包含很多.这里将以一个画图的小程序讲解对鼠标的响应. ...
- Python模拟鼠标和键盘操作实现重复性操作
前言 由于工作需要,要利用某软件去采集数据,做重复的动作大概500多次.所以想写一个程序代替人,去点击和输入. 一开始的思路有两个:1.用Python或者windows对此软件直接操作.2.利用Pyt ...
- Qt之股票组件-股票检索--支持预览框、鼠标、键盘操作
目录 一.感慨一下 二.效果展示 三.搜索编辑框 1.编辑框 2.预览框 四.相关文章 原文链接:Qt之股票组件-股票检索--支持预览框.鼠标.键盘操作 一.感慨一下 之前做过一款炒股软件,个人觉着是 ...
- python + selenium webdriver 通过python来模拟鼠标、键盘操作,来解决SWFFileUpload调用系统底层弹出框无法定位问题
Webdriver是基于浏览器操作的,当页面上传文件使用的是flash的控件SWFFileUpload调用的时候,调用的是系统底层的文件选择弹出框 这种情况,Webdriver暂时是不支持除页面外的其 ...
- selenium 使用action进行鼠标,键盘操作
<!--test.html--> <html> <head> <title>Set Timeout</title> <script&g ...
- Selenium_Selenium WebDriver 中鼠标和键盘事件分析及扩展
在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击.双击.右击.拖拽等动作:或者键盘输入.快捷键使用.组合键使用等模拟键盘的操作.在 W ...
- Selenium WebDriver 中鼠标和键盘事件分析及扩展(转)
本文将总结 Selenium WebDriver 中的一些鼠标和键盘事件的使用,以及组合键的使用,并且将介绍 WebDriver 中没有实现的键盘事件(Keys 枚举中没有列举的按键)的扩展.举例说明 ...
- Selenium WebDriver 中鼠标和键盘事件分析及扩展[转载]
原文:http://www.ibm.com/developerworks/cn/java/j-lo-keyboard/ 概念 在使用 Selenium WebDriver 做自动化测试的时候,会经常模 ...
- Selenium WebDriver 中鼠标和键盘事件分析及扩展
[From] http://www.51testing.com/html/18/631118-861557.html 在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和 ...
随机推荐
- 松鼠的新家(lca)
洛谷—— P3258 [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的 ...
- SpringMVC整合MongoDB
首先,在pom文件中新增spring-data-mongodb的依赖: <dependency> <groupId>org.springframework.data</g ...
- 学习笔记 Java类的封装、继承和多态 2014.7.10
1.问题:toString()没搞懂? int a = 1; Integer aa = new Integer(a); //这是实现的过程 System.out.println("Hello ...
- stateMachine 相关知识
一个state的基本构造,processMessage 以及可选的enter exit 和getName. processMessager是用于处理数据. enter 和exit 则是类似于 面向编程 ...
- 阿里巴巴为什么主推HSF?比Dubbo有哪些优势?
作者:匿名用户链接:https://www.zhihu.com/question/39560697/answer/187538165来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- 百科知识 华为手机P7如何更换电池
参考下面 教程 https://item.jd.com/3265516.html
- C 标准库 - <errno.h>
C 标准库 - <errno.h> 简介 C 标准库的 errno.h 头文件定义了整数变量 errno,它是通过系统调用设置的,在错误事件中的某些库函数表明了什么发生了错误.该宏扩展为类 ...
- Arduino关于旋转编码器程序的介绍(Reading Rotary Encoders)--by Markdown
介绍 旋转或编码器是一个角度測量装置. 他用作精确測量电机的旋转角度或者用来控制控制轮子(能够无限旋转,而电位器只能旋转到特定位置).其中有一些还安装了一个能够在轴上按的button,就像音乐播放器的 ...
- LeetCode(15)题解--3Sum
https://leetcode.com/problems/3sum/ 题目: Given an array S of n integers, are there elements a, b, c i ...
- SAM4E单片机之旅——5、LED呼吸和PWM
PWM在高频情况下,一个很好的用处就是通过控制占空比来控制输出的功率,比如控制风扇转速.LED灯的亮度等.这次就利用PWM的中断功能,动态改变脉冲的占空比,来实现呼吸灯的效果. 一.实现思路 PWM可 ...