|
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: from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys ↓↓↓o( o`ω′)ツ┏━┓(敲桌子~~~)注意往下看: 在Java中滑鼠事件和鍵盤單鍵事件中需要引用的是剛才所說的包。為什麼我不在鍵盤按鍵組合事件中沒有採用該方法呢?因為我嘗試了多次都沒有成功(如果各位小夥伴知道該方法的按鍵組合使用方法請留言呦~),所以在鍵盤按鍵組合事件採用的了另一個方法(該方法不需要匯入Actions包),該方法是同樣適用於鍵盤單鍵事件的。 |