Web自动化测试——代码篇五常用方法——鼠标键盘事件
Posted Q同码
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Web自动化测试——代码篇五常用方法——鼠标键盘事件相关的知识,希望对你有一定的参考价值。
Java | Python | Ruby | ||
鼠标事件 | 右击 | Actions action = new Actions(driver); action.contextClick(inputBox).build().perform() |
ActionChains(driver).context_click(元素).perform() | driver.action.context_click(元素).perform |
双击 | action.doubleClick(元素).build().perform() | ActionChains(driver).double_click(元素).perform() | driver.action.double_click(元素).perform | |
左击 | action.clickAndHold(元素).build().perform() | ActionChains(driver).click_and_hold(元素).perform() | driver.action.click_and_hold(元素).perform | |
拖动 | action.dragAndDrop(源元素,目标元素).build().perform(); | ActionChains(driver).drag_and_drop(源元素,目标元素).perform() | driver.action.drag_and_drop(源元素,目标元素).perform | |
悬停 | action.moveToElement(driver.findElement(元素).build().perform() | ActionChains(driver).move_to_element(元素).perform() | driver.action.move_to(元素).perform | |
键盘事件 | 回退键(Esc) | action.sendKeys(元素, Keys.ESCAPE).perform() | 元素.send_keys(Keys.ESCAPE) | 元素.send_keys :escape |
键盘F1 | action.sendKeys(元素, Keys.F1).perform() | 元素.send_keys(Keys.F1) | 元素.send_keys :f1 | |
Home键 | action.sendKeys(元素, Keys.HOME).perform() | 元素.send_keys(Keys.HOME) | 元素.send_keys :home | |
End键 | action.sendKeys(元素, Keys.END).perform() | 元素.send_keys(Keys.END) | 元素.send_keys :end | |
Insert键 | action.sendKeys(元素, Keys.INSERT).perform() | 元素.send_keys(Keys.INSERT) | 元素.send_keys :insert | |
Delete键 | action.sendKeys(元素, Keys.DELETE).perform() | 元素.send_keys(Keys.DELETE) | 元素.send_keys :delete | |
删除键(Backspace) | action.sendKeys(元素, Keys.BACK_SPACE).perform() | 元素.send_keys(Keys.BACK_SPACE) | 元素.send_keys :backspace | |
制表键(Tab) | action.sendKeys(元素, Keys.TAB).perform() | 元素.send_keys(Keys.TAB) | 元素.send_keys :tab | |
空格键(Space) | action.sendKeys(元素, Keys.SPACE).perform() | 元素.send_keys(Keys.SPACE) | 元素.send_keys :space | |
回车键(Enter) | action.sendKeys(元素, Keys.ENTER).perform() | 元素.send_keys(Keys.ENTER) | 元素.send_keys :enter | |
Shift键 | action.sendKeys(元素, Keys.SHIFT).perform() | 元素.send_keys(Keys.SHIFT) | 元素.send_keys :shift | |
Alt键 | action.sendKeys(元素, Keys.ALT).perform() | 元素.send_keys(Keys.ALT) | 元素.send_keys :alt | |
Page Up键(PgUp) | action.sendKeys(元素, Keys.PAGE_UP).perform() | 元素.send_keys(Keys.PAGE_UP) | 元素.send_keys :page_up | |
Page Down键(PgDn) | action.sendKeys(元素, Keys.PAGE_DOWN).perform() | 元素.send_keys(Keys.PAGE_DOWN) | 元素.send_keys :page_down | |
上键(↑) | action.sendKeys(元素, Keys.UP).perform() | 元素.send_keys(Keys.UP) | 元素.send_keys :up | |
下键(↓) | action.sendKeys(元素, Keys.DOWN).perform() | 元素.send_keys(Keys.DOWN) | 元素.send_keys :down | |
左键(←) | action.sendKeys(元素, Keys.LEFT).perform() | 元素.send_keys(Keys.LEFT) | 元素.send_keys :enter | |
右键(→) | action.sendKeys(元素, Keys.RIGHT).perform() | 元素.send_keys(Keys.RIGHT) | 元素.send_keys :left | |
全选(Ctrl+A) | driver.findElement(By.id("kw")).sendKeys(Keys.chord(Keys.CONTROL,"a")) | 元素.send_keys(Keys.CONTROL,‘a‘) | 元素.send_keys [:control, ‘a‘] | |
复制全选(Ctrl+C) | driver.findElement(By.id("kw")).sendKeys(Keys.chord(Keys.CONTROL,"c")) | 元素.send_keys(Keys.CONTROL,‘c‘) | 元素.send_keys [:control, ‘c‘] | |
剪切全选(Ctrl+X) | driver.findElement(By.id("kw")).sendKeys(Keys.chord(Keys.CONTROL,"x")) | 元素.send_keys(Keys.CONTROL,‘x‘) | 元素.send_keys [:control, ‘x‘] | |
粘贴全选(Ctrl+V) | driver.findElement(By.id("kw")).sendKeys(Keys.chord(Keys.CONTROL,"v")) | 元素.send_keys(Keys.CONTROL,‘v‘) | 元素.send_keys [:control, ‘v‘] | |
数字1 | action.sendKeys(元素, Keys.NUMPAD1).perform() | 元素.send_keys(Keys.NUMPAD1) | 元素.send_keys :numpad1 | |
在调用部分方法之前需要导入对应的包 Java: import org.openqa.selenium.interactions.Actions Python: ↓↓↓o( o`ω′)ツ┏━┓(敲桌子~~~)注意往下看: |
**代码时间 **
( ̄??)hahahaha事件有点多,这里就挑几个来跑个代码(有兴趣的可以自己全部实验一下(^U^)ノYO)
Java
1 package JavaTest; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.Keys; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.firefox.FirefoxDriver; 7 import org.openqa.selenium.interactions.Actions; 8 9 public class Test { 10 public static void main(String[] arg) throws InterruptedException 11 { 12 WebDriver driver = new FirefoxDriver(); 13 driver.get("http://www.baidu.com/"); 14 15 Actions action = new Actions(driver); 16 driver.findElement(By.id("kw")).sendKeys("Java1"); //对百度输入框赋值 17 Thread.sleep(2000); 18 action.sendKeys(driver.findElement(By.id("kw")), Keys.BACK_SPACE).perform(); //删除键 19 action.sendKeys(driver.findElement(By.id("kw")), Keys.ENTER).perform(); //回车键 20 Thread.sleep(2000); 21 driver.findElement(By.id("kw")).sendKeys(Keys.chord(Keys.CONTROL,"a")); //复制 22 Thread.sleep(2000); 23 driver.findElement(By.id("kw")).sendKeys(Keys.chord(Keys.CONTROL,"x")); //剪切 24 Thread.sleep(2000); 25 driver.close(); //结束 26 } 27 }
Python
1 from time import * 2 from selenium import webdriver 3 from selenium.webdriver.common.by import By 4 from selenium.webdriver.common.keys import Keys 5 6 # 启动Firefox浏览器 7 driver = webdriver.Firefox() 8 driver.get(‘http://www.baidu.com‘) 9 10 driver.find_element(By.XPATH,"//*[@class=‘s_ipt‘]").send_keys(‘Python1‘) # 对百度输入框赋值 11 sleep(2) 12 driver.find_element(By.ID,‘kw‘).send_keys(Keys.BACK_SPACE) # 删除键 13 driver.find_element_by_xpath("//input[@type=‘submit‘]").send_keys(Keys.ENTER) # 回车键 14 sleep(2) # 休眠2秒 15 driver.find_element(By.ID,‘kw‘).send_keys(Keys.CONTROL, ‘A‘) # 复制 16 sleep(2) # 休眠2秒 17 driver.find_element(By.ID,‘kw‘).send_keys(Keys.CONTROL, ‘X‘) # 剪切 18 sleep(2) 19 20 driver.close() # 结束
Ruby
1 class Baidu 2 require ‘rubygems‘ 3 require ‘selenium-webdriver‘ 4 5 #打开firefox并输入网址 6 driver = Selenium::WebDriver.for :firefox 7 driver.navigate.to "http://www.baidu.com" 8 9 driver.find_element(:name, ‘wd‘).send_keys(‘Ruby1‘) # 对百度输入框赋值 10 sleep(2) # 休眠2秒 11 driver.find_element(:id, ‘kw‘).send_keys :backspace # 删除键 12 driver.find_element(:id, ‘kw‘).send_keys :enter # 回车键 13 sleep(2) 14 driver.find_element(:xpath, "//*[@class=‘s_ipt‘]").send_keys [:control,‘a‘] # 复制 15 sleep(2) 16 driver.find_element(:id, ‘kw‘).send_keys [:control,‘x‘] # 剪切 17 sleep(2) 18 19 driver.close #退出程序 20 end
以上是关于Web自动化测试——代码篇五常用方法——鼠标键盘事件的主要内容,如果未能解决你的问题,请参考以下文章
Selenium02篇python+selenium实现Web自动化:鼠标操作和键盘操作!