selenium使用robot对象操作键盘
来源:互联网 发布:河南大学软件学院学费 编辑:程序博客网 时间:2024/06/06 02:52
//通过Robot对象完成按键操作
import java.awt.AWTException;import java.awt.Robot;import java.awt.Toolkit;import java.awt.datatransfer.StringSelection;import java.awt.event.KeyEvent;import org.openqa.selenium.*;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;import org.testng.annotations.Test;import org.testng.annotations.BeforeMethod;import org.testng.annotations.AfterMethod;public class RobotTest {WebDriver driver;String url="https://www.baidu.com"; @Test public void TestDemo() throws InterruptedException { driver.get(url); WebDriverWait wait = new WebDriverWait(driver,10); //显式等待 wait.until(ExpectedConditions.presenceOfElementLocated(By.id("kw"))); driver.findElement(By.id("kw")).click(); setAndctrlVClipboardData("selenium"); pressTabKey(); pressEnterKey(); Thread.sleep(3000); } @BeforeMethod public void beforeMethod() { driver = new FirefoxDriver(); } @AfterMethod public void afterMethod() { driver.quit(); } public void setAndctrlVClipboardData(String string) { StringSelection stringSelection = new StringSelection(string); //使用Toolkit对象的setContents将字符串放到剪切板中 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null); 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); } public void pressTabKey() { Robot robot = null; try{ robot = new Robot(); }catch(AWTException e1){ e1.printStackTrace(); } robot.keyPress(KeyEvent.VK_TAB); robot.keyRelease(KeyEvent.VK_TAB); } public void pressEnterKey() { Robot robot = null; try{ robot = new Robot(); }catch(AWTException e1){ e1.printStackTrace(); } robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); }}
Toolkit类将各种组件与本机工具包绑定起来,Toolkit.getDefaultToolkit();是其最常用的方法,返回默认工具包,提供对本地最底层的java操作。
Toolkit.getDefaultToolkit().getSystemClipboard()获取系统剪切板
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("kw")));
driver.findElement(By.id("kw")).click();
一开始漏了这个点击事件,粘贴板粘贴selenium到地址栏……
运行结果如下:
阅读全文
0 0
- selenium使用robot对象操作键盘
- Selenium Webdriver API-Robot对象操作键盘
- UI自动化测试(四)AutoIT工具使用和robot对象模拟键盘按键操作
- 使用JAVA Robot 扩展Webdriver 模拟键盘鼠标操作
- selenium键盘操作
- Selenium 鼠标和键盘操作
- selenium webdriver 模拟键盘操作
- selenium中键盘模拟操作
- 使用Actions对象模拟鼠标键盘操作
- selenium 操作测试对象
- selenium(3)-鼠标操作、键盘操作
- Python Selenium 键盘和鼠标操作
- selenium webdriver 模拟鼠标键盘的操作
- python selenium鼠标键盘操作(ActionChains)
- SeleniuM的按键操作模拟键盘
- selenium+python键盘加鼠标操作
- 【Selenium】8模拟键盘鼠标操作
- selenium 使用对象库
- spring报错:Unexpected exception parsing XML document from class path resource [Beans_annotation.xml]
- hdu 3642 求长方体的体积交 (线段树 +离散化+扫描线)(重点!模板!)
- Java_基础—Map集合的功能概述
- wklken k-vim 安装提示
- Java一些关键字的理解概括
- selenium使用robot对象操作键盘
- MFC 多文档程序分割窗口
- java------用File类列出一个目录下的所有文件夹和文件
- Problem Solved:cvc4: error while loading shared libraries: libcvc4parser.so.4
- Mybatis在Maven上的 hello world
- PHP常用功能块
- Clojure 为何不是面向对象的?
- 深入理解Java:类加载机制及反射
- 牛克编程-寻找coder