Selenium滚动屏幕截图

2024-09-26 17:57:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python脚本,它贯穿整个UI并填充很多页面。我拍了一张截图,但是如果页面比屏幕长,那么我会错过一半的页面。我尝试了更长的篇幅,但对于普通的页面,这是荒谬的,无法使用。我尝试使用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);")

Tags: 项目脚本actionsuiexecute屏幕driverscript
1条回答
网友
1楼 · 发布于 2024-09-26 17:57:46

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

一个简单的例子是:

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()

相关问题 更多 >

    热门问题