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()。
相关推荐
gc_22996 小时前
学习Python中Selenium模块的基本用法(1:简介)
python·selenium
小马哥编程1 天前
如何解决 undetected_chromedriver 启动慢问题
chrome·selenium·ui
程序员小远2 天前
Pytest+Selenium UI自动化测试实战实例
自动化测试·软件测试·python·selenium·测试工具·ui·pytest
yy鹈鹕灌顶4 天前
Selenium+Java 自动化测试入门到实践:从环境搭建到元素操作
java·selenium·测试工具
是小崔啊6 天前
【爬虫】06 - 自动化爬虫selenium
爬虫·selenium·自动化
二向箔reverse7 天前
Selenium 攻略:从元素操作到 WebDriver 实战
python·selenium·测试工具
二向箔reverse7 天前
自动化测试工具 Selenium 入门指南
selenium·测试工具·自动化
山烛7 天前
深入了解 find_element 方法:Web 自动化定位元素的核心
前端·javascript·python·selenium·find_element
赴3358 天前
python网络爬虫之selenium库(二)
爬虫·python·selenium
WeiJingYu.9 天前
标题 “Python 网络爬虫 —— selenium库驱动浏览器
爬虫·python·selenium