Python爬虫(4)-Selenium模拟鼠标操作

有目标就不怕路远。年轻人.无论你现在身在何方.重要的是你将要向何处去。只有明确的目标才能助你成功。没有目标的航船.任何方向的风对他来说都是逆风。因此,再遥远的旅程,只要有目标.就不怕路远。没有目标,哪来的劲头?一车尔尼雷夫斯基

导读:本篇文章讲解 Python爬虫(4)-Selenium模拟鼠标操作,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

在Selenium4.2以后的版本里面鼠标的操作方法都封装在了ActionChains中需要时直接取即可。

1.鼠标双击

使用方法就是调用ActionChains然后传入你需要点击的按钮的位置即可

ActionChains(driver).double_click(f1).perform()

perform()的意思就是执行所有ActionChains中的动作

具体的操作方法如下

from selenium.webdriver import Chrome, ChromeOptions, ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome()
action = ActionBuilder(driver)
driver.get("https://so.csdn.net/so/search?spm=1001.2101.3001.4498&q=XPATH%E5%AE%9A%E4%BD%8D&t=&u=&s=hot")
driver.refresh()
f1=driver.find_element(By.CSS_SELECTOR,'div.search-box')
ActionChains(driver).double_click(f1).perform()

2.鼠标单击

鼠标单击就是直接用click()即可,这种点一下操作主要是左键点击
右键点击就需要调用ActionChains,ActionChains(driver).context_click(f1).perform()

from selenium.webdriver import Chrome, ChromeOptions, ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome()
action = ActionBuilder(driver)
driver.get("https://so.csdn.net/so/search?spm=1001.2101.3001.4498&q=XPATH%E5%AE%9A%E4%BD%8D&t=&u=&s=hot")
driver.refresh()
f1=driver.find_element(By.CSS_SELECTOR,'div.search-box').click()#左键单击
ActionChains(driver).context_click(f1).perform()#右键单击

3.鼠标悬停于元素上

将鼠标移动到元素的上方

from selenium.webdriver import Chrome, ChromeOptions, ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome()
action = ActionBuilder(driver)
driver.get("https://so.csdn.net/so/search?spm=1001.2101.3001.4498&q=XPATH%E5%AE%9A%E4%BD%8D&t=&u=&s=hot")
driver.refresh()
f1=driver.find_element(By.CSS_SELECTOR,'div.search-box')
ActionChains(driver).move_to_element(f1).perform()

4.将鼠标移动到目标位置

此时鼠标位于窗口的左上方
action = ActionBuilder(driver)
action.pointer_action.move_to_location(10, 0)
action.perform()

from selenium.webdriver import Chrome, ChromeOptions, ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome()
action = ActionBuilder(driver)
driver.get("https://so.csdn.net/so/search?spm=1001.2101.3001.4498&q=XPATH%E5%AE%9A%E4%BD%8D&t=&u=&s=hot")
driver.refresh()
f1=driver.find_element(By.CSS_SELECTOR,'div.search-box')
ActionChains(driver).move_to_element(f1).perform()
action = ActionBuilder(driver)
action.pointer_action.move_to_location(8, 0)
action.perform()

5.将鼠标移动到指定位置

ActionChains(driver).move_by_offset(20,20).perform()

from selenium.webdriver import Chrome, ChromeOptions, ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome()
action = ActionBuilder(driver)
driver.get("https://so.csdn.net/so/search?spm=1001.2101.3001.4498&q=XPATH%E5%AE%9A%E4%BD%8D&t=&u=&s=hot")
driver.refresh()
f1=driver.find_element(By.CSS_SELECTOR,'div.search-box')
ActionChains(driver).move_to_element(f1).perform()
ActionChains(driver).move_by_offset(20,20).perform()

6.用鼠标拖放元素

将A元素移动到B元素的位置然后释放掉鼠标

from selenium.webdriver import Chrome, ChromeOptions, ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome()
action = ActionBuilder(driver)
driver.get("https://so.csdn.net/so/search?spm=1001.2101.3001.4498&q=XPATH%E5%AE%9A%E4%BD%8D&t=&u=&s=hot")
driver.refresh()
f1=driver.find_element(By.XPATH,'//*[@id="csdn-toolbar"]/div/div/div[1]/ul/li[2]/a')
f2=driver.find_element(By.XPATH, '//*[@id="csdn-toolbar"]/div/div/div[1]/ul/li[3]/a')
ActionChains(driver).drag_and_drop(f1,f2).perform()

可以清楚看到从下载移动到学习这个按钮上
在这里插入图片描述

7.鼠标拖动

这里的拖动需要给一个起始和结束位置,只要知道起始的位置和拖动结束位置鼠标就能够根据给的位置进行操作

也就是你需要知道窗口中x和y的值在窗口中的那个位置,以及你想要移动到的位置。

只要给定好范围,就能够进行鼠标在窗口中的拖动。

from selenium.webdriver import Chrome, ChromeOptions, ActionChains
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome()
action = ActionBuilder(driver)
driver.get("https://so.csdn.net/so/search?spm=1001.2101.3001.4498&q=XPATH%E5%AE%9A%E4%BD%8D&t=&u=&s=hot")
driver.refresh()
f1=driver.find_element(By.XPATH,'//*[@id="csdn-toolbar"]/div/div/div[1]/ul/li[2]/a')
start = f1.location
f2=driver.find_element(By.XPATH, '//*[@id="csdn-toolbar"]/div/div/div[1]/ul/li[3]/a').location
ActionChains(driver).drag_and_drop_by_offset(f1,f2['x'] - start['x'],f2['x']-start['y']).perform()

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/134082.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!