一、键盘操作
用代码来模拟键盘的Enter或一系列的组合键,前面使用sendkeys()方法模拟键盘的输入,除此之外还可以模拟键盘组合键输入如下:
整理一些比较常用的键盘操作如下:
sendKeys(Keys.BACK_SPACE); //删除键--Backspace
sendKeys(Keys.SPACE); //空格键 Space
sendKeys(Keys.TAB); //制表键 Tab
sendKeys(Keys.ESCAPE); //回退键 ESC
sendKeys(Keys.ENTER); //回车键 Enter
sendKeys(Keys.F5); //键盘F5(可模拟F1-F12)
sendKeys(Keys.CONTROL,"a"); //全选 Ctrl+A
sendKeys(Keys.CONTROL,"c"); //复制 Ctrl+C
sendKeys(Keys.CONTROL,"x"); //剪切 Ctrl+X
sendKeys(Keys.CONTROL,"v"); //粘贴 Ctrl+V
... ...
示例如下:
driver.get("https://www.baidu.com"); driver.manage().window().maximize(); //加线程等待时间是为了让眼睛看的更清楚 Thread.sleep(5000); WebElement element = driver.findElement(By.id("kw")); element.sendKeys("软件自动化测试"); Thread.sleep(5000); element.sendKeys(Keys.BACK_SPACE); Thread.sleep(5000); element.sendKeys(Keys.BACK_SPACE); Thread.sleep(5000); element.sendKeys("模拟键盘输入"); Thread.sleep(5000); element.sendKeys(Keys.TAB); Thread.sleep(5000); element.sendKeys(Keys.ENTER); Thread.sleep(5000);
学习后总结,未完待续。。。
原文地址:https://www.cnblogs.com/xiaozhaoboke/p/11137683.html
时间: 2024-10-06 22:38:27