selenium鼠标操作实战

鼠标操作实战

鼠标单击操作 click()

python 复制代码
from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
ele = driver.find_element_by_link_text('新闻')
ele.click()  # 鼠标单击
sleep(2)
driver.quit()

内置鼠标操作包ActionChains

WebDriver封装了一套鼠标操作的包,我们先来看一下鼠标操作的流程。

  • 引入包:from selenium.webdriver.common.action_chains import ActionChains。
  • 定位元素,存储到某个变量:ele = driver.find_element_by_×××('××')。
  • 固定写法:ActionChains(driver).click(ele).perform(),如图 所示。

    ActionChains下的单机鼠标
python 复制代码
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep

driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
ele = driver.find_element_by_link_text('新闻')
# ele.click()  # 鼠标单击
ActionChains(driver).click(ele).perform()
sleep(2)
driver.quit()

鼠标双击操作double_click()

python 复制代码
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.get("http://sahitest.com/demo/clicks.htm")
ele = driver.find_element_by_xpath('/html/body/form/input[2]')
sleep(2)
# 通过double_click方法来模拟鼠标双击的操作
ActionChains(driver).double_click(ele).perform()
sleep(3)
driver.quit()

鼠标右击操作context_click()

python 复制代码
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.get("http://sahitest.com/demo/clicks.htm")
ele = driver.find_element_by_xpath('/html/body/form/input[4]')
sleep(2)
# 通过context_click方法模拟鼠标右击的操作
ActionChains(driver).context_click(ele).perform()

sleep(3)
driver.quit()

鼠标指针悬浮操作move_to_element(ele)

python 复制代码
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.get("http://sahitest.com/demo/mouseover.htm")
ele = driver.find_element_by_xpath('/html/body/form/input[1]')
sleep(2)
# 通过move_to_element方法模拟鼠标指针的悬浮操作
ActionChains(driver).move_to_element(ele).perform()

sleep(3)
driver.quit()

鼠标拖动操作drag_and_drop(source, target)

python 复制代码
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get("http://sahitest.com/demo/dragDropMooTools.htm")
source = driver.find_element_by_xpath('//*[@id="dragger"]')
target = driver.find_element_by_xpath('/html/body/div[5]')

sleep(2)
# 通过drag_and_drop方法模拟鼠标拖动操作
ActionChains(driver).drag_and_drop(source, target).perform()

sleep(3)
driver.quit()

其他鼠标操作汇总

  • 再来总结一下鼠标操作的流程。
    • 引入ActionChains包。
    • 定位要操作的元素。
    • 固定写法:ActionChains(driver).xxx(pars).perform()。
相关推荐
朱剑君7 小时前
第十五天:Selenium与PhantomJS
爬虫·python·selenium
伊一大数据&人工智能学习日志18 小时前
selenium爬取苏宁易购平台某产品的评论
爬虫·python·selenium·测试工具·网络爬虫
代码轨迹20 小时前
青龙面板运行selenium启动Chrome报错
chrome·python·selenium
起个破名想半天了1 天前
Web自动化之Selenium 超详细教程(python)
python·selenium·自动化
武陵悭臾2 天前
网络爬虫学习:借助DeepSeek完善爬虫软件,实现模拟鼠标右键点击,将链接另存为本地文件
python·selenium·网络爬虫·pyautogui·deepseek·鼠标右键模拟·保存链接为htm
2301_793069822 天前
Java和SQL测试、性能监控中常用工具
java·sql·selenium
刽子手发艺2 天前
Selenium+OpenCV处理滑块验证问题
opencv·selenium·webpack
bst@微胖子2 天前
Python高级语法之selenium
开发语言·python·selenium
奔跑吧邓邓子3 天前
【Python爬虫(14)】解锁Selenium:Python爬虫的得力助手
开发语言·爬虫·python·selenium