Taking screenshots in Selenium with Python helps document the browser state during test runs. It supports debugging, layout verification, and visual tracking across different stages of your automation.
Overview
When to Take a Screenshot in Selenium?
Screenshots are often used during automation to capture the browser state for analysis, validation, or reporting. Use them in the following situations:
- During a test failure to log the exact UI state at the point of error
- After major interactions to confirm expected changes on the page
- While debugging issues with layout or element positioning
- When generating visual logs for test reports or reviews
How to Take Screenshots in Selenium Using Python?
Selenium supports multiple screenshot methods, including:
- Visible screen with save_screenshot: Use this to capture the part of the page currently in view.
- Full page with full_Screenshot(): Use this to capture the entire scrollable page.
- Specific element with element.screenshot: Capture only a targeted element like a button, error message, or container.
Benefits of Screenshot Testing With Python
Screenshot testing helps you visually track UI changes and catch layout issues that functional tests might miss. Below are key advantages of using screenshot testing with Python:
- Catch layout bugs: A test might pass all functional checks. but the UI could still be broken or misaligned. Screenshots help spot these visual issues that aren’t caught by normal assertions.
- Simplify debugging: When a test fails, seeing a screenshot of the browser helps you quickly understand what went wrong without guessing from logs alone.
- Track UI changes: Screenshots let you compare the app’s appearance over time to catch unexpected design changes after updates or deployments.
- Improve reporting: Including screenshots in test reports gives your team visual context to better understand issues and speed up fixes.
- Verify responsiveness: Every device has different screen sizes and resolutions. Screenshots help you quickly check that your app looks good on all of them.
Read More: What is UI Responsiveness Testing?
Prerequisites for setting up Selenium & Python for Screenshots
There are a few dependencies that must be taken care of in order to take a screenshot using Python and Selenium. They are listed below:
- Install Python and an IDE, preferably PyCharm. It becomes easier to work with the Selenium binding that comes with Python with an IDE.
- When using Python-Selenium, to get automated responses from web browsers it is necessary to make sure that the Selenium WebDriver is working without any errors. For a hassle-free working of Python and Selenium, make sure the webdriver is in path and matches the compatible version with the web browser.
- Another Python package named Selenium-Screenshot is often used to take screenshots. Install the package using the pip command (the easiest way to install it).
- To view the screenshot taken by Selenium and Selenium-Screenshot, use another python package pillow or PIL, from which testers use the Image module to open and display the screenshot.
- Ensure the path is set correctly for all dependencies. If this is not in place, it may throw unnecessary errors while running the program.
Learn about the common exceptions in Selenium.
How to take 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() function to capture the visible 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 module
Below is a detailed description of these steps.
Import the packages
To use python and selenium, install the following packages. Simply use the pip install command to install them and import the same in the program.
[python] from selenium import webdriver from PIL import Image from Screenshot import Screenshot_clipping [/python]
Take the first screenshot using save_screenshot
Below is the full code to take a screenshot using Selenium and display it.
While taking the screenshot, testers need Selenium WebDriver to open a web browser automatically. Now, WebDriver may throw an error stating that the ChromeDriver 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 example.
This example uses a chrome web driver, but testers can use another web browser. Additionally, in order to interact with desktop applications instead of a web application, use one of the several python bindings built for the purpose.
[python] driver = webdriver.Chrome(executable_path = ‘path\to\chromedriver.exe’’) url = "https://www.google.com/"’ driver.get(url) [/python]
The above code will open the web browser and open the URL given in the driver.get().
After this, the next part 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 using the Selenium WebDriver.
[python] screenshot = Image.open(‘ss.png’) screenshot.show() [/python]
Read More: How to Switch Tabs in Selenium For Python
3. Capture Specific Element with element.screenshot
You can take a screenshot of just one part of the page by targeting a specific element. This comes in handy when you only want to capture something like a button, a banner, or any section and not the whole screen.
Here is an example:
from selenium import webdriver driver = webdriver.Chrome() driver.get('https://example.com') # Find the element you want to capture element = driver.find_element("id", "logo") # Save a screenshot of that element only element.screenshot('element_screenshot.png') driver.quit()
This will save an image of just the element with the ID “logo” instead of the entire page.
4. Taking a Full Page Screenshot
Use the code below to get the full page screenshot using 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') screen = Image.open(image) screen.show() [/python]
Output:
Here instead of just the visible screen, the driver was able to hover down to the bottom of the web page and capture a long screenshot/full length screenshot using the full_Screenshot() method in the Screenshot_Clipping module.
Potential Challenges and Solutions While Taking Python Screenshots
While taking screenshots with Python and Selenium is straightforward, developers may face these challenges.
- Partial or blank screenshots: Sometimes, the screenshot might miss parts of the page or appear blank if the page hasn’t fully loaded. Make sure to wait for the page or element to load completely before capturing.
- Scrolling issues for full-page screenshots: Capturing the entire page can be tricky if the page is long or uses dynamic loading. However, you can use libraries or functions designed for full-page screenshots, and test on different pages to handle variations.
- Element not found errors: When capturing a specific element, it might not be immediately available or visible. Add waits to ensure the element is present and visible before taking its screenshot.
- File saving problems: Sometimes screenshots fail to save due to incorrect file paths or permissions. Always verify the path and ensure your script has permission to write files in the target folder.
- Browser compatibility: Different browsers may behave slightly differently when capturing screenshots, so test across browsers to catch any inconsistencies.
Read More: Understanding Browser Compatibility Matrix
Best Practices While Taking Python Screenshots
To get consistent and useful screenshots with Python and Selenium, it’s important to follow these best practices.
- Account for lazy loading: Ensure all lazy-loaded content (images, sections, etc.) is fully loaded or scrolled into view before capturing to avoid missing elements in the screenshot.
Read More: How to Lazy Load Images in Javascript
- Use element-focused screenshots when possible: Capturing only the relevant element reduces noise and file size and targets exactly what you need to verify.
- Handle different screen sizes: Test screenshots on various window sizes or devices to ensure consistent UI behavior across viewports.
- Automate cleanup of temporary images: Delete or archive screenshots automatically after validation to prevent clutter during large test runs.
- Integrate screenshots into your CI/CD 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 tool to improve automated testing with Python and Selenium. You can capture the visible screen with save_screenshot(), full pages using full_screenshot (), or specific elements with element.screenshot(). These help catch layout issues, confirm UI changes, and make debugging easier. However, real device testing is essential to ensure your screenshots reflect how your app behaves on actual devices.
BrowserStack gives you access to 3,500+ real devices and browsers to test across multiple platforms and configurations for more reliable results. It fully supports Selenium with Python and lets you run automated tests seamlessly on real devices in the cloud, scale your test coverage, and get detailed reports with screenshots and logs to speed up debugging.