当前位置: 首页 > news >正文

从0开始python学习-23.selenium 常见鼠标的操作

注意:必须创建一个事件链对象(ActionChains);最后鼠标事件链完成之后一定要进行事务提交perform()(如果事件链不提交那么所有的鼠标事件都不会执行)
1. 左键单击:click()       
2. 在元素上按住鼠标左键,不松开:click_and_hold( on_element=None )            
3. 右击:context_click()             
4. 左键双击:double_click()             
5. 点击鼠标左键,按住不放:click_and_hold()         
6. 在某个元素位置松开鼠标左键:release(on_element=None) 
7. 拖动某个元素至目标元素处:drag_and_drop(source, target)   
8. 鼠标移动到某个元素:move_to_element(element)         
9. 鼠标移动到距离当前位置(x,y)的地方:move_by_offset(xoffset, yoffset) 
10.将鼠标移动到距某个元素多少距离的位置:move_to_element_with_offset(to_element, xoffset, yoffset)
11.执行所有 ActionChains 中存储的行为:perform()     
12.在源元素上按住鼠标左键,然后移动到目标偏移并释放鼠标按钮:drag_and_drop_by_offset(源, xoffset , yoffset )   
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import Keys, ActionChainsdriver = webdriver.Chrome()url = "https://www.baidu.com/"driver.get(url)driver.maximize_window ()# 输入框:<input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
# 百度一下:<input type="submit" id="su" value="百度一下" class="bg s_btn">
element_kw = driver.find_element(By.XPATH, '//*[@id="kw"]')
element_kw.send_keys('11112')
element_su = driver.find_element(By.XPATH,'//*[@id="su"]')
actions = ActionChains(driver)# 左键单击:click()
element_su.click()# 在元素上按住鼠标左键,不松开:click_and_hold( on_element=None )
# 在某个元素位置松开鼠标左键:release(on_element=None)
# 如果参数不写,那么点的是当前鼠标位置; 如果参数写定位到的元素对象element,那就是点这个元素
actions.click_and_hold(element_su).release(element_su).perform()# 右击:context_click()
actions.context_click(element_su).perform()# 左键双击:double_click()
actions.double_click(element_su).perform()# 拖动某个元素至目标元素处:drag_and_drop(source, target)
# source:鼠标按下的元素;target:鼠标向上的元素
url_drag = 'file:///D:/study/%E8%AF%BE%E5%A0%82%E8%B5%84%E6%96%99/14drag.html'
driver.get(url_drag)
el4 = driver.find_element(By.XPATH, '//*[@id="div1"]')
el5 = driver.find_element(By.XPATH, '//*[@id="div2"]')
actions.drag_and_drop(el4,el5).perform()# 鼠标移动到某个元素:move_to_element(element)
element_set = driver.find_element(By.LINK_TEXT,"设置")
actions.move_to_element(element_set).perform()# 鼠标移动到距离当前位置(x,y)的地方:move_by_offset(xoffset, yoffset)
# xoffset:要移动到的 X 偏移量,作为正整数或负整数。yoffset:要移动到的 Y 偏移量,作为正整数或负整数。
actions.move_by_offset(0,100).perform()  # 不能实现出来功能,有大佬懂的可以帮助下吗# 将鼠标移动到距某个元素多少距离的位置:move_to_element_with_offset(to_element, xoffset, yoffset)
element_set = driver.find_element(By.LINK_TEXT,"设置")
actions.move_to_element_with_offset(element_set,100,100).perform()# 在源元素上按住鼠标左键,然后移动到目标偏移并释放鼠标按钮:drag_and_drop_by_offset(源, xoffset , yoffset )
# source:鼠标按下的元素。xoffset:要移动到的 X 偏移量。yoffset:要移动到的 Y 偏移量。
url_drag = 'file:///D:/study/%E8%AF%BE%E5%A0%82%E8%B5%84%E6%96%99/14drag.html'
driver.get(url_drag)
el5 = driver.find_element(By.XPATH, '//*[@id="div2"]')
actions.click_and_hold(el5).perform() # 先用鼠标按住需要拖动的元素,不松开
actions.drag_and_drop_by_offset(el5,500, 500).perform() # 然后再进行元素的拖动sleep(10)
driver.quit()

http://www.lryc.cn/news/181286.html

相关文章:

  • 电气基础——电源、变压器、接触器、断路器、线缆
  • 步力宝科技爆款产品定位,开创智能物联网新商业
  • 凉鞋的 Unity 笔记 105. 第一个通识:编辑-测试 循环
  • Bug:elementUI样式不起作用、Vue引入组件报错not found等(Vue+ElementUI问题汇总)
  • 【大麦小米学量化】使用文心一言AI编写股票量化交易策略代码(含演示代码和进阶演示)
  • 软考 系统架构设计师系列知识点之软件架构风格(1)
  • 轮询与中断
  • 使用docker完成minio服务部署扩容备份迁移生产实践文档
  • 管道-有名管道
  • 谷歌注册手机号码无法验证
  • C语言编译与链接过程详解
  • Qt信号和槽 定时器
  • zemax对称式目镜
  • 层次架构、面向服务架构(四十四)
  • Ubuntu22无法自动进入lightdm图像界面
  • 01BFS最短距离的原理和C++实现
  • 【洛谷 P5266】【深基17.例6】学籍管理 题解(映射+分支)
  • 10.03
  • 链表单向链表跳跃链表
  • 博客无限滚动加载(html、css、js)实现
  • 腾讯云南京服务器性能如何?南京服务器测速IP地址
  • MySQL和Oracle中,语法的不同点以及如何在xml中书写日期比较大小
  • 谈谈Redis分布式锁
  • Redis的java客户端-RedisTemplate光速入门
  • 格点数据可视化(美国站点的日降雨数据)
  • YoloV8改进策略:LSKNet加入到YoloV8中,打造更适合小目标的YoloV8
  • 力扣-303.区域和检索-数组不可变
  • web:[极客大挑战 2019]LoveSQL
  • 数据结构—快速排序(续)
  • Snapdragon Profiler分析Android GPU