當需要模擬鍵盤或者滑鼠操作時,Python需要使用 ActionChains 來處理,Java需要 Actions 來處理,
常用模擬滑鼠的行為,比如單擊,雙擊,拖動等,當呼叫 ActionChains 或者 Actions 的方法時,會將所有操作按順序存入佇列,當呼叫 perform() 方法時,佇列中的事件會依次執行,
引入依賴
- Python 版本
# 引入依賴
from selenium.webdriver import ActionChains
- Java版本
import org.openqa.selenium.interactions.Actions;
實戰演示
點擊相關操作
下面代碼中,action是模擬鍵盤或者滑鼠的實體物件,on_element 是需要傳遞一個元素進去,默認值為 None,
單擊指定元素,如果不指定,會單擊當前游標的位置
- Python 版本
action.click(on_element=None)
- Java版本
Actions action = new Actions(webDriver);
action.click(on_element=None);
長按某個元素
- Python 版本
action.click_and_hold(on_element=None)
- Java版本
Actions action = new Actions(webDriver);
action.clickAndHold(on_element=None);
執行右鍵操作
- Python 版本
action.context_click(on_element=None)
- Java版本
Actions action = new Actions(webDriver);
action.contextClick(on_element=None);
執行左鍵雙擊
- Python 版本
action.double_click(on_element=None)
- Java版本
Actions action = new Actions(webDriver);
action.doubleClick(on_element=None);
拖拽起始的元素到目標元素,即 source 到 target
- Python 版本
action.drag_and_drop(source, target)
- Java版本
Actions action = new Actions(webDriver);
action.dragAndDrop(WebElement source, WebElement target);
將目標拖動到指定的位置
- Python 版本
# xoffset 和 yoffset 是相對于 source 左上角為原點的偏移量
action.drag_and_drop_by_offset(source, xoffset, yoffset)
- Java版本
Actions action = new Actions(webDriver);
actions.dragAndDropBy(WebElement source, int xOffset, int yOffset);
按鍵
使用這個方法可以方便的實作某些組合鍵盤事件,比如按下 ctrl+c 鍵,
- Python 版本
action.key_down(value, element=None)
- Java版本
Actions action = new Actions(webDriver);
actions.keyDown(element, value);
松開某個鍵,可以配合上面的方法實作按下 ctrl+c 并且釋放
- Python 版本
ActionChains(driver).key_down(Keys.CONTROL)\
.send_keys('c').key_up(Keys.CONTROL).perform()
- Java版本
Actions action = new Actions(webDriver);
action.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).perform();
其他按鍵請參考:7.4 特殊字符 - selenium-python中文檔案
github 參考地址:selenium/keys.py at 916168f403dded05f878fe189d68c0f9152335c9 · SeleniumHQ/selenium · GitHub
移動
指定游標移動到某一個位置,需要給出兩個坐標位置
- Python 版本
# xoffset 和 yoffset 是相對于網頁左上角的偏移量
action.move_by_offset(xoffset, yoffset)
- Java版本
Actions action = new Actions(webDriver);
action.moveByOffset(xOffset,yOffset);
將滑鼠移動到指定元素的位置
- Python 版本
action.move_to_element(to_element)
- Java版本
Actions action = new Actions(webDriver);
action.moveToElement(to_element);
移動滑鼠到相對于某個元素的偏移位置
- Python 版本
# xoffset 和 yoffset 是相對于 to_element 左上角的偏移量
action.move_to_element_with_offset(to_element, xoffset, yoffset)
- Java版本
Actions action = new Actions(webDriver);
action.moveToElement(to_element, xOffset, yOffset);
其它
執行 ActionChains 中的操作
前面介紹的方法會將所有操作按順序存入佇列,要執行這些操作,需要呼叫 perform() 方法,
- Python 版本
action.move_to_element_with_offset(to_element, xoffset, yoffset).perform()
- Java版本
Actions action = new Actions(webDriver);
action.moveToElement(to_element, int xOffset, int yOffset).perform();
釋放按下的滑鼠
- Python 版本
action.release(on_element=None)
- Java版本
Actions action = new Actions(webDriver);
action.release(on_element=None)
向焦點元素位置輸入值
焦點元素:使用 tab 鍵,那些被選中的元素就是焦點元素,
- Python 版本
action.send_keys(*keys_to_send)
- Java版本
Actions action = new Actions(webDriver);
action.sendKeys(*keys_to_send)
向指定的元素輸入資料
- Python 版本
action.send_keys_to_element(element, *keys_to_send)
- Java版本
喜歡軟體測驗的小伙伴們,如果我的博客對你有幫助、如果你喜歡我的博客內容,請 “點贊” “評論” “收藏” 一鍵三連哦,更多技術文章Actions action = new Actions(webDriver); action.sendKeys(element,keys_to_send);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/472279.html
標籤:其他
