gpt4 book ai didi

python - 如何限制 selenium python 中的屏幕截图高度?

转载 作者:太空宇宙 更新时间:2023-11-04 06:40:12 29 4
gpt4 key购买 nike

我想使用 Selenium python 获取完整网页的屏幕截图。为此,我使用了 this answer .

但现在我想分割屏幕截图并将生成的屏幕截图的高度限制为 30 000px。例如,如果网页高度为 40 000 像素,我想要第一个 30 000 像素的屏幕截图,然后是一个 10 000 像素的屏幕截图。

解决方案不应该是保存整页然后裁剪图像,因为我需要它来处理很长的网页。事实上,不可能获得高度为 120 000 像素的屏幕截图,否则会出现此类错误:

    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: [Exception... "Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: chrome://marionette/content/capture.js :: capture.canvas :: line 154" data: no]

我试过了,但它根本不起作用:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from time import sleep

def save_screenshot(d, path):
max_size = 30000
original_size = d.get_window_size()
required_width = d.execute_script('return document.body.parentNode.scrollWidth')+74
required_height = d.execute_script('return document.body.parentNode.scrollHeight')+74
if required_height <= max_size:
d.set_window_size(required_width, required_height)
d.find_element_by_tag_name('body').screenshot(path) # avoids scrollbar
else :
for i in range(0,int(required_height/max_size)):
d.set_window_position(0,max_size*i)
d.set_window_size(required_width, max_size)
d.find_element_by_tag_name('body').screenshot(path.replace(".png",str(i+1)+".png"))
d.set_window_position(0,max_size*int(required_height/max_size))
d.set_window_size(required_width, required_height%max_size)
d.find_element_by_tag_name('body').screenshot(path.replace(".png",str(int(required_height/max_size)+1)+".png"))
d.set_window_position(0,0)
d.set_window_size(original_size['width'], original_size['height'])

if __name__ == '__main__':
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options,executable_path=r"C:/Users/path/geckodriver.exe")
driver.get("http://www.lwb.com/")
sleep(3)
save_screenshot(driver,"C:/Users/path/test.png")
driver.close()

有人可以帮我吗?

谢谢,

米哈

最佳答案

您可以使用 pillow pip install pillow 裁剪保存的屏幕截图

def crop_image(img, img_limit):
img_width, img_height = img.size
crop_dim = (0, 0, img_width, img_limit) # left top right bottom
cropped_img = img.crop(crop_dim)
return cropped_img

在完成 save_screenshot(driver, "path/to/img") 之后,执行以下操作:

from PIL import Image

img_limit = 30000 # your image size limit
img = Image.open("path/to/img")
img = crop_image(img, img_limit)
img.save("path/to/img")

如果您不想在操作之前保存图像,您可以使用 get_screenshot_as_png,它将返回二进制数据而不是保存它:

from PIL import Image
from io import BytesIO

img_limit = 30000 # your image size limit
img_binary = driver.get_screenshot_as_png()
img = Image.open(BytesIO(img_binary))
img = crop_image(img, img_limit)
img.save("path/to/save")

当你完成从内存中删除二进制数据时,确保执行 del imgdel img_binary

为了截取整个页面的一个屏幕截图,请执行以下操作:

from selenium import webdriver

DRIVER_PATH = "path/to/chrome/driver"
URL = "site.url"

options = webdriver.ChromeOptions()
options.add_argument("headless")
driver = webdriver.Chrome(executable_path = DRIVER_PATH, chrome_options = options)

# setting a long window height to take one full screenshot
driver.set_window_size(1920, 90000) # width , height
driver.get(URL)
driver.maximize_window()

img_binary = driver.get_screenshot_as_png()

PS:如果你用这个方法截屏,就不需要pillow了。只需使用 set_window_size 设置您想要的窗口的高度和宽度,这将使您在屏幕截图中获得相同的大小

关于python - 如何限制 selenium python 中的屏幕截图高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55884479/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com