0


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

在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()
标签: python 爬虫 selenium

本文转载自: https://blog.csdn.net/qq_16519957/article/details/128741090
版权归原作者 轻烟飘荡 所有, 如有侵权,请联系我们删除。

“Python爬虫(4)-Selenium模拟鼠标操作”的评论:

还没有评论