(selenium+python)_UI自動化06_模擬鼠標事件


模擬鼠標事件

web網站常用鼠標事件為:點擊(click可實現)、右擊、雙擊、懸停、長按、拖動。在selenium中可以通過 ActionChains類實現模擬鼠標常用操作。

ActionChains類中鼠標常用方法:

1 context_click(element)  # 右擊
2 double_click(element)  # 雙擊
3 click_and_hold(element)  # 長按
4 move_to_element(element)  # 鼠標懸停在目標元素
5 move_by_offset(xoffset, yoffset)  # 鼠標懸停在目標坐標
6 drag_and_drop(source_ele, target_ele)  # 拖動

 備注:

1,使用時需導入ActionChains:from selenium.webdriver.common.action_chains import ActionChains

2,模擬事件后需添加.perform()才會執行操作

實例

模擬鼠標懸停

 1 # 模擬鼠標懸停實例
 2 from selenium.webdriver.common.action_chains import ActionChains
 3 from selenium import webdriver
 4 from time import sleep
 5  
 6 driver = webdriver.Chrome()
 7 driver.get('https://www.jd.com/')  #打開京東
 8 sleep(3)
 9  
10 # 模擬鼠標事件
11 tag_element = driver.find_element_by_xpath('//*[text()="我的京東"]')  # 菜單-我的京東
12 ActionChains(driver).move_to_element(tag_element).perform()  # 鼠標懸浮在-我的京東,展開子菜單


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM