如何使用带有 Selenium 的无头 Google Chrome 保存手机屏幕截图

How to save mobile screenshot using Headless Google Chrome with Selenium

提问人:goku 提问时间:6/26/2020 最后编辑:goku 更新时间:3/15/2023 访问量:1152

问:

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
mobile_emulation = {
                "deviceMetrics": {"width": 360, "height": 640, "pixelRatio": 3.0},
                "userAgent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1"}
options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(options=options)
driver.get('https://stackoverflow.com/')
driver.save_screenshot('test.png')

您好,selenium拍摄的图像被剪切,并出现页面滚动(上/下和右/左)条,有没有办法使用selenium截取移动视图的屏幕截图?

编辑:1

PasteBin HTML 测试

对于浏览器,我调整宽度

required_width = driver_selected.execute_script('return document.body.parentNode.scrollWidth')

对于移动设备

required_width = driver_selected.get_window_size().get('width') # Keep same

最后在两者上

required_height = driver_selected.execute_script('return document.body.parentNode.scrollHeight')
driver_selected.set_window_size(required_width, required_height)
driver_selected.find_element_by_tag_name('body').screenshot(png_file)
python selenium selenium-chromedriver google-chrome-headless mobileemulation

评论

0赞 linw1995 6/29/2020
您的环境信息是什么?此映像是在我的环境中没有滚动条的情况下生成的(MacOS 10.15.5、selenium 3.141.0、chromedriver 83.0.4103.39)
0赞 Lucan 6/29/2020
我也无法复制Chrome 83.0.4103.116(x64;WinX的);Selenium 3.141.0,Python 3.8.3。请提供更多详情。
0赞 goku 6/29/2020
嗨,@Lucan栏只是问题的一部分,有些网站在浏览器或移动设备中打开时会有不同的视图。但是,当我使用此代码打开时,我得到相同的结果,但被切断了
0赞 Lucan 6/29/2020
你能提供一些例子,你得到的和你期望的吗?您的代码生成的屏幕截图与我在 iPhone 上访问该站点时看到的屏幕截图相同。
0赞 goku 6/29/2020
@Lucan大多数示例都是本地 HTML,但对于移动设备和浏览器(桌面)之间不同的任何网站来说,问题都是一样的

答:

0赞 idontknow 11/20/2020 #1

如果要对网页进行全屏截图,可以使用此功能。

您遇到的问题的描述尚不清楚,因此我假设您想隐藏滚动条并防止它们出现在 Selenium 生成的屏幕截图上。

"""Take a full-page screenshot of the browser"""

# Save the original window size so we can restore it later
original = self.driver.get_window_size()
# Format the original in a tuple object we can use later
original = (original['width'], original['height'],)

# Get the height that's needed in order to fully render the page
newheight = int(self.driver.execute_script("""
return Math.max(
    document.body.scrollHeight,
    document.body.offsetHeight,
    document.documentElement.clientHeight,
    document.documentElement.scrollHeight,
    document.documentElement.offsetHeight
);
"""))

# Set the new height
# Most responsive webpages handle width flawlessly, so you can use a constant width
# You can also set it dynamically if you wish
self.driver.set_window_size(1000, newheight)

# Hide the main scrollbar using some css tricks
# You can also inject a
# <style>* {overflow: hidden}</style>
# to hide all scrollbars if you want 
self.driver.execute_script("""
document.body.parentElement.style.overflow = "hidden";
""")

# b64ss = self.driver.get_screenshot_as_base64()
# print(b64ss)
# The screenshot must be saved to a file because base64 has
# size restrictions that usually cause an incomplete screenshot

self.driver.save_screenshot('mobile_fullpage_screenshot.png')

# Restore the original window sizes
self.driver.set_window_size(*original)
0赞 Berkay Kirmizioglu 3/15/2023 #2

这应该有效

from selenium import webdriver


def full_screenshot(driver, save_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.save_screenshot(save_path)
    driver.set_window_size(original_size['width'], original_size['height'])


options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
mobile_emulation = {
    "deviceMetrics": {"width": 360, "height": 640, "pixelRatio": 3.0},
    "userAgent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1"
}
options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(options=options)
driver.set_window_size(360, 640)  # Set the window size to match the mobile viewport dimensions
driver.get('https://stackoverflow.com/')

full_screenshot(driver, 'test.png')
driver.quit()

full_screenshot 函数调整窗口大小以匹配页面的尺寸,截取屏幕截图,然后恢复原始窗口大小。这应该可以帮助您捕获没有滚动条的整个页面的屏幕截图。