![](/img/trans.png)
[英]Take screenshot of full page with Selenium Python with chromedriver
[英]Take screenshot of full web page with Selenium Python with Chrome driver
我想截取一個網站的整個頁面。 以下是網站條件
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 sage
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.