当前位置: 首页 > 知识库问答 >
问题:

如何使用带硒的无头谷歌Chrome保存手机截图

梁锋
2023-03-14
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拍摄移动视图的截图?

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)

共有1个答案

徐洋
2023-03-14

如果你想拍摄网页的全屏截图,你可以用这个。

您遇到的问题描述不清楚,所以我假设您想隐藏滚动条并防止它们出现在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)
 类似资料:
  • 是否可以在Python中使用chromedriver和selenium对元素进行截图。我知道chrome开发工具允许我们对特定元素进行截图。即使是JavaScript方法,我也可以使用命令获取数据URI。

  • 这篇官方教程似乎过时了,因为根据这篇文章,ga.js被弃用了。 此外,在教程中,它有: 而在我有 在Chrome扩展中使用谷歌分析的最新正确方法是什么

  • 我在Chrome浏览器中使用Roboto字体时遇到问题。。具体来说,我似乎无法得到精简和薄/轻等字体权重。我下载所有3种完整字体: 然后我在声明中使用它们,比如: 然而,它给我的是普通的机器人。如果我改变“压缩”一个使用字体家族“机器人压缩”它的作品。。。这是有道理的,因为chrome采用显然为时已晚。但是,将字体系列更改为“roboto condensed”不会使用较轻的

  • 我知道这在以前是不可能的,但现在有了以下更新: 现在有可能在Java中使用Selenium吗?

  • storage和colab之间的链接如下所示:

  • 我正在开发一个颤动应用程序,它应该有一个通用的Web和移动设备代码库。 我的应用程序将有一个谷歌地图,据我所知,没有一个软件包可以满足所有平台。 所以很可能我必须使用这些独立的包创建两个独立的MapWidgets,一个用于web,一个用于移动。 对于移动设备: 对于web来说,它有点复杂,看起来实际上不是一个可用的版本,据我所知,(如果我错了请纠正我),它实际上使用了另一个不是由flutter团队