web自动化之鼠标事件

鼠标操作
from selenium.webdriver.common.action_chains inport ActionChains
通过ActionChains 类来完成鼠标操作
主要操作流程:
1.存储鼠标操作
2.perfor()执行鼠标操作

常见的鼠标操作
double_click 双击
context_clik 右键操作
drag_and_drop 拖拽操作。 左键按住拖动某一个元素到另外一个元素,然后释放按键
move_to_element() 鼠标悬停

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as Ec
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains  # 鼠标操作的类

driver = webdriver.Chrome()

driver.get("https://www.baidu.com/")
# 窗口最大化
driver.maximize_window()
# 等待元素出现
WebDriverWait(driver,20).until(Ec.visibility_of_element_located((By.XPATH,'//div[@id="u1"]//a[text()="设置"]')))
# 鼠标悬浮到设置按钮上
web = driver.find_element_by_xpath('//div[@id="u1"]//a[text()="设置"]')
ac = ActionChains(driver)
ac.move_to_element(web)
#  点击操作
driver.find_element_by_xpath('//div[@id="u1"]//a[text()="设置"]').click()
# 等待元素出现
WebDriverWait(driver,20).until(Ec.visibility_of_element_located((By.XPATH,'//a[text()="高级搜索"]')))
# 点击高级搜索
driver.find_element_by_xpath('//a[text()="高级搜索"]').click()

猜你喜欢

转载自www.cnblogs.com/666666pingzi/p/10555983.html