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 环境搭建........ ...
随机推荐
- 『编程题全队』Beta 阶段冲刺博客集合
『编程题全队』Beta 阶段冲刺博客集合 »敏捷冲刺 日期:2018.5.23 博客连接:『编程题全队』Scrum 冲刺博客 »Day1 日期:2018.5.23 博客连接:『编程题全队』Beta 阶 ...
- call/cc 总结 | Scheme
call/cc 总结 | Scheme 来源 https://www.sczyh30.com/posts/Functional-Programming/call-with-current-contin ...
- 【转】安全加密(一):这些MCU加密方法你都知道吗?
本文导读 随着物联网和边缘计算的出现,五花八门的MCU也被应用其中,如何保证我们的程序安全和知识产权不受侵犯呢,本文我们将对主流MCU的程序加密进行讲解,希望能够帮助你选择最适合自己应用的微处理器. ...
- Json对象和Json字符串的区别
说白了,字符串都是带引号的. 尤其是在使用springmvc的时候,后台@RequestBody接受的是一个json格式的字符串,一定是一个字符串. 参考这个博客还可以: https://blog. ...
- javascript中的this到底指什么?
来自百度知道解释 JavaScript:this是什么? 定义:this是包含它的函数作为方法被调用时所属的对象.说明:这句话有点咬嘴,但一个多余的字也没有,定义非常准确,我们可以分3部分来理解它!1 ...
- sklearn5_preprocessing数据标准化
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...
- js监听浏览器tab窗口切换
js监听浏览器tab窗口切换 ——IT唐伯虎 摘要:js监听浏览器tab窗口切换. if (document.hidden !== undefined) { document.addEventLis ...
- Solr记录-solr基础内容
Solr架构(体系结构) 在本章中,我们将讨论Apache Solr的架构. 下图显示了Apache Solr的体系结构的框图. Solr架构 - 构件块以下是Apache Solr的主要构建块(组件 ...
- 从零开始写一个武侠冒险游戏-0-开发框架Codea简介
从零开始写一个武侠冒险游戏-0-开发框架Codea简介 作者:FreeBlues 修订记录 2016.06.21 初稿完成. 2016.08.03 增加对 XCode 项目文件的说明. 概述 本游戏全 ...
- Excel VBA保护工作表
'设定可编辑区域 ActiveSheet.Protection.AllowEditRanges.Add Title:="区域1", Range:=Range("E5:H1 ...