Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

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

1 Answer

0 votes
by (62.9k points)

Use the following code using Firefox WebDriver which saves a 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)

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium certification

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...