Python+Selenium - 鼠标操作
2024-10-20 00:50:58
鼠标操作类:action_chains模块的ActionChains类
使用组成:操作 + 执行(perform())
导入代码
from selenium.webdriver.common.action_chains import ActionChains
查看代码常用操作有:
click(self, on_element=None)
click_and_hold(self, on_element=None) #Holds down the left mouse button on an element.
context_click(self, on_element=None) #Performs a context-click (right click) on an element.
double_click(self, on_element=None)
drag_and_drop(self, source, target) #Holds down the left mouse button on the source element, then moves to the target element and releases the mouse button.
move_to_element(self, to_element) #Moving the mouse to the middle of an element.
release(self, on_element=None) #Releasing a held mouse button on an element.
perform() #调用这个函数执行鼠标操作
示例:
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.baidu.com/")
sleep(1)
#找到元素
loc = (By.ID,"s-usersetting-top")
ele = driver.find_element(*loc)
#对元素进行鼠标操作
#实例化ActionChains类
ac = ActionChains(driver)
# 把鼠标悬浮在这个元素
ac.move_to_element(ele)
# ac.move_to_element(ele).click(ele) #鼠标悬浮并点击。可把两个动作拆开写
#执行操作。鼠标操作最后要有一个perform(),执行perform()后面还有鼠标操作代码时,需要再调用一次perform()
ac.perform()
sleep(3)
driver.quit()
最新文章
- 【Go入门教程3】流程(if、goto、for、switch)和函数(多个返回值、变参、传值与传指针、defer、函数作为值/类型、Panic和Recover、main函数和init函数、import)
- JAVA jdbc获取数据库连接
- JavaScript DOM编程艺术学习笔记(一)
- jQuery插件Skippr实现焦点图
- 【javascript】html5中使用canvas编写头像上传截取功能
- 生产环境服务CPU高问题分析
- 【BZOJ 1491】 [NOI2007]社交网络
- c#基础知识对比(面向对象)
- 百度文本编辑器 Ueditor for net 使用七牛存储附件的实现
- 使用JS截取字符串函数详解
- Android应用程序框架层和系统运行库层日志系统源代码分析
- 一个很好的通用 excel 导出工具类
- SQLyog快捷键汇总
- Win10更新
- 读了这篇文章,你将变身web分析大师
- sqlserver打开对象资源管理器管理的帮助文档的快捷键
- Jquery——hover与toggle
- IRing项目开发
- django数据库基本操作-增删改查(tip)-基本
- TOSCA自动化测试工具--怎么写自动化用例