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()。
相关推荐
就叫飞六吧6 小时前
Python自动化selenium-一直卡着不打开浏览器怎么办?
python·selenium·自动化
天天进步201517 小时前
Selenium的ActionChains:自动化Web交互的强大工具
前端·selenium·自动化
demodeom1 天前
常见浏览器 WebDriver 驱动下载
selenium·webdriver
高耳机High-Earphone2 天前
多人五子棋联机对战平台 测试报告
自动化测试·selenium·性能测试·ssm项目·五子棋对战·项目测试·梯度压力测试
2401_897930064 天前
Selenium 入门介绍
selenium·测试工具
TOWNST5 天前
Python Selenium 一小时速通教程
开发语言·python·selenium
晓13135 天前
第三章 爬虫提速、selenium模块、requests模块进阶(终)
爬虫·python·selenium·测试工具·http
亿牛云爬虫专家7 天前
浏览器自动化检测对抗:修改navigator.webdriver属性的底层实现
python·selenium·自动化·爬虫代理·amazon·代理ip·playwright
GGBondlctrl7 天前
【自动化测试】如何获取cookie,跳过登录的简单操作
开发语言·自动化测试·selenium·绕过验证方法
小白学大数据8 天前
Scrapy结合Selenium实现搜索点击爬虫的最佳实践
开发语言·chrome·爬虫·selenium·scrapy