• 从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 )   
    1. from time import sleep
    2. from selenium import webdriver
    3. from selenium.webdriver.common.by import By
    4. from selenium.webdriver import Keys, ActionChains
    5. driver = webdriver.Chrome()
    6. url = "https://www.baidu.com/"
    7. driver.get(url)
    8. driver.maximize_window ()
    9. # 输入框:
    10. # 百度一下:
    11. element_kw = driver.find_element(By.XPATH, '//*[@id="kw"]')
    12. element_kw.send_keys('11112')
    13. element_su = driver.find_element(By.XPATH,'//*[@id="su"]')
    14. actions = ActionChains(driver)
    15. # 左键单击:click()
    16. element_su.click()
    17. # 在元素上按住鼠标左键,不松开:click_and_hold( on_element=None )
    18. # 在某个元素位置松开鼠标左键:release(on_element=None)
    19. # 如果参数不写,那么点的是当前鼠标位置; 如果参数写定位到的元素对象element,那就是点这个元素
    20. actions.click_and_hold(element_su).release(element_su).perform()
    21. # 右击:context_click()
    22. actions.context_click(element_su).perform()
    23. # 左键双击:double_click()
    24. actions.double_click(element_su).perform()
    25. # 拖动某个元素至目标元素处:drag_and_drop(source, target)
    26. # source:鼠标按下的元素;target:鼠标向上的元素
    27. url_drag = 'file:///D:/study/%E8%AF%BE%E5%A0%82%E8%B5%84%E6%96%99/14drag.html'
    28. driver.get(url_drag)
    29. el4 = driver.find_element(By.XPATH, '//*[@id="div1"]')
    30. el5 = driver.find_element(By.XPATH, '//*[@id="div2"]')
    31. actions.drag_and_drop(el4,el5).perform()
    32. # 鼠标移动到某个元素:move_to_element(element)
    33. element_set = driver.find_element(By.LINK_TEXT,"设置")
    34. actions.move_to_element(element_set).perform()
    35. # 鼠标移动到距离当前位置(x,y)的地方:move_by_offset(xoffset, yoffset)
    36. # xoffset:要移动到的 X 偏移量,作为正整数或负整数。yoffset:要移动到的 Y 偏移量,作为正整数或负整数。
    37. actions.move_by_offset(0,100).perform() # 不能实现出来功能,有大佬懂的可以帮助下吗
    38. # 将鼠标移动到距某个元素多少距离的位置:move_to_element_with_offset(to_element, xoffset, yoffset)
    39. element_set = driver.find_element(By.LINK_TEXT,"设置")
    40. actions.move_to_element_with_offset(element_set,100,100).perform()
    41. # 在源元素上按住鼠标左键,然后移动到目标偏移并释放鼠标按钮:drag_and_drop_by_offset(源, xoffset , yoffset )
    42. # source:鼠标按下的元素。xoffset:要移动到的 X 偏移量。yoffset:要移动到的 Y 偏移量。
    43. url_drag = 'file:///D:/study/%E8%AF%BE%E5%A0%82%E8%B5%84%E6%96%99/14drag.html'
    44. driver.get(url_drag)
    45. el5 = driver.find_element(By.XPATH, '//*[@id="div2"]')
    46. actions.click_and_hold(el5).perform() # 先用鼠标按住需要拖动的元素,不松开
    47. actions.drag_and_drop_by_offset(el5,500, 500).perform() # 然后再进行元素的拖动
    48. sleep(10)
    49. driver.quit()

  • 相关阅读:
    基于vite3+tauri模拟QQ登录切换窗体|Tauri自定义拖拽|最小/大/关闭
    Java获取/resources目录下的资源文件方法
    VMware安装虚拟机(CentOS6)分配ip,并通过Xshell连接
    Go语言中的defer关键字
    竞赛 题目:基于深度学习卷积神经网络的花卉识别 - 深度学习 机器视觉
    [PAT练级笔记] 35 Basic Level 1035 插入与归并
    Hystrix 请求合并、请求隔离、优化
    Redis简介
    Spring Boot 中是使用 JDK Proxy 动态代理还是 CGLib ?
    人脸识别及检测
  • 原文地址:https://blog.csdn.net/Meseiter/article/details/133463997