Selenium WebDriver如何模拟复制和粘贴
以最简单的例子来说明,我们需要在bing搜索引擎中,输入并查询“Selenium自动化测试”几个字。可以很快就写出如下代码:
String queryString = "Selenium自动化测试";
WebElement element = driver.findElement(By
.xpath("//input[@id='sb_form_q']"));
// 直接输入查询字符串
element.sendKeys(queryString);
// 点击查询按钮
driver.findElement(By.xpath("//input[@id='sb_form_go']")).click();
// 截图函数
captureScreenshot("截图测试JUnit");
但是如果我们想把当前的粘贴板Clipboard中的数据粘贴到bing的搜索输入框,该怎么办呢?Selnium是否支持从从粘贴板中粘贴数据呢?答案是肯定的,直接上代码,代码很简单,并且有注释,不再进行解释。
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.*;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import com.thoughtworks.selenium.SeleneseTestBase; public class SearchChineseCharacters extends SeleneseTestBase {
private static WebDriver driver;
static final int MAX_TIMEOUT_IN_SECONDS = 5; @BeforeClass
public static void setUpBeforeClass() throws Exception {
System.setProperty("webdriver.chrome.driver",
System.getProperty("user.dir") + File.separator
+ "chromedriver.exe");
driver = new ChromeDriver();
String url = "http://cn.bing.com/";
driver.manage().window().maximize();
driver.manage().timeouts()
.implicitlyWait(MAX_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS);
driver.get(url);
} @AfterClass
public static void tearDownAfterClass() throws Exception {
if (driver != null) {
System.out.println("运行结束!");
driver.quit();
}
} @Test
public void test() {
String queryString = "Selenium自动化测试";
WebElement element = driver.findElement(By
.xpath("//input[@id='sb_form_q']"));
// 直接输入查询字符串
// element.sendKeys(queryString); // 下面的语句模拟复制粘贴功能、copy & paste
// 向粘贴板中存放数据,还可以注释掉下面的语句,进行手工复制一些东西到粘贴板
setClipboardData(queryString);
// 模拟Ctrl+V,进行粘贴
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e1) {
e1.printStackTrace();
}
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
// 点击查询按钮
driver.findElement(By.xpath("//input[@id='sb_form_go']")).click();
// 截图函数
captureScreenshot("截图测试JUnit"); } private void captureScreenshot(String fileName) {
String imagePath = System.getProperty("user.dir") + File.separator
+ fileName + ".png";
try {
byte[] decodedScreenshot = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.BYTES);
FileOutputStream fos = new FileOutputStream(new File(imagePath));
fos.write(decodedScreenshot);
fos.close();
System.out.println("截图保存至" + imagePath);
} catch (Exception e) {
e.printStackTrace();
}
} public static void setClipboardData(String string) {
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard()
.setContents(stringSelection, null);
}
}
使用场景:
1.上传文件,
2.富文本框都行
Selenium WebDriver如何模拟复制和粘贴的更多相关文章
- selenium webdriver(4)---模拟鼠标键盘操作
webdriver提供Actions来模拟鼠标悬浮.拖拽和键盘输入等操作,详细代码见org.openqa.selenium.interactions.Actions.本文通过几个实例来说明Action ...
- Selenium WebDriver Code
Selenium WebDriver 用于模拟浏览器的功能,可以做网站测试用,也可以用来做crawler.我是用eclipse开发的,导入selenium-server-standalone-***. ...
- 转载 基于Selenium WebDriver的Web应用自动化测试
转载原地址: https://www.ibm.com/developerworks/cn/web/1306_chenlei_webdriver/ 对于 Web 应用,软件测试人员在日常的测试工作中, ...
- selenium中的对文本进行全选,复制,粘贴,剪切和删除的操作
# 键盘全选操作from selenium.webdriver.common.keys import Keysdriver.find_element_by_css_selector('#key-dem ...
- 使用selenium webdriver+beautifulsoup+跳转frame,实现模拟点击网页下一页按钮,抓取网页数据
记录一次快速实现的python爬虫,想要抓取中财网数据引擎的新三板板块下面所有股票的公司档案,网址为http://data.cfi.cn/data_ndkA0A1934A1935A1986A1995. ...
- selenium webdriver模拟鼠标键盘操作
在测试使用Selenium webdriver测试WEB系统的时候,用到了模拟鼠标.键盘的一些输入操作. 1.鼠标的左键点击.双击.拖拽.右键点击等: 2.键盘的回车.回退.空格.ctrl.alt.s ...
- selenium webdriver模拟鼠标键盘
在测试使用Selenium webdriver测试WEB系统的时候,用到了模拟鼠标.键盘的一些输入操作. 1.鼠标的左键点击.双击.拖拽.右键点击等: 2.键盘的回车.回退.空格.ctrl.alt.s ...
- selenium webdriver从安装到使用(python语言),显示等待和隐性等待用法,切换窗口或者frame,弹框处理,下拉菜单处理,模拟鼠标键盘操作等
selenium的用法 selenium2.0主要包含selenium IDE 和selenium webDriver,IDE有点类似QTP和LoadRunner的录制功能,就是firefox浏览器的 ...
- selenium webdriver (python)的基本用法一
阅在线 AIP 文档:http://selenium.googlecode.com/git/docs/api/py/index.html目录一.selenium+python 环境搭建........ ...
随机推荐
- func_get_args()在php71与php56的区别
func_get_args() 获取函数的所有参数,返回一个数组 官方:http://www.php.net/manual/en/function.func-get-args.php 但是此函数在ph ...
- MySQL基础(二):视图、触发器、函数、事务、存储过程
一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. 视图和上一篇学到的临时表搜索类似. ...
- 5G的作业- 云计算
作业命题:5G对于保险行业的影响,技术层面和业务模式层面 一.5G网络的特点: 5G网络主要有三大特点,极高的速率 enhanced mobile broadband (eMBB),极大的容量 Mas ...
- php学习目录
前面的话 前端工程师为什么要学习php?是因为招聘要求吗?这只是一方面 一开始,我对学习php是抵触的,毕竟javascript已经够自己喝一壶的了,再去学习php,可能让自己喝醉.但是,在学习jav ...
- anaconda的spyder打不开
因为最近一段时间没有用Spyder,今天使用时,发现Spyder又又打不开了. 之前遇到Spyder打不开的情况时,是根据这里的教程:删除.matplotlib和.spyder两个文件,但这次删除这两 ...
- c# 移除文本文件里的某一行
参考自:http://zhidao.baidu.com/question/87467507.html //定义一个变量用来存读到的东西 string text = ""; //用一 ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- python Popen卡死问题
程序经常卡死,定位了半天才定位到原因,原来是Popen导致的卡死: 程序如下: s = subprocess.Popen([*,*,*], stdout=subprocess.PIPE) ret = ...
- 说说Cookie和Session
Session和Cookie在网站开发中是用来保存用户与后端服务器的交互状态.它们有各自的缺点和优点.而且,他们的优点和应用场景是对立的. Cookie 完整地描述:当一个用户通过HTTP访问一个 ...
- bzoj千题计划180:bzoj4411: [Usaco2016 Feb]Load balancing
http://www.lydsy.com/JudgeOnline/problem.php?id=4411 用树状数组维护扫描线 一个树状数组维护扫描线之上的y<=i点,另一个维护扫描线之下y&l ...