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 环境搭建........ ...
随机推荐
- js 判断是否选中
js的方法 window.onload = function(){ var oDiv = document.getElementById('div1'); alert(oDiv.checked) } ...
- Windows 设置开机自动登录
1. 自己一些windows的虚拟机 有时候开机之后 输入用户名密码时间特别长. 需要等待很久, 如果能够设置开机自动登录的话 能够节约很多时间. 2. 最简单的办法 运行输入 control us ...
- css 鼠标移上去会变大
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- Django-基本指令
目录 Django基本指令 下载Django 创建Django项目 创建APP应用 启动Django项目 更新数据库表或字段 清空数据库数据 创建超级管理员 查看更多命令 Django基本指令 下载D ...
- Windows 10 中的存储空间
存储空间有助于保护你的数据免受驱动器故障的影响,并随着你向电脑添加驱动器而扩展存储.你可以使用存储空间将两个或多个驱动器一起分组到一个存储池中,然后使用该池的容量来创建称为存储空间的虚拟驱动器.这些存 ...
- mininet invalid literal for int() with base 10: 'cpu.cfs_period_us:'
问题产生原因:内核编译时没有加入 CONFIG_CFS_BANDWIDTH 选项 http://www.haifux.org/lectures/299/netLec7.pdf https://mail ...
- native2ascii -reverse -encoding UTF-8 validation_msg.properties > validation_msg_src.properties
native2ascii -reverse -encoding UTF-8 validation_msg.properties > validation_msg_src.properties
- 自学Zabbix3.5.1-监控项item-key介绍
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix3.5.1-监控项item-key介绍 个人觉得艰难理解,故附上原文档:https ...
- 【bzoj4516】 Sdoi2016—生成魔咒
http://www.lydsy.com/JudgeOnline/problem.php?id=4516 (题目链接) 题意 依次向字符串末尾加上一个字符,每次求不同子串个数. Solution 如果 ...
- 各种蕴含算法思想的DP - 2
study from: https://www.cnblogs.com/flashhu/p/9480669.html 3.斜率dp study from:http://www.cnblogs.com/ ...