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 做自动化测试的时候,会经常模拟鼠标和 ...
随机推荐
- luogu P1040 加分二叉树
题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,-,n),其中数字1,2,3,-,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di,tree及它的每个子树都 ...
- 第2章 Spring Boot 文档
Spring Boot 文档 本节简要介绍了Spring Boot文档,是整个文档的参考指南. 您可以完整阅读本参考指南,或者如果您不感兴趣的话可以跳过该部分. 1. 关于文档 Spring Boot ...
- Hystrix的介绍和简单使用
这周在看项目的相关代码时,接触到了Hystrix,因此查询了相关资料学习了下. 一.什么是Hystrix Hystrix是Netflix针对微服务分布式系统的熔断保护中间件,当我们的客户端连接远程的微 ...
- IOS开发之----两种保存用户名和密码实现记住密码库
使用Keychain存储用户敏感信息 iOS的keychain服务提供了一种安全的保存私密信息(密码,序列号,证书等)的方式,每个ios程序都有一个独立的keychain存储.相对于 NSUserDe ...
- iOS -- 原生NSStream实现socket
- (void)startSocket:(NSString *)address andPort:(int)port { CFReadStreamRef readRef; CFWriteStreamRe ...
- Zxing二维码精简(竖屏、拉伸处理、扫描框大小和扫描线移动)
本帖最后由 levil_ad 于 2013-12-30 13:55 编辑 最近没事做了下二维码扫描,用的是ZXing的开源代码,官方源码地址:http://code.google.com/p/zxin ...
- POJ2503字典树
此代码原始出处:http://blog.csdn.net/cnyali/article/details/47367403 #include<stdio.h> #include<str ...
- 很多shell命令后面的单横杠和双横杠,原来这个意思
原文: https://blog.csdn.net/deyili/article/details/5471023 ------------------------------------------- ...
- 直接返回list不封装的结果集
直接返回list不封装的结果集,在Jsp访问方式: 1.封装成map访问 2.用jstl: <c:forEach var="images" items="${lis ...
- iOS 获取图片某一点的颜色对象(UIColor*)。
- (UIColor *)colorAtPixel:(CGPoint)point { // Cancel if point is outside image coordinates if (!CGRe ...