如何使用Selenium WebDriver截屏 [英] How to take screenshot with Selenium WebDriver

查看:156
本文介绍了如何使用Selenium WebDriver截屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道是否可以使用Selenium WebDriver截屏吗? (注意:不是Selenium RC)

Does anyone know if it's possible to take a screenshot using Selenium WebDriver? (Note: Not Selenium RC)

推荐答案

Python

每个WebDriver都有一个.save_screenshot(filename)方法.因此,对于Firefox来说,可以这样使用:

Python

Each WebDriver has a .save_screenshot(filename) method. So for Firefox, it can be used like this:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get('http://www.google.com/')
browser.save_screenshot('screenie.png')

令人困惑的是,还存在执行相同功能的.get_screenshot_as_file(filename)方法.

Confusingly, a .get_screenshot_as_file(filename) method also exists that does the same thing.

还有以下方法:.get_screenshot_as_base64()(用于嵌入html)和.get_screenshot_as_png()(用于检索二进制数据).

There are also methods for: .get_screenshot_as_base64() (for embedding in html) and .get_screenshot_as_png()(for retrieving binary data).

,请注意,WebElements具有.screenshot()方法,该方法的工作原理类似,但是仅捕获选定的元素.

and Note that WebElements have a .screenshot() method that works similarly, but only captures the selected element.

这篇关于如何使用Selenium WebDriver截屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