DEV Community

Tony Colston
Tony Colston

Posted on

2

how to take a screenshot of your Selenium test with Python

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()
Enter fullscreen mode Exit fullscreen mode

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.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay