您的位置:首页 > 其它

关于Selenium的截图问题

2020-04-01 18:35 423 查看

截图方法

Broswer.get(url = 'https://www.baidu.com/')
Broswer.get_screenshot_as_file('.\img\\001.png')#必须以png结尾
Broswer.save_screenshot('.\img\\baidu.png')#必须以png结尾

源码

def save_screenshot(self, filename):
"""
Saves a screenshot of the current window to a PNG image file. Returns
False if there is any IOError, else returns True. Use full paths in
your filename.

:Args:
- filename: The full path you wish to save your screenshot to. This
should end with a `.png` extension.

:Usage:
driver.save_screenshot('/Screenshots/foo.png')
"""
return self.get_screenshot_as_file(filename)
def get_screenshot_as_file(self, filename):
"""
Saves a screenshot of the current window to a PNG image file. Returns
False if there is any IOError, else returns True. Use full paths in
your filename.

:Args:
- filename: The full path you wish to save your screenshot to. This
should end with a `.png` extension.

:Usage:
driver.get_screenshot_as_file('/Screenshots/foo.png')
"""
if not filename.lower().endswith('.png'):
warnings.warn("name used for saved screenshot does not match file "
"type. It should end with a `.png` extension", UserWarning)
png = self.get_screenshot_as_png()
try:
with open(filename, 'wb') as f:
f.write(png)
except IOError:
return False
finally:
del png
return True

从源码中我们可以看出,

save_screenshot
返回的是
get_screenshot_as_file
,所以两个方法本质一模一样。通过查看get_screenshot_as_file(),如果IO出错截图就会删除并且返回False

note
:一定要以
png
结尾否则会
warning

截图格式设置

#Gets the screenshot of the current window as a binary data.
Broswer.get_screenshot_as_png()

# Gets the screenshot of the current window as a base64 encoded stringwhich is useful in embedded images in HTML.
Broswer.get_screenshot_as_base64()

两种方式是对截图存储格式进行设置,分别是二进制格式base64编码格式存储。

官方解释为什么以base64编码格式存储?

以base64编码的字符串的形式获取当前窗口的屏幕截图,这在HTML中的嵌入图像中非常有用。

  • 点赞
  • 收藏
  • 分享
  • 文章举报
傻子丶疯子 发布了10 篇原创文章 · 获赞 0 · 访问量 534 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: