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 环境搭建........ ...
随机推荐
- Jmeter使用笔记之组件的作用域
以前一直使用loadrunner,最近入职新公司后需要使用jmeter,这里把使用过程中出现的一些问题进行总结,同时会和自己使用loadrunner的情况相比较,以后也会不断总结,GO! 一.组件的作 ...
- Linux命令(十八) 压缩或解压缩文件和目录 gzip gunzip
目录 1.命令简介 2.常用参数介绍 3.实例 4.直达底部 命令简介 和 zip 命令类似,gzip 用于文件的压缩,gzip压缩后的文件扩展名为 ".gz",gzip默认压缩后 ...
- SpringBoot(十四)_springboot使用内置定时任务Scheduled的使用(一)
为什么使用定时? 日常工作中,经常会用到定时任务,比如各种统计,并不要求实时性.此时可以通过提前设置定时任务先把数据跑出来,后续处理起来更方便. 本篇文章主要介绍 springboot内置定时任务. ...
- 【华为机试】—— 15.求int型正整数在内存中存储时1的个数
题目 解法 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner ...
- 通信对象 System.ServiceModel.Channels.ServiceChannel 无法用于通信,因为其处于“出错”状态。
通信对象 System.ServiceModel.Channels.ServiceChannel 无法用于通信,因为其处于“出错”状态. 在 System.ServiceModel.Channels. ...
- Codeforces 68D - Half-decay Tree
题意 有一颗高度为 \(h\) 的完全二叉树(即点数为 \(2^{h+1}-1\) ),有两种操作: add x y 给 \(x\) 点的权值加 \(y\) decay 一次衰变定义为选择一个叶子节点 ...
- 51nod 1102 面积最大的矩形 (单调栈)
链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1102 思路: 首先介绍下单调栈的功能:利用单调栈,可以找到从左/ ...
- java.lang.UnsupportedClassVersionError: org/kie/api/KieServices$Factory : Unsupported major.minor version 52.0
Unsupported major.minor version 52.0为JDK1.8编译器的版本,需要更换为JDK1.8的编译器与JDK1.8开发环境 idea中通过修改
- 界面编程之QT的Socket通信20180730
/*******************************************************************************************/ 一.linu ...
- Jenkins + Pipeline 构建流水线发布
Jenkins + Pipeline 构建流水线发布 利用Jenkins的Pipeline配置发布流水线 参考: https://jenkins.io/doc/pipeline/tour/depl ...