首页 > 解决方案 > Selenium - 滚动屏幕截图

问题描述

我有一个通过 UI 并填写很多页面的 python 脚本。我截图了,但是如果页面比屏幕长,那么我错过了一半的页面。我尝试了更长的长度,但对于普通页面,它是荒谬且无法使用的。我尝试使用 ashot,但不知道如何将其添加到我在 pycharm 中的项目中。

有什么智慧或其他想法吗?谢谢。

更新尝试但不起作用的事情(页面上没有发生任何事情:

actions = ActionChains(driver)
actions.move_to_element(continuebtn)

还:

chrome_options.add_argument('--enable-javascript')
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
driver.execute_script("window.scrollTo(0, 1080);")

标签: pythonseleniumscreenshot

解决方案


您可以截取多个屏幕截图并滚动浏览整个页面。

一个简单的例子是:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.firefox.options import Options
import time

d = webdriver.Firefox(executable_path="PATH TO YOUR DRIVER")
d.get("https://en.wikipedia.org/wiki/HTTP_cookie")
time.sleep(1)
scrollHeight = d.execute_script("return window.scrollMaxY")
index = 0
while d.execute_script("return window.pageYOffset") < scrollHeight:
    d.save_screenshot(PICTURE_PATH+"/"+ FILENAME + str(index).zfill(2) + ".png")
    d.execute_script("window.scrollByPages(1)")
    # maybe call time.sleep in here somewhere to load lazyloaded images
    time.sleep(0.2)
    index += 1
d.quit()

推荐阅读