我想截取网站整个页面的屏幕截图。以下是网站条件
body
标签内只是script
标签)我确实尝试了一些我能找到的解决方案,但没有一个能按我的意愿工作。以下是我尝试过的一些尝试
"message":"Cannot take screenshot with 0 height."
我猜这是因为内容是由 Javascript 呈现的。请注意,我已经给了它一些时间来渲染 time.sleep(5)
browser.save_screenshot()
的结果相同)。下面是我尝试过的代码。 def S(X): return browser.execute_script(
'return document.querySelector("#main-layout-content").scroll'+X
)
browser = webdriver.Chrome()
browser.get('some link go here')
browser.set_window_size(S('Width') + 100, S('Height') + 1000)
#The print statement below work
print(S('Height'))
browser.find_element_by_id('main-layout-content').screenshot('web_screenshot.png')
有什么办法可以实现我所需要的吗?
这是我获取网页全尺寸屏幕截图的方法
from selenium import webdriver
browser = webdriver.Chrome()
# We go to the webpage here and wait 2 seconds for fully load
browser.get('https://someURL')
time.sleep(2)
# Where to save the picture
directory = '/home'
# Video title
title = 'Hello world'
try:
# We try to get the top-level component in which all out page is its children
# This is different for each website
elem = browser.find_element_by_id('main-layout-content')
# Get the height of the element, and adding some height just to be safe
total_height = elem.size['height'] + 1000
# Set the window size - what is the size of our screenshot
# The width is hardcoded because the screensize is fixed for each computer
browser.set_window_size(1920, total_height)
# Wait for 2 seconds
time.sleep(2)
# Take the screenshot
browser.find_element_by_id(
'main-layout-content').screenshot(f'./{directory}/{title}.png')
except SystemError as err:
print('Take screenshot error at' + title)