Selenium 不截取整个网站的屏幕截图,当它不是无头的时候
Selenium does not take the screenshot of the whole website, when its not headless
免责声明:我知道,已经有一个类似的问题,但是 none 的答案适用于无头浏览器,所以我决定再制作 1 个更详细的答案(我的问题已经提到:)
大家好。
我偶然发现了一个看起来很简单但很难解决的问题。我需要在显示器上截取一个 NON-HEADLESS BROWSER 的屏幕截图,即 1920x1080(稍后会很重要),这将对整个网页进行屏幕截图,而不仅仅是您当前可以看到的部分。
我尝试了什么:
import os
import time
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument("--start-maximized")
chromedriver = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'chromedriver.exe')
chrome = webdriver.Chrome(chromedriver, options=chrome_options)
url = 'https://whosebug.com/'
chrome.get(url)
time.sleep(2)
total_height = chrome.execute_script("return document.body.parentNode.scrollHeight") + 1000
chrome.set_window_size(1920, total_height)
time.sleep(2)
chrome.save_screenshot("screenshot1.png")
chrome.quit()
^ 这个,与 Headless 一起工作得很好,不幸的是,当我删除 --headless
选项时,selenium 将尝试调整自身大小,但由于它试图调整到 1080
以上(height
)它立即调整为 1080
,这导致屏幕截图 1920x1080
。我需要以“理论上”的方式让 selenium 仅在截取屏幕截图时暂时 headless
(不幸的是,据我所知这是不可能的)。
在浏览器不是无头的情况下不起作用的其他常用方法:
el = driver.find_element_by_tag_name('body')
el.screenshot(path)
original_size = driver.get_window_size()
required_width = driver.execute_script('return document.body.parentNode.scrollWidth')
required_height = driver.execute_script('return document.body.parentNode.scrollHeight')
driver.set_window_size(required_width, required_height)
driver.find_element_by_tag_name('body').screenshot(path) # avoids scrollbar
driver.set_window_size(original_size['width'], original_size['height'])
element = chrome.find_element_by_tag_name('body')
element_png = element.screenshot_as_png
with open("test2.png", "wb") as file:
file.write(element_png)
带有 headless
选项
没有 headless
选项
您可以使用 Screenshot_Clipping 来滚动页面并从每个滚动中截取屏幕截图。
只需 运行 python3
中的这条命令
pip install Selenium-Screenshot
然后创建截图对象:
ob=Screenshot_Clipping.Screenshot()
使用该对象,您可以使用 ob.full_Screenshot
来捕获全屏
在 Github 上查看 PythonFullPageScreenShot 项目以获取完整源代码
请注意,屏幕截图只能截取网站高度的 10000,您可以缩放到 100 以截取全高
免责声明:我知道,已经有一个类似的问题,但是 none 的答案适用于无头浏览器,所以我决定再制作 1 个更详细的答案(我的问题已经提到:
大家好。
我偶然发现了一个看起来很简单但很难解决的问题。我需要在显示器上截取一个 NON-HEADLESS BROWSER 的屏幕截图,即 1920x1080(稍后会很重要),这将对整个网页进行屏幕截图,而不仅仅是您当前可以看到的部分。
我尝试了什么:
import os
import time
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument("--start-maximized")
chromedriver = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'chromedriver.exe')
chrome = webdriver.Chrome(chromedriver, options=chrome_options)
url = 'https://whosebug.com/'
chrome.get(url)
time.sleep(2)
total_height = chrome.execute_script("return document.body.parentNode.scrollHeight") + 1000
chrome.set_window_size(1920, total_height)
time.sleep(2)
chrome.save_screenshot("screenshot1.png")
chrome.quit()
^ 这个,与 Headless 一起工作得很好,不幸的是,当我删除 --headless
选项时,selenium 将尝试调整自身大小,但由于它试图调整到 1080
以上(height
)它立即调整为 1080
,这导致屏幕截图 1920x1080
。我需要以“理论上”的方式让 selenium 仅在截取屏幕截图时暂时 headless
(不幸的是,据我所知这是不可能的)。
在浏览器不是无头的情况下不起作用的其他常用方法:
el = driver.find_element_by_tag_name('body')
el.screenshot(path)
original_size = driver.get_window_size()
required_width = driver.execute_script('return document.body.parentNode.scrollWidth')
required_height = driver.execute_script('return document.body.parentNode.scrollHeight')
driver.set_window_size(required_width, required_height)
driver.find_element_by_tag_name('body').screenshot(path) # avoids scrollbar
driver.set_window_size(original_size['width'], original_size['height'])
element = chrome.find_element_by_tag_name('body')
element_png = element.screenshot_as_png
with open("test2.png", "wb") as file:
file.write(element_png)
带有 headless
选项
没有 headless
选项
您可以使用 Screenshot_Clipping 来滚动页面并从每个滚动中截取屏幕截图。
只需 运行 python3
中的这条命令pip install Selenium-Screenshot
然后创建截图对象:
ob=Screenshot_Clipping.Screenshot()
使用该对象,您可以使用 ob.full_Screenshot
来捕获全屏
在 Github 上查看 PythonFullPageScreenShot 项目以获取完整源代码
请注意,屏幕截图只能截取网站高度的 10000,您可以缩放到 100 以截取全高