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)ruby
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 }
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() # 結束
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