selenium的元素定位-鼠标事件

鼠标事件

ActionChains 类提供了鼠标操作的常用方法:

perform(): 执行所有 ActionChains 中存储的行为;

context_click(): 右击;

double_click(): 双击;

drag_and_drop(source, target): 拖动;

move_to_element(): 鼠标悬停

参照百度页面鼠标悬浮定位元素

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.get(url='https://www.baidu.com')
#最大化
driver.maximize_window()
#找到设置按钮
shezhi = driver.find_element_by_xpath("//div[@id='u1']/a[@class='pf']")
#鼠标悬浮
ActionChains(driver).move_to_element(shezhi).perform()
#定位设置-搜索设置
# sousuo = driver.find_element_by_xpath("//a[@class='setpref']")
# ActionChains(driver).move_to_element(sousuo).click().perform()
"""
ActionChains(driver) 实例化对象
move_to_element(sousuo) 移动到定位的元素上面
click()点击的动作
perform()执行整个操作的最后提交动作
"""
driver.find_element_by_xpath("//a[@class='setpref']").click() #等同于上面2行
原文地址:https://www.cnblogs.com/xinjing-jingxin/p/9299835.html