Python WebDriver 模拟下拉页面内容 - 使用 ActionChains 模块
使用 Python WebDriver 和 ActionChains 模块可以轻松模拟下拉页面内容。以下代码演示了如何使用 ActionChains 模块模拟下拉操作:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
# 执行一些操作,使页面出现滚动条
# ...
# 模拟下拉操作
actions = ActionChains(driver)
actions.move_to_element(driver.find_element_by_tag_name('body')).click_and_hold().perform()
actions.move_by_offset(0, 100).perform()
actions.release().perform()
代码解析:
move_to_element()方法将鼠标移动到指定元素上,这里选择了页面的body元素;click_and_hold()方法模拟按下鼠标左键并保持不放;move_by_offset()方法将鼠标向下移动指定距离,这里为 100 个像素;release()方法模拟释放鼠标左键。
通过多次执行这个操作,可以模拟连续的下拉操作。
例如,可以将 move_by_offset(0, 100) 中的 100 替换成一个变量,并通过循环不断改变这个变量的值,以实现不同的下拉距离。
原文地址: https://www.cveoy.top/t/topic/oXS7 著作权归作者所有。请勿转载和采集!