简体   繁体   English

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

[英]How to save mobile screenshot using Headless Google Chrome with Selenium

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')

Hello, The Image being taken by selenium is cut with page scroll (up/down & right/left) bars appearing, is there any way of taking screenshot of mobile view using selenium?您好,selenium 拍摄的图像被剪切并出现页面滚动(上/下和右/左)条,有没有什么方法可以使用 selenium 截取移动视图?

EDIT:1编辑:1

pastebin html test pastebin html 测试

for browser I adjust the width对于浏览器我调整宽度

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

for mobile用于手机

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

finally on both最后在两者上

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)

If you want to take a full-screen screenshot of the webpage, you can use this.如果你想对网页进行全屏截图,你可以使用这个。

The description of the problem you are experiencing isn't clear, so I'm assuming you want to hide the scroll bars and prevent them from appearing on the screenshot Selenium produces.您遇到的问题的描述不清楚,所以我假设您想隐藏滚动条并防止它们出现在 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么保存截图有时会在Selenium +无头Chrome + Python 2.7中被无限期阻止? - Why does save screenshot sometimes block indefinitely in Selenium + headless Chrome + Python 2.7? Selenium 使用“--headless”时 webdriver 屏幕截图不同 - Selenium webdriver screenshot is different when using '--headless' AttributeError: &#39;NoneType&#39; 对象在通过 Python 使用 Selenium 使用无头 Chrome 截取屏幕截图时没有属性 &#39;encode&#39; 错误 - AttributeError: 'NoneType' object has no attribute 'encode' error while taking screenshot with headless Chrome using Selenium through Python 如何使用 Google Chrome 和 Selenium 从无头模式切换到正常模式? - How can I switch from headless mode to normal mode using Google Chrome and Selenium? 如何使用 Selenium 和 Python 通过 Headless Chrome 使用 Chrome 配置文件 - How to use Chrome Profiles through Headless Chrome using Selenium and Python 使用python硒和Firefox或Chrome浏览器获取整个页面的截图 - Taking screenshot of whole page with python selenium and Firefox or Chrome headless Selenium 在 Chrome 中有效,但在使用无头 Chrome 时无效 - Selenium works in Chrome but not when using headless Chrome 如何使用 selenium webdriver 在无头 chrome 上允许通知 - How to allow notifications on headless chrome using selenium webdriver 如何使用 selenium、BeautifulSoup 和 headless Google 打印出来自 Google 的答案 - How to printout answer from Google using selenium, BeautifulSoup and headless Google Selenium 截取元素截图 - Google Chrome - Selenium take element screenshot - Google Chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM