Python 中 Selenium Webdriver 测试失败时的自动截图

2022-01-16 00:00:00 python selenium webdriver screenshot

问题描述

如果我的 webdriver 测试失败(任何异常或断言错误),我想自动捕获屏幕截图.我正在使用 Python unittest 和 Selenium Webdriver.有没有人可以解决这个问题?

I want to automatic capturing screenshots if my webdriver tests failed (any exception or assertion error). I am using Python unittest and Selenium Webdriver. Does anyone have any solution to this problem?


解决方案

在 Firefox 中做一些 webdriver 的东西...保存任何异常的截图到一个过时的图像文件:

do some webdriver stuff in Firefox... save screenshot on any exception to a dated image file:

from datetime import datetime
from selenium import webdriver

browser = webdriver.Firefox()

try:
    # do some webdriver stuff here
except Exception as e:
    print e
    now = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
    browser.get_screenshot_as_file('screenshot-%s.png' % now)

相关文章