selenium模拟鼠标滚动
时间: 2023-09-04 12:09:36 浏览: 267
可以使用Selenium中的ActionChains类来模拟鼠标滚动。下面是一个示例代码:
```
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get("https://www.example.com")
# 模拟鼠标滚动
actions = ActionChains(driver)
actions.move_to_element(driver.find_element_by_tag_name("body")).perform()
actions.click_and_hold().move_by_offset(0, 100).release().perform()
driver.quit()
```
在上面的例子中,首先创建了一个webdriver实例,并打开了一个网页。然后使用ActionChains类创建了一个动作对象,并将鼠标移动到页面的body元素上。接着,使用click_and_hold()方法模拟鼠标左键按下,然后使用move_by_offset()方法向下移动100个像素,最后使用release()方法模拟鼠标左键松开。最后,使用perform()方法执行这些动作。
如果需要模拟鼠标向上滚动,可以将move_by_offset()方法中的y值改为负数。例如:
```
actions.click_and_hold().move_by_offset(0, -100).release().perform()
```
阅读全文
相关推荐


















