How to take Screenshots using Python and Selenium

Related Product On This Page Benefits of Screenshot Testing With PythonJune 08, 2026 · 7 min read · Tool Comparison

Related Product

How to conduct Screenshots using Python and Selenium

Taking screenshots in Selenium with Python helps document the browser state during examination trial. It support debugging, layout check, and optical trailing across different point of your mechanisation.

Overview

When to Take a Screenshot in Selenium?

Screenshots are often used during mechanization to capture the browser state for analysis, proof, or reporting. Use them in the following situations:

  • During a tryout failure to log the exact UI state at the point of mistake
  • After major interactions to confirm ask changes on the page
  • While debug matter with layout or element emplacement
  • When generating visual log for test reports or follow-up

How to Take Screenshots in Selenium Using Python?

Selenium supports multiple screenshot method, including:

  • Visible screen with save_screenshot:Use this to capture the part of the page currently in perspective.
  • Full page with full_Screenshot (): Use this to capture the integral scrollable page.
  • Specific element with element.screenshot:Capture entirely a targeted factor like a push, fault message, or container.

Benefits of Screenshot Testing With Python

helps you visually track UI changes and get layout matter that might miss. Below are key advantages of using screenshot try with Python:

  • Catch layout bugs:A tryout might pass all functional checks. but the UI could still be broken or misaligned. Screenshots help spot these visual issue that aren ’ t caught by normal asseveration.
  • Simplify debugging:When a exam fails, witness a screenshot of the browser helps you quickly understand what went wrong without guessing from logs entirely.

Read More:

  • Track UI changes:Screenshots let you compare the app ’ s appearance over clip to catch unexpected design change after updates or deployment.
  • Improve reporting:Including screenshots in test story gives your team visual context to better understand matter and accelerate up fixes.
  • Verify reactivity:Every device has different and resolutions. Screenshots help you quickly check that your app looks full on all of them.

Read More:

Prerequisites for setting up Selenium & amp; Python for Screenshots

There are a few dependencies that must be taken care of in order to direct a screenshot employ Python and Selenium. They are listed below:

  • Install Python and an IDE, preferablyPyCharm. It becomes easier to work with the Selenium bandaging that comes with Python with an IDE.
  • When using Python-Selenium, to get automated responses from web browsers it is necessary to make certain that the is working without any errors. For a hassle-free workings of Python and Selenium, create sure the webdriver is in path and matches the compatible version with the web browser.
  • Another Python package identifySelenium-Screenshotis often used to direct screenshots. Install the package using the pipcommand (the easiest way to establish it).
  • To catch the screenshot direct by Selenium and Selenium-Screenshot, use anotherpython bundle pillow or PIL,from which testers use the Image module to open and display the screenshot.
  • Ensure the route is set correctly for all dependencies. If this is not in place, it may throw unneeded errors while escape the program.

Learn about the.

How to guide a screenshot using Python and Selenium

Screenshots can be captured at different levels depending on your testing needs.

  • Import the necessary packages: Selenium, Selenium-Screenshot, and Pillow
  • Take the first screenshot using Selenium WebDriver ’ s save_screenshot () role to captivate the seeable part of the page
  • Take a screenshot of a specific element using the element.screenshot () method
  • Capture a full-page screenshot using the full_Screenshot () function from the Selenium-Screenshot faculty

Below is a detailed description of these measure.

Import the packages

To use python and selenium, install the undermentioned packages. Simply use the pip install command to install them and spell the like in the program.

[python] from selenium import webdriver from PIL import Image from Screenshot import Screenshot_clipping [/python]

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

Take the first screenshot utilize save_screenshot

Below is the full code to occupy a screenshot using Selenium and display it.

Read More:

While taking the screenshot, quizzer demand Selenium WebDriver to open a web browser automatically. Now, WebDriver may throw an error stating that the needs to be in the PATH. To resolve this, simply download a Chrome Driver that is compatible with the current version and give the executable path as shown in the representative.

This exemplar uses a chrome web driver, but testers can use another web browser. Additionally, in order to interact with desktop applications alternatively of a web coating, use one of the respective python bindings make for the purpose.

[python] driver = webdriver.Chrome (executable_path = ‘ path\to\chromedriver.exe ’ ’) url = `` https: //www.google.com/ '' ’ driver.get (url) [/python]

