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 做自动化测试的时候,会经常模拟鼠标和 ...
随机推荐
- tr命令用法
原文链接 # echo "abcd"|tr 'a' 'b' bbcd tr 命令 用途 转换字符. 语法 tr [ -c | -cds | -cs | -C | -Cds | -C ...
- Jenkins-------初探
Jenkins 安装和使用就不说了,说一下jenkins mail的配置,稍微有点坑,记住两个地址一致 插件安装时也出问题,大天朝的防火墙真是醉了,如下 更换我大天朝的镜像站 链接如下 ht ...
- Ubuntu 16.04下使用Wine安装Xshell 4和Xftp 4
说明: 1.使用的Wine版本是深度出品(Deepin),已经精简了很多没用的配置,使启动能非常快,占用资源小. 2.由于Xshell 5的C++库无法在这个Wine版本运行,即使升级官方原版的2+版 ...
- sql标准支持了事务隔离级别
事务隔离级别 尽管数据库为用户提供了锁的DML操作方式,但直接使用锁管理是非常麻烦的,因此数据库为用户提供了自动锁机制.只要用户指定会话的事务隔离级别,数据库就会分析事务中的SQL语句,然后自动为事务 ...
- INDY9发送tstream
INDY9发送tstream 首先都要发送stream.Size, 这是必须的. // 服务端 AThread.Connection.WriteInteger(stream2.Size); AThre ...
- 【层次查询】Hierarchical Queries之亲兄弟间的排序(ORDER SIBLINGS BY)
http://blog.itpub.net/519536/viewspace-624176 有关层次查询之前的文章参考如下. [层次查询]Hierarchical Queries之"树的遍历 ...
- U-net图像分割
[Keras]基于SegNet和U-Net的遥感图像语义分割 2014 年,加州大学伯克利分校的 Long 等人提出全卷积网络(FCN),这使得卷积神经网络无需全连接层即可进行密集的像素预测,CNN ...
- vue2.0 + vux (二)Footer组件
1.Footer组件 Footer.vue <!-- 底部 footer --> <template> <div> <tabbar> <!-- 综 ...
- js 字符串常用方法
数组方面 1.push:向数组尾部增加内容,返回的是新数组的长度. var arr = [1,2,3]; console.log(arr); var b = arr.push(4); console. ...
- redis 3.0.1 在CentOS上的安装
一.下载 wget http://download.redis.io/releases/redis-3.0.1.tar.gz 二.解压 tar xzf redis-3.0.1.tar.gz 三.进入 ...