如何使用 Selenium WebDriver 截取屏幕截图? [英] How can I take a screenshot with Selenium WebDriver?
问题描述
是否可以使用 Selenium WebDriver 截取屏幕截图?
Is it possible to take a screenshot using Selenium WebDriver?
(注意:不是Selenium Remote Control)
推荐答案
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屋!