The above codification will open the web browser and open the URL yield in thedriver.get ().

After this, the following portion of the code will take the screenshot and save it as a.png file.

[python] driver.save_screenshot (‘ ss.png ’) [/python]

The next line of the code will show the screenshot taken use the Selenium WebDriver.

[python] screenshot = Image.open (‘ ss.png ’) screenshot.show () [/python]

Read More:

Talk to an Expert

3. Capture Specific Element with element.screenshot

You can take a screenshot of just one component of the page by targeting a specific element. This comes in handy when you solely want to enamour something like a push, a banner, or any section and not the whole screen.

Here is an example:

from selenium importation webdriver driver = webdriver.Chrome () driver.get ('https: //example.com ') # Find the element you want to catch element = driver.find_element (`` id '', `` logo '') # Save a screenshot of that element exclusively element.screenshot ('element_screenshot.png ') driver.quit ()

This will save an persona of just the element with the ID “ logo ” instead of the entire page.

4. Taking a Full Page Screenshot

Use the codification below to get the full page screenshot apply Selenium and Python.

[python] from selenium import webdriver from PIL import Image from Screenshot import Screenshot_Clipping ss = Screenshot_Clipping.Screenshot () driver = webdriver.Chrome (executable_path=r ' C: \Users\waseem k\Desktop\chromedriver.exe ') url = `` https: //www.browserstack.com/ '' driver.get (url) image = ss.full_Screenshot (driver, save_path=r '. ', image_name='name.png ') blind = Image.open (image) screen.show () [/python]

Output:

Here instead of just the seeable blind, the driver was able to hover down to the bottom of the web page and seizure a long screenshot/full length screenshot using thefull_Screenshot ()method in theScreenshot_Clipping module.

Potential Challenges and Solutions While Taking Python Screenshots

While taking screenshots with Python and Selenium is straightforward, developers may face these challenge.

  • Partial or blank screenshots:Sometimes, the screenshot might miss part of the page or appear blank if the page hasn ’ t fully loaded. Make certain to wait for the page or element to load completely before capturing.

Read More:

  • Scrolling issue for full-page screenshots:Capturing the entire page can be tricky if the page is long or uses dynamic load. However, you can use libraries or functions designed for full-page screenshots, and exam on different page to address variations.

Read More:

  • Element not launch fault: When capturing a specific element, it might not be immediately available or seeable. Add waits to ensure the element is present and visible before guide its screenshot.
  • File saving problems:Sometimes screenshots fail to salvage due to incorrect file paths or permission. Always verify the route and ensure your script has permit to indite files in the target pamphlet.
  • Browser compatibility:Different browser may deport slightly otherwise when capturing screenshots, so test across browser to catch any inconsistencies.

Read More:

Best Practices While Taking Python Screenshots

To get coherent and useful screenshots with Python and Selenium, it ’ s important to postdate these best recitation.

  • Account for:Ensure all lazy-loaded content (images, sections, etc.) is fully loaded or scrolled into view before catch to avoid missing constituent in the screenshot.

Read More:

  • Use element-focused screenshots when possible:Capturing alone the relevant element reduces noise and file sizing and targets exactly what you necessitate to verify.
  • Handle different blind sizes:Test screenshots on various window sizes or device to ensure consistent UI conduct across viewports.
  • Automate cleanup of temporary images:Delete or archive screenshots automatically after validation to prevent clutter during large examination runs.
  • Integrate screenshots into your reports: Embed screenshots directly into test reports so the team can quickly identify UI problems without hunting through files.

Conclusion

Screenshots are a simple but effective instrument to improve machine-controlled testing with Python and Selenium. You can capture the seeable screen with save_screenshot (), entire Page utilise full_screenshot (), or specific elements with element.screenshot (). These help get layout issues, confirm UI modification, and do debug easier. However, real device testing is indispensable to control your screenshots mull how your app behaves on actual devices.

gives you access to 3,500+ existent devices and browser to prove across multiple program and configurations for more reliable effect. It fully supports Selenium with Python and lets you run automated tryout seamlessly on existent devices in the cloud, scale your exam reporting, and get elaborated reports with screenshots and logarithm to speed up debugging.

Tags
32,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free