以最简单的例子来说明,我们需要在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如何模拟复制和粘贴的更多相关文章

  1. selenium webdriver(4)---模拟鼠标键盘操作

    webdriver提供Actions来模拟鼠标悬浮.拖拽和键盘输入等操作,详细代码见org.openqa.selenium.interactions.Actions.本文通过几个实例来说明Action ...

  2. Selenium WebDriver Code

    Selenium WebDriver 用于模拟浏览器的功能,可以做网站测试用,也可以用来做crawler.我是用eclipse开发的,导入selenium-server-standalone-***. ...

  3. 转载 基于Selenium WebDriver的Web应用自动化测试

    转载原地址:  https://www.ibm.com/developerworks/cn/web/1306_chenlei_webdriver/ 对于 Web 应用,软件测试人员在日常的测试工作中, ...

  4. selenium中的对文本进行全选,复制,粘贴,剪切和删除的操作

    # 键盘全选操作from selenium.webdriver.common.keys import Keysdriver.find_element_by_css_selector('#key-dem ...

  5. 使用selenium webdriver+beautifulsoup+跳转frame,实现模拟点击网页下一页按钮,抓取网页数据

    记录一次快速实现的python爬虫,想要抓取中财网数据引擎的新三板板块下面所有股票的公司档案,网址为http://data.cfi.cn/data_ndkA0A1934A1935A1986A1995. ...

  6. selenium webdriver模拟鼠标键盘操作

    在测试使用Selenium webdriver测试WEB系统的时候,用到了模拟鼠标.键盘的一些输入操作. 1.鼠标的左键点击.双击.拖拽.右键点击等: 2.键盘的回车.回退.空格.ctrl.alt.s ...

  7. selenium webdriver模拟鼠标键盘

    在测试使用Selenium webdriver测试WEB系统的时候,用到了模拟鼠标.键盘的一些输入操作. 1.鼠标的左键点击.双击.拖拽.右键点击等: 2.键盘的回车.回退.空格.ctrl.alt.s ...

  8. selenium webdriver从安装到使用(python语言),显示等待和隐性等待用法,切换窗口或者frame,弹框处理,下拉菜单处理,模拟鼠标键盘操作等

    selenium的用法 selenium2.0主要包含selenium IDE 和selenium webDriver,IDE有点类似QTP和LoadRunner的录制功能,就是firefox浏览器的 ...

  9. selenium webdriver (python)的基本用法一

    阅在线 AIP 文档:http://selenium.googlecode.com/git/docs/api/py/index.html目录一.selenium+python 环境搭建........ ...

随机推荐

  1. 小程序 JM

    // 本地 // 'https://ly.com/' // 短信验证码参数: let dataValue = { 'type': 1, mobile: '13615814562' }; dataVal ...

  2. Delphi 之 菜单组件(TMainMenu)

    菜单组件TMainMenu 创建菜单双击TmenuMain,单击Caption就可以添加一个菜单项 菜单中添加分割线只需加“-”就可以添加一个分割线 级联菜单的设计 单击鼠标右键弹出菜单中选择Crea ...

  3. iOS程序执行顺序 AppDelegate及 UIViewController 的生命周期

    iOS程序的启动执行顺序 AppDelegate 及 UIViewController 的生命周期 iOS应用程序的状态切换很重要,而UIViewControler对于iOS这种MVC模式来说尤为重要 ...

  4. Spring点滴十:Spring自动装配(Autowire)

    在基于XML配置元数据,在bean的配置信息中我们可以使用<constructor-arg/>和<property/>属性来实现Spring的依赖注入.Spring 容器也可以 ...

  5. LCP 模板

    LCP Description 给定串 \(S\) . \(m\) 组询问 \((X, Y, L, R)\): 求 \(S[X,Y]\) 与 \(S[L,R]\) 的最长公共前缀. Input 第一行 ...

  6. 内置函数zip()

    zip有拉链的意思,zip函数像拉链一样将0个或多个可迭代对象按相同位置组合成一个zip对象,该zip对象的每个元素是由每个可迭代对象的相同位置的元素组成的元祖. 如果zip中有多个序列,而各序列的长 ...

  7. 基础知识--:before伪元素和:after伪元素

    http://book.51cto.com/art/201108/285688.htm 3.7  替换指定位置 大家都知道before和after是前.后的意思.但是奇怪的是,CSS中的:before ...

  8. NodeJS 笔记 URL模块

    url模块 ,包含分析和解析 URL 的工具. var url = require('url'); url.parse(urlStr[, parseQueryString][, slashesDeno ...

  9. logback常见配置

    依赖jar包 <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core --> <dependency& ...

  10. nova-api源码分析(APP的创建)

    目录结构如下: 上面介绍了nova-api发布所用到的一些lib库,有了上面的基础知识,再来分析nova-api的发布流程,就比较轻松了.nova-api可以提供多种api服务:ec2, osapi_ ...