loading...

how to take a screenshot of your Selenium test with Python

tonetheman profile image Tony Colston ・1 min read

One thing you will want to do is save a screenshot from your Selenium test. You can use the image as an artifact to prove a test is passing or failing. An image of your test can also prove useful for debugging things visually.

from selenium import webdriver

driver = None
try:
    cpath = "e:\\projects\\sel\\chromedriver.exe" # note 1
    driver = webdriver.Chrome(cpath)
    driver.get("https://google.com")
    driver.save_screenshot("google.png") # note 2
finally:
    if driver is not None:
        driver.close()

Note 1 - I have my chromedriver.exe in the directory "e:\projects\sel", you will need to change this value to match where chromedriver.exe is located on disk.

Note 2 - This is the line where the image is generated. The name of the image is passed to the function in this case I named the image "google.png." You can choose any name you like.

The image format used is PNG (portable network graphics) https://en.wikipedia.org/wiki/Portable_Network_Graphics

The one thing to note that this picture will be "above the fold" and not a full page image.

Posted on by:

tonetheman profile

Tony Colston

@tonetheman

wanna be gamedev ... all around nerd and dev

Discussion

pic
Editor guide