selenium自动化鼠标操作

鼠标操作类:ActionChains 模拟鼠标操作

from selenium.webdriver.common.action_chains import ActionChains

鼠标动作:动作放在一个动作列表中,动作一定要有执行(perform())要不不会执行

     双击:double_click()

     单击:click()

     右击:context_click()

     悬浮:mover_to_element() **重点

     按住鼠标的左键:click_and_hold  释放:release

     拖拽:drag_and_drop(传两个参数)

     执行动作:perform()

     暂停:pause(单位秒)

from selenium import webdriver
import time
# 鼠标
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.maximize_window()  # 最大化浏览器
driver.get("file:///E:/WebWebpageTest/page.html")

# 悬浮的元素 //button[text()="注册用户"]
# 1、实例化 ActionChains 类
ac = ActionChains(driver)
# 2、添加鼠标动作:调用对应的鼠标动作函数
ele = driver.find_element_by_xpath('//select[@id="select"]')
ac.move_to_element(ele).click(ele)  # 悬浮并点击,鼠标类可以链条式的操作,因为它返回的是自己本身
ac.double_click(ele)  # 双击
# 3、执行鼠标动作:perform()
ac.perform()
driver.quit()

猜你喜欢

转载自www.cnblogs.com/yongzhuang/p/12505680.html