Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

On This Page
Learn how to use Python screenshots for efficient software testing and development processes. Enhance your UI and speed up error identification.

Paulo Oliveira
January 27, 2026
In the world of technology, ensuring flawless software functionality and identifying problems is essential. As the graphical user interface (GUI) becomes more complex, the need for consistent performance across various devices increases. It is important to find an efficient method for monitoring, evaluating, and enhancing software operations. Moreover, it’s important to understand how specific errors impact what users see on their screens.
To address these challenges, one effective approach is to utilize visual evidence, specifically screenshots, as valuable tools in software testing and development. These screenshots serve as invaluable assets for identifying problems and confirming the impact of errors on the user interface. This visual evidence is essential for maintaining a consistent user interface, pinpointing issues, and speeding up the correction process.
It also bridges automated operations and manual verification, enabling development teams to assess software responses and identify inconsistencies visually. It also helps development teams make data-driven decisions, monitor project development, identify areas for improvement, and optimize the user interface through A/B tests.
In this blog, you will learn to capture screenshots in Python and explore various techniques that can be applied in automation testing. You will also learn the best practices and tools used for Python screenshots.
By the time you finish reading, you’ll have a solid knowledge of using Python for taking screenshots in automated testing. This will enhance your software development and testing process.
Visual captures are often referred to as screenshots; they serve as visual markers for on-screen activities, whether you’re creating step-by-step guides or documenting your software experiences. However, their significance becomes particularly evident in specific domains, with software testing and its automated aspects being a prime example.
Let’s say you have just launched a modern website boasting fast transitions, a user-friendly layout, and an excellent user experience. However, you start receiving feedback about its inconsistent appearance across different devices. This is where screenshots step in as an unrecognized hero. They provide immediate visual cues that are crucial for spotting any overlooked irregularities.
For instance, screenshots can be used to compare images and detect differences between different versions of an application, making them a necessary tool for ensuring consistent user experiences.



In software testing, screenshots are similar to ‘visual documentation’. They confirm that the software’s appearance matches its intended design, ensuring graphical elements are displayed correctly and free from any imperfections. In the context of test automation, their importance becomes even more noticeable.
Automated tests frequently run outside regular working hours, making screenshots valuable visual representations. Instead of diving into extensive logs or trying to decode cryptic error codes, quality assurance professionals can refer to screenshots to gain clear insight into emerging issues.
As we continue, we’ll discover how Python’s versatility simplifies capturing and organizing screenshots, making it an effective tool for visual insights. Let’s start setting up Python in the following section of this blog on Python screenshots.
Before we begin working with Python screenshots, let’s explore the standard steps for setting up the Python environment :
python --version
To work with Python screenshots and related libraries, it’s essential to use a compatible Python version. Python 3.6 or its subsequent releases are preferable. In this case, we will use Python 3.6, which offers consistent performance and adaptability with the latest tools and libraries. To determine your current Python version, input the following command line in your terminal
python3 --version

After setting Python in your system, you are ready to write Python code. If you are already aware of Python basics and want to upgrade to Python advanced, check out this complete Python tutorial for valuable, in-depth insights to enhance your Python knowledge.
Screenshot testing is essential in the modern development environment, and Python makes this process more efficient and flexible. Below are some key benefits of incorporating Python screenshot testing:
In summary, Python screenshot testing is a ‘nice-to-have’ and a ‘must-have’ in your testing process. Its user-friendly nature, adaptability, and seamless integration make it the top choice for teams committed to building robust and flawless applications.
Note: Upgrade your testing with Python and screenshot testing. Try TestMu AI Today!
Capturing screens isn’t just about recording or documentation; it becomes incredibly valuable when added to automated tests. Let’s delve into how taking images can improve testing and reporting.
This plays an important role in our digital experiences. It ensures that what we see on the screen matches our expectations and requirements. In the following section below, we will learn the significance of visual verification in software testing, user experience, and more.

A visual check of an online shopping tool revealed that, in specific display settings, certain user interface elements were overlapping.

These instances highlight the complementary nature of visual analysis alongside functional tests, ensuring an all-round user experience.
Python screenshots are essential for regression testing. It helps identify changes or issues in software behavior. In this section, we’ll explore their role in ensuring software reliability.
When new code is added, things might unintentionally look different on the screen. By taking pictures before and after updates and comparing them closely, you can spot any unexpected changes.
Pinpointing visual inconsistencies and changes in your software during every integration is a challenging task. Learn how to simplify this process by watching this video on Smart UI visual testing and transform your regression testing with ease.
Adding Python screenshots in your test reports doesn’t only make the documentation look better, it actively improves the quality assurance process by providing a visual story that simplifies inspection and troubleshooting. This visual approach to documentation becomes an essential resource offering insights not easily obtained from text-based logs or summaries.
Consider a Python-based Selenium test script where you are automatically navigating through a web application. Every time the script encounters an error, it could be programmed to capture a screenshot of the current browser window. These Python screenshots could then be embedded in the test report, providing a precise view of what went wrong and where.
Here is a Python snippet for capturing a screenshot with Selenium that can then be embedded within an HTML test report:
# Direct the driver to navigate to the specified URLfrom selenium import webdriver
from selenium.webdriver.common.by import By
def test_report():
# Set up the webdriver
driver = webdriver.Chrome()
# Maximize the browser window to ensure that it occupies the entire screen
driver.maximize_window()
driver.get("https://www.lambdatest.com/selenium-playground/simple-form-demo")
try:
# Find an element by its ID and click on it
element = driver.find_element(By.ID, "showInput")
element.click()
except Exception as e:
driver.get_screenshot_as_file("test_failure.png")
raise e # Rethrow the exception to mark the test as failed
# Close the browser
driver.quit()
Here are some ways to facilitate Python screenshot integration.
@allure is a community-driven reporting tool that allows easy integration of screenshots. It enhances test reports by letting testers attach Python screenshots for each step of the test case, thereby offering an in-depth review.
For instance, using @allure annotations with a Selenium-based test can help you capture and attach screenshots automatically whenever a test fails or throws an exception.
@allure.step("Capture failure screenshot")
def capture_failure_screenshot(driver):
driver.get_screenshot_as_file("failure_screenshot.png") The pytest framework becomes even more powerful when used with extensions like pytest-selenium. This combination provides native support for capturing screenshots, particularly when a test fails or shows irregularity.
You can configure pytest to automatically capture a screenshot of failure and attach it to the test report using the command --capture=tee-sys.
In the pytest framework, you can utilize a specific command to manage how the output is captured and shown during test execution. To gain a deeper understanding, let’s dissect this command into two components and explore why it’s vital within the pytest framework.
In simple terms, when you include image captures in automated testing, they improve the quality assurance process. Whether using tools like Selenium or just checking things visually, these image captures play a vital role in today’s quality control practices.
When testing web applications using Python screenshots, Selenium is widely used. Selenium WebDriver is a powerful component that allows programmatic automation of web browsers. While Selenium is well-known for simulating user actions across browsers like Chrome, Firefox, or Safari, its potential for screenshot testing is often overlooked.
Selenium WebDriver is particularly appealing to Python developers because of its seamless integration with Python. With Selenium, you can automate browser interactions and take screenshots at various points during the execution. This feature is invaluable for comprehensive web application testing where visual validation is vital.
The methods commonly used in Selenium for capturing Python screenshots include.
| Methods | Description |
|---|---|
| save_screenshot('path/to/save/screenshot') | This method allows you to specify the path to save the screenshot. |
| get_screenshot_as_file('filename.png') | This will save the screenshot as a PNG file. |
| get_screenshot_as_png() | This method returns the screenshot as binary data, which can be useful for on-the-fly image processing or analysis. |
Here you have a Selenium example using the three methods explained above:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
def test_fullpage():
# Set up the webdriver
driver = webdriver.Chrome()
# Maximize the browser window to ensure that it occupies the entire screen
driver.maximize_window()
# Direct the driver to navigate to the specified URL
driver.get("https://www.lambdatest.com/selenium-playground/simple-form-demo")
# Take a screenshot of the entire webpage and save it to a file in the specific path
driver.save_screenshot('screenshots/selenium-save-screenshot.png')
# Take a screenshot of the entire webpage and save it to a file
driver.get_screenshot_as_file('screenshots/selenium-get-screenshot-as-file.png')
# Take a screenshot of the entire webpage and save the data that can be used for further processing or saving
screenshot_data = driver.get_screenshot_as_png()
# Close the browser
driver.quit()
Selenium allows you to capture full-page Python screenshots, which can be immensely useful for visual regression testing. This helps ensure that all elements on the page, from the header to the footer, appear as expected. While capturing the viewport (what is currently visible in the browser window) is straightforward, capturing a full-page screenshot may require some additional steps. You may need to scroll through the page and take multiple screenshots, then stitch them together to create a complete image of the full page.
Code Walkthrough
Importing necessary Selenium libraries.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ECDefine a function called test_fullpage this function will consist of the browser we will invoke, and with the maximize_window() function, we are instructed to maximize the browser window to ensure it occupies the entire screen.
def test_fullpage():
# Set up the webdriver
driver = webdriver.Chrome()
# Maximize the browser window to ensure that it occupies the entire screen
driver.maximize_window()Direct the WebDriver to navigate to a specific URL (In this case, “https://www.testmuai.com/selenium-playground/simple-form-demo“)
# Direct the driver to navigate to the specified URL
driver.get("<a href="https://www.lambdatest.com/selenium-playground/simple-form-demo" target="_blank">https://www.lambdatest.com/selenium-playground/simple-form-demo</a>"Takes a screenshot of the entire webpage and saves it to a file in the specified path screenshots/selenium-save-screenshot.png.
# Take a screenshot of the entire webpage and save it to a file in the specific path
driver.save_screenshot('screenshots/selenium-save-screenshot.png')Takes another screenshot of the entire webpage and saves it to a file with a different name screenshots/selenium-get-screenshot-as-file.png.
# Take a screenshot of the entire webpage and save it to a file
driver.get_screenshot_as_file('screenshots/selenium-get-screenshot-as-file.png'Capture the Python screenshot of the entire webpage and save the data as a PNG file that can be used for further processing or saving.
# Take a screenshot of the entire webpage and save the data that can be used for further processing or saving
screenshot_data = driver.get_screenshot_as_png()') Close the browser to end the WebDriver session.
# Close the browser
driver.quit()') To summarize, in the above script, the browser we invoked is Google Chrome using WebDriver, which navigates to a specific web page, captures screenshots in different ways, and saves them into different files using data variables. It can be used for different purposes, such as visual testing and automated quality assurance.
A highly valuable capability of Selenium is its capacity to pinpoint and capture Python screenshots of precise web elements. This is particularly useful when your testing needs to zoom in on particular UI components like buttons, banners, or modals. Utilizing WebDriver’s functions, you can identify the element you’re interested in and capture just that part of the screen. For instance, you can use the following method.
element.screenshot(‘element_screenshot.png’) to take a screenshot of a particular element you want to target.
Here you have a Selenium example that captures a screenshot of a specific element:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
def test_element():
# Set up the webdriver
driver = webdriver.Chrome()
# Maximize the browser window to ensure that it occupies the entire screen
driver.maximize_window()
# Direct the driver to navigate to the specified URL
driver.get("https://www.lambdatest.com/selenium-playground/simple-form-demo")
# Locate a specific element on the page using its ID attribute
specific_element = driver.find_element(By.ID, "user-message")
# Take a screenshot of just the located element and save it to a file
specific_element.screenshot('screenshots/selenium-element-snapshot.png')
# Close the browser
driver.quit()
To understand what the code is trying to perform, let’s walk through the code in a step-by-step procedure.
Code walkthrough
The above Python code generates specific page element screenshots; let’s understand what the code is trying to perform in a step-by-step procedure.
Importing necessary Selenium libraries.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ECDefine a function called test_fullpage this function will consist of the browser we will invoke, and with the maximize_window() function, we are instructed to maximize the browser window to ensure it occupies the entire screen.
def test_fullpage():
# Set up the webdriver
driver = webdriver.Chrome()
# Maximize the browser window to ensure that it occupies the entire screen
driver.maximize_window()Direct the WebDriver to navigate to a specific URL (In this case, “https://www.testmuai.com/selenium-playground/simple-form-demo“)
driver.get("<a href="https://www.lambdatest.com/selenium-playground/simple-form-demo" target="_blank">https://www.lambdatest.com/selenium-playground/simple-form-demo</a>"))To locate the element on the web page using its unique ID. In this case, the element ID is user-message and stored in the variable specific_element.
# Locate a specific element on the page using its ID attribute
specific_element = driver.find_element(By.ID, "user-message")"))This function takes a screenshot of the located element and saves it into a file. In this case, the Python screenshots are saved as screenshots/selenium-element-snapshot.png
# Take a screenshot of just the located element and save it to a file
specific_element.screenshot('screenshots/selenium-element-snapshot.png') Close the browser to end the WebDriver session.
# Close the browser
driver.quit()') To summarize, the browser we invoked is Google Chrome using WebDriver, which navigates to a specific web page, locates a specific element by its ID, captures a screenshot of that element, and saves it to a file. This is useful for capturing Python screenshots of individual elements on a web page for testing or documentation purposes.
Manual screenshot capture is often slow and inefficient, particularly in situations requiring frequent and repetitive web application testing. It not only consumes human resources but also introduces potential errors and requires accuracy and QA metrics process efficiency.
To address these challenges and transition from manual to automated screenshot capture to improve the test process, watch this video on how to perform automated screenshot testing. This video will give you valuable insight that can help in the smooth transition from the manual to the automated screenshots process.
Subscribe to the TestMu AI YouTube channel for more videos on automated screenshots, Selenium testing, and Cypress testing to enhance your testing experience!
The journey through Python screenshots management, especially in automated testing and documentation, comes with unique challenges. These hurdles can potentially affect the quality of visuals, but they can be effectively overcome with the right strategies. Let us discover some of the challenges and their solutions below.
Managing dynamic content while capturing screenshots can be quite challenging. With web applications increasingly relying on dynamic elements, maintaining accuracy and consistency in screenshots can be challenging. In this section, we will look into the challenges and solutions for dealing with the complexities of dynamic content within Python screenshot management.
Challenge
Modern web platforms and applications often feature content based on user interactions, including personalized greetings and smart advertisements.
Solution
You can use scripts to make the unpredictable elements more stable during captures or even avoid them. On the other hand, focus on the parts that stay the same or use masked regions to ignore the parts that change when conducting visual analysis.
Example
from selenium import webdriver
from selenium.webdriver.common.by import By
def test_dynamic_element():
# Set up the webdriver
driver = webdriver.Chrome()
# Maximize the browser window to ensure that it occupies the entire screen
driver.maximize_window()
# Direct the driver to navigate to the specified URL
driver.get('https://ecommerce-playground.lambdatest.io/index.php?route=product/category&path=57')
# Scroll down till the end
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Locate a specific element on the page using CSS Selector
specific_element = driver.find_element(By.CSS_SELECTOR, "img[alt='MacBook Air']")
# Take a screenshot of just the located element and save it to a file
specific_element.screenshot('screenshots/selenium-dynamic-element.png')
# Close the browser
driver.quit()
To summarize, the code initializes the Chrome WebDriver and then navigates to the URL. After opening the page, it scrolls down to the end of the page using JavaScript’s scrollTo method. Finally, it locates the element with an alt attribute of “MacBook Air”, takes a screenshot, saves it as selenium-dynamic-element.png, and closes the browser.
When running this code, you will get the below screenshot:

Handling multiple screen sizes and orientations can be challenging in the digital world. Ensuring your visuals work well on different devices is crucial. In this section, we’ll discuss the challenge and the solution for managing various screen variations while taking Python screenshots.
Challenge
Various device screens with different resolutions can display visuals differently, making capturing screenshots more complicated.
SolutionUse checks that are specifically designed for adaptive designs. Use tools that help you take screenshots on various screen sizes and viewing modes. Additionally, create a list of common device dimensions to simplify the analysis process.
Managing dynamic or animated content in screenshot capture can be challenging. Modern web applications often have elements that change frequently, making it hard to capture accurate screenshots. In this section, we’ll discuss strategies to handle these challenges while taking Python screenshot.
Challenge
Active graphics or rapidly updating elements can yield inconsistent visual records.
Solution
To capture dynamic content more effectively, delay or pause to slow down the changing elements. In automated processes, set up the system to temporarily stop or slow these elements, ensuring your screenshots are clear and consistent.
While capturing screen visuals in the dynamic digital environment may pose some challenges, these difficulties can be overcome with careful planning, the right tools, and well-practiced techniques.
Managing dynamic content manually can be time-consuming. That’s where tools like Selenium and other automation frameworks come in handy. Selenium can be useful when dealing with dynamic content elements through implicit and explicit waits. These waits functions wait for the entire page to load and then capture the elements. This process really reduces manual work, making the process of handling dynamic content smoother and faster.
If you want to streamline your screenshot testing process and overcome the challenges of dynamic content, multiple screen resolutions, and animated elements, you can use a cloud platform like TestMu AI. TestMu AI is an AI-powered test orchestration and execution platform that lets you run manual and automated tests at scale with over 3000+ real devices, browsers, and OS combinations.
Screenshots API for browser testing is a feature offered by TestMu AI that allows you to automatically capture screenshots of web pages across multiple browsers and operating systems. Using this API, you can programmatically initiate screenshot tests on various browser and OS combinations. It is particularly useful for QA professionals and developers who must ensure their web applications are visually consistent across different platforms.
Determining the right moment to utilize TestMu AI’s Screenshot API can significantly amplify its benefits. Here are some scenarios where the Screenshot API becomes indispensable:
Taking Python screenshots during testing is essential but can often be tedious and time-consuming. Leveraging TestMu AI’s Screenshot API for cross-browser testing offers an automated and streamlined solution. Here are some key benefits of using this powerful tool:
To showcase how this API can be used with Python, here’s a sample pytest code snippet. The code triggers a POST request to run a screenshot test on TestMu AI. This example uses Python’s requests library to make the API call.
Project Setup
pip install -r requirements.txt

Code implementation
import json
import requests
import os
from time import sleep
# API documentation: https://www.lambdatest.com/support/docs/automated-screenshot-api-for-cross-browser-testing/
username = os.getenv("LT_USERNAME")
accessKey = os.getenv("LT_ACCESS_KEY")
def test_screenshot_api():
url = "https://%s:%[email protected]/screenshots/v1" % (username, accessKey)
headers = {"accept": "application/json", "Content-Type": "application/json"}
payload = {
"url": "https://www.lambdatest.com/selenium-playground/simple-form-demo",
"defer_time": 5,
"email": True,
"mac_res": "1024x768",
"win_res": "1366X768",
"configs": {
"windows 10": {
"chrome": ["74"],
"firefox": ["66"],
"opera": ["58"],
"ie": ["11"]
},
"macos mojave": {
"chrome": ["74"]
}
}
}
response = requests.post(url, headers=headers, data=json.dumps(payload))
assert response.status_code == 200
response_data = response.json()
test_id = response_data.get('test_id') # Using `get` method to avoid KeyError if 'test_id' is not present.
print(f"The test ID is: {test_id}")
sleep(45)
fetch_details_executed_test_session(test_id)
def fetch_details_executed_test_session(testId):
url = "https://%s:%[email protected]/screenshots/v1/%s" % (username, accessKey, testId)
headers = {"accept": "application/json"}
response = requests.get(url, headers=headers)
assert response.status_code == 200
response_data = response.json()
print(response_data)
Code Walkthrough
To better understand what the code is trying to perform in a step-by-step process.









You can run this test using the below command:
pytest test_api.py
Result

To summarize, the fetch_details_executed_test_session() function displays all the details of the screenshots, including the links for the screenshots. For example, in the above sample, the execution on Windows 10, Firefox version 66, and resolution 1366×768 provided the URL link that has this screenshot.

In the previous section, we delved into Selenium’s robust capabilities for capturing screenshots, which are particularly useful for web-based testing scenarios. However, Selenium is one of many tools in your arsenal for screenshot capture and manipulation, especially when dealing with desktop applications or needing more control over screen captures. That’s where this bonus section comes in, expanding your toolkit with libraries like Pillow, PyAutoGUI, and PyGetWindow.
Pillow provides a strong foundation for general image capture and manipulation tasks. Meanwhile, PyAutoGUI and PyGetWindow offer more specialized functionalities tailored for desktop interactions. These libraries can be invaluable for test automation, enabling you to capture precise screen states of your desktop or specific windows, thereby providing a more holistic testing approach. With these libraries, you can easily extend your testing beyond the browser, ensuring that every element of your application looks and functions as intended.
Pillow, often called PIL (Python Imaging Library), is an open-source Python library designed for extensive file format support and an efficient internal representation. It’s a robust tool for image processing tasks, enabling users to create, modify, and save various image file formats.
Among its many functionalities, one particularly handy feature is its ability to capture screenshots.
PIL can be easily installed using the pip command below:
pip install pillow

Pillow is a popular Python library for working with images. It’s known for being user-friendly and handling various image formats easily. Here are some of the positive aspects that make it a popular choice for image processing tasks:
Pillow supports a wide range of image file formats, including popular ones like JPEG, PNG, and TIFF, as well as less commonly used formats like BMP and ICO.
The API provided by Pillow is straightforward and easy to use, making it accessible even for those relatively new to image processing or Python programming.
From basic tasks like cropping, resizing, and rotation to more advanced features like applying filters, drawing text, or even changing individual pixels, Pillow offers a wide array of image manipulation capabilities.
Pillow is designed to be efficient with memory, allowing for processing large images without consuming excessive resources.
Built on the foundation of the original PIL (Python Imaging Library), Pillow is optimized for performance, ensuring rapid image processing tasks.
Pillow enjoys robust community support, which means you can find a wealth of third-party resources, including tutorials, sample projects, and an active issue tracker for troubleshooting.
The library is actively maintained and regularly updated, ensuring that it stays compatible with the latest Python versions and receives performance improvements and new features.
Whether you are automating tests, developing a web application, working in scientific research, or simply automating some personal tasks, Pillow can fit various needs and project scopes.
In summary, the Pillow library’s versatility, ease of use, and robust feature set make it an excellent choice for anyone looking to perform image processing tasks in Python.
The methods commonly used in Pillow for Python screenshot capturing and image manipulation include
| Methods | Description |
|---|---|
| ImageGrab.grab() | Captures an image of the entire screen or a specific region if coordinates are provided. |
| Image.show() | Displays the captured image using the default image viewer. |
| Image.save(‘filename.extension’) | Saves the captured image to a file. The format can be specified by the file extension like .png, .jpg, etc. |
| Image.crop((left, upper, right, lower)) | Crops the image to a region defined by the coordinates (left, upper, right, lower) |
| Image.rotate(angle) | Rotates the image by the specified angle in degrees. |
| Image.filter(filter_type) | Applies a filter to the image. Filters can be found in the ImageFilter module and include options like BLUR, CONTOUR, DETAIL, etc. |
Capturing the entire screen might seem straightforward, but when integrated into a larger project, this simple action can offer a wealth of benefits.
During automated UI tests, you can capture the entire screen at various stages, such as debugging, monitoring, user experience studies, automated reporting, documentation, real-time decision-making, client demonstrations, and historical recording.
Here’s a simple example:
from PIL import ImageGrab
# Capture the whole screen
img = ImageGrab.grab()
# Display the image
img.show()
This code leverages the ImageGrab.grab() method, which returns an image of the screen.
Once you’ve captured a screenshot, you might want to save it for future reference. Here’s how you can save the screenshot to a file:
# Save the image to file
img.save("screenshot.png")
This line of code will save the image in PNG format with the name screenshot.png. You can change the file extension to save in different formats like JPG or BMP.
If you are interested in capturing just a particular region of your screen, Pillow has got you covered. The grab() method accepts a four-item tuple that defines the left, upper, right, and lower pixel coordinates.
from PIL import ImageGrab
# Define the region (left, top, right, bottom). This captures a 300x300 box from the top-left corner.
region = (0, 0, 300, 300)
img_region = ImageGrab.grab(bbox=region)
# Display the image
img_region.show()
# Save the image to file
img_region.save("screenshot-region.png")
This code will capture a 300×300 pixel box starting from the top-left corner of the screen.
Pillow isn’t just for capturing images; it’s a complete image processing library. This means you can edit your screenshots directly using the library:
from PIL import ImageGrab
# Capture the whole screen
img = ImageGrab.grab()
# Define the cropping box
box = (50, 50, 250, 250)
cropped_img = img.crop(box)
# Display the image
cropped_img.show()
# Save the image to file
cropped_img.save("screenshot-cropping.png")
from PIL import ImageGrab
# Capture the whole screen
img = ImageGrab.grab()
# Rotates the image by 90 degrees
rotated_img = img.rotate(90)
# Display the image
rotated_img.show()
# Save the image to file
rotated_img.save("screenshot-rotating.png")
from PIL import ImageGrab
from PIL import ImageFilter
# Capture the whole screen
img = ImageGrab.grab()
# Apply blur filter to image
blurred_img = img.filter(ImageFilter.BLUR)
# Display the image
blurred_img.show()
# Save the image to file
blurred_img.save("screenshot-filters.png")
Pillow is a handy Python library for managing Python screenshots and working with images, making it a good choice for various image-related tasks.
PyAutoGUI provides complete Python screenshot functionalities that allow you to easily capture the screen or specific regions of it. It offers methods to capture the entire screen and capture a designated rectangular area of the screen. These features can be extremely useful for automated testing, monitoring, or documentation.
The library also offers the unique capability to locate an element on the screen by its image. This is highly beneficial for GUI automation tasks requiring interaction with specific buttons or UI elements. You can find more about these screenshot features on its GitHub repository.
The methods commonly used in PyAutoGUI for screenshot capturing include:
| Methods | Description |
|---|---|
| screenshot() | Captures the entire screen and returns it as an Image object. |
| screenshot(region=(left, top, width, height)) | Captures a specific region of the screen based on the coordinates provided. |
| locateOnScreen(‘image.png’) | Searches the screen for the given image and returns the coordinates of the first occurrence. |
| locateAllOnScreen(‘image.png’) | Searches the screen for the given image and returns all occurrences. |
To install PyAutoGUI, you can just use pip:
pip install pyautogui

Capturing Python screenshots with PyAutoGUI is simple. Here’s how to use it:
import pyautogui
# Capture the entire screen
screenshot = pyautogui.screenshot()
# Save the screenshot to a file
screenshot.save("screenshot_pyautogui.png")
For capturing a specific region of the screen, similar to Pillow, you can define the coordinates as shown in the code below
import pyautogui
# Capture a specific region: (left, top, width, height)
region_screenshot = pyautogui.screenshot(region=(0, 0, 300, 300))
# Save the screenshot to a file
region_screenshot.save("screenshot-pyautogui-region.png")
While the previous libraries focus on capturing parts or the entirety of the screen, PyGetWindow allows you to capture specific application windows. This is especially useful if you take Python screenshots of individual apps without extra post-processing or cropping. You can find more about these features on its GitHub repository.
The methods commonly used in PyGetWindow for screenshot capturing include:
| Methods | Description |
|---|---|
| screenshot(region=(window.left, window.top, window.width, window.height)) | Take a Python screenshot of a window that was previously saved in the window variable. |
| getAllTitles() | Retrieves the titles of all currently open windows. |
| getWindowsWithTitle(title) | Retrieves a list of Window objects that match the given title. |
| getWindowRect() | Returns the position and size of the specified window as a tuple of integers (left, top, width, height). |
| getActiveWindow() | Captures the currently active/focused window. |
To install PyGetWindow, you can just use pip:
pip install PyGetWindow

Capturing Python screenshots with PyGetWindow is simple. Here’s how you can use it:
import pygetwindow as gw
import pyautogui
# Get a specific window by its title
window = gw.getWindowsWithTitle('Calculadora')[0]
# Use PyAutoGUI to capture the window's region
screenshot = pyautogui.screenshot(region=(window.left, window.top, window.width, window.height))
screenshot.save("application-window-screenshot.png")
Instead of capturing the entire screen and cropping out unnecessary parts, PyGetWindow, lets you directly capture individual application windows, streamlining the process.
If an application window changes its position or size, PyGetWindow can still accurately capture it, given its capability to dynamically identify window boundaries.
Capturing smaller, targeted areas can be faster and more resource-efficient than taking full-screen captures, especially on systems with large resolutions.
In short, Pillow is great for general image tasks, while PyAutoGUI and PyGetWindow are more specialized for taking Python screenshots. Choose the one that fits your task and how much control you need.
In this section, we will apply some of the knowledge of this blog about taking Python screenshots using Selenium native screenshots and TestMu AI Capabilities.
Discover how to conduct Python automation testing for your websites or web applications using a cloud-based grid TestMu AI.
To perform Selenium Python testing on the TestMu AI cloud grid, you should use the capabilities to configure the environment where the test will run. In this blog, we will run the tests on Windows 11 and the browser as Chrome.
We will execute the below test scenarios:
Test Scenario 1 – Using Selenium native screenshots
Steps:
|
|---|
Test Scenario 2 – Using TestMu AI capabilities
Preconditions:
Steps:
|
|---|
Step 1: Install Selenium, pytest, and other necessary libraries.
Once Python is installed, use the Python package manager, pip, to install Selenium and pytest by running the following command:
pip install -r requirements.txt
Requirements.txt contains the dependencies we want to install.

Result

Step 2: Download and Install Visual Studio Code or your preferred IDE.
Step 3: Configure pytest in Visual Studio Code.
To finish the configuration, we must tell Visual Studio Code that pytest will be our test runner by following the instructions below:
Now that your IDE and framework are set up let’s configure the TestMu AI settings and define the required capabilities to include in your test script.
Step 1: Create a TestMu AI account.
Step 2: Get your Username and Access Key by going to your Profile avatar from the TestMu AI dashboard and selecting Account Settings from the list of options.

Step 3: Copy your Username and Access Key from the Password and Security tab.

Step 4: Generate Capabilities, which contain details like your desired browser and its various operating systems. Generate your TestMu AI Capabilities Generator as per your requirement.

Step 5: Now that you have both the Username, Access key, and capabilities copied, all you need to do is paste it on your test script.
After configuring the required details, let’s jump into the scenarios executing on TestMu AI platform.

[WEBSITE]
url = https://www.lambdatest.com/selenium-playground/simple-form-demo
[LOGIN]
username = your-lambdatest-username
access_key = your-lambdatest-access-key
[CLOUDGRID]
grid_url = hub.lambdatest.com/wd/hub
build_name = Python Screenshots Build
test_name = Test Case X
w3c = True
browser_version = latest
selenium_version = 4.11.0
[ENV]
platform = Windows 11
browser_name = Chrome
Lets understand the above code configuration in detail on what each of the attributes is responsible for.
Code walkthrough – config.ini
In short, the config.ini file sets up the website to test, user access, automation parameters, and the testing environment.
Now that we have set up the config file let’s create a Python file and write code.
from selenium import webdriver
from selenium.webdriver.common.by import By
import pytest
import os
import configparser
# Load the configuration file
config = configparser.ConfigParser()
config.read('config/config.ini')
@pytest.fixture()
def driver(request):
username = os.getenv("LT_USERNAME")
config.set('LOGIN', 'username', username)
accessKey = os.getenv("LT_ACCESS_KEY")
config.set('LOGIN', 'access_key', accessKey)
username = config.get('LOGIN', 'username')
accessKey = config.get('LOGIN', 'access_key')
gridUrl = config.get('CLOUDGRID', 'grid_url')
web_driver = webdriver.ChromeOptions()
platform = config.get('ENV', 'platform')
browser_name = config.get('ENV', 'browser_name')
lt_options = {
"user": config.get('LOGIN', 'username'),
"accessKey": config.get('LOGIN', 'access_key'),
"build": config.get('CLOUDGRID', 'build_name'),
"name": config.get('CLOUDGRID', 'test_name'),
"platformName": platform,
"w3c": config.get('CLOUDGRID', 'w3c'),
"browserName": browser_name,
"browserVersion": config.get('CLOUDGRID', 'browser_version'),
"selenium_version": config.get('CLOUDGRID', 'selenium_version')
}
options = web_driver
options.set_capability('LT:Options', lt_options)
url = f"https://{username}:{accessKey}@{gridUrl}"
driver = webdriver.Remote(
command_executor=url,
options=options
)
yield driver
driver.quit
def test_screenshots_using_selenium(driver):
driver.get(config.get('WEBSITE', 'url'))
driver.get_screenshot_as_file('screenshots/screenshot-selenium-step-1.png')
# Find an input element by its ID and enter text
input_element = driver.find_element(By.ID, "user-message")
input_element.send_keys("This is a screenshot test text!")
driver.get_screenshot_as_file('screenshots/screenshot-selenium-step-2.png')
# Find an element by its ID and click on it
element = driver.find_element(By.ID, "showInput")
element.click()
# Find an element by its ID and extract its text
element = driver.find_element(By.ID, "message")
driver.get_screenshot_as_file('screenshots/screenshot-selenium-step-3.png')
assert element.text == "This is a screenshot test text!"
Code Walkthrough – test_selenium.py





https://www.lambdatest.com/blog/wp-content/uploads/2023/10/input-field-1-1.png

You can run this test using the below command:
pytest test_selenium.py
You will get the below result:

Inside the screenshots folder, you will see the generated files:

You can see the generated images:
Filename – screenshot-selenium-step-1.png

Filename – screenshot-selenium-step-2.png

Filename – screenshot-selenium-step-3.png

In scenario 1, we saw how the screenshots were taken and stored into a folder using TestMu AI capabilities, but as we discussed in the previous scenario we did not enable “visual” capability to “true”, now in scenarios 2 let’s do the same, but this time we will enable the capability “visual: true”.
from selenium import webdriver
from selenium.webdriver.common.by import By
import pytest
import os
import configparser
# Load the configuration file
config = configparser.ConfigParser()
config.read('config/config.ini')
@pytest.fixture()
def driver(request):
username = os.getenv("LT_USERNAME")
config.set('LOGIN', 'username', username)
accessKey = os.getenv("LT_ACCESS_KEY")
config.set('LOGIN', 'access_key', accessKey)
username = config.get('LOGIN', 'username')
accessKey = config.get('LOGIN', 'access_key')
gridUrl = config.get('CLOUDGRID', 'grid_url')
web_driver = webdriver.ChromeOptions()
platform = config.get('ENV', 'platform')
browser_name = config.get('ENV', 'browser_name')
lt_options = {
"user": config.get('LOGIN', 'username'),
"accessKey": config.get('LOGIN', 'access_key'),
"build": config.get('CLOUDGRID', 'build_name'),
"name": config.get('CLOUDGRID', 'test_name'),
"platformName": platform,
"w3c": config.get('CLOUDGRID', 'w3c'),
"browserName": browser_name,
"browserVersion": config.get('CLOUDGRID', 'browser_version'),
"selenium_version": config.get('CLOUDGRID', 'selenium_version'),
"visual": True
}
options = web_driver
options.set_capability('LT:Options', lt_options)
url = f"https://{username}:{accessKey}@{gridUrl}"
driver = webdriver.Remote(
command_executor=url,
options=options
)
yield driver
driver.quit
def test_screenshots_using_lambdatest(driver):
driver.get(config.get('WEBSITE', 'url'))
# Find an input element by its ID and enter text
input_element = driver.find_element(By.ID, "user-message")
input_element.send_keys("This is a screenshot test text!")
# Find an element by its ID and click on it
element = driver.find_element(By.ID, "showInput")
element.click()
# Find an element by its ID and extract its text
element = driver.find_element(By.ID, "message")
assert element.text == "This is a screenshot test text!"

Code Walkthrough
The code is almost the same as test_selenium.py, just changing two points:


You can run this test using the below command:
pytest test_lambdatest.py
Result

If you look at the TestMu AI Dashboard, you can see the details of the test case execution.

Highlighted in red, you can see the buttons to open the screenshots taken in each test step. If you open one of them (for example, the last one), it is possible to see how it is displayed.

To make the most of Python screenshots for tasks like documentation, testing, or presentations, you need a clear plan. Without a systematic approach, you can end up with too many images or lose the effectiveness of your visuals. Following simple and consistent practices is key to achieving clarity and efficiency.
Cross-browser testing checks if your website works well on different web browsers (like Chrome, Firefox, Edge, and Safari). It’s essential for a consistent user experience.
One of the standout capabilities for efficient Python screenshots is the utilization of automated screenshot APIs, such as the one provided by LambdaTest. This feature allows for the generation of Python screenshots across many browser and operating system combinations, which is invaluable for cross-browser testing
Organizing screenshots is crucial for easy access and efficiency. Create a simple system with clear file names and folders to keep your digital records in order.
A descriptive and logical naming scheme is crucial. Integrating elements like capture date, context, and a brief label can be insightful. For example, 14Aug2023_Error_LoginUI.png provides an immediate sense of the image’s focus. Such a structured naming methodology helps in faster location and improved structuring.
Dedicating specific folders aligned with projects or themes improves the ease of locating images. Leveraging cloud storage platforms, such as OneDrive or iCloud, provides the benefits of accessibility and seamless sharing. Utilizing versioning systems, especially for iterative testing phases, is essential to monitor transitions.
Clarity is important for effective communication. Use simple language, visuals, and logical organization to ensure your message is easily understood.
The essence of Python screenshots should be instantly evident. Utilizing applications to underline or highlight hidden regions ensures instant recognition. Elements like directional pointers, encapsulations, or concise textual overlays can improve the conveyed message.
Excess content in Python screenshots can be distracting. Removing unnecessary elements or adjusting dimensions to highlight essential information makes the image more focused and relevant.
It involves reducing image file sizes without compromising quality. This is crucial for faster websites, efficient storage, and better user experiences. Techniques include format selection, compression tools, and optimization libraries.
This is crucial for web-based platforms and digital sharing, where optimized image sizes can enhance responsiveness and save digital space. When compressing, it’s vital to preserve visual quality.
The digital world offers many resources for image optimization. Tools like ImageOptim or JPEGmini can significantly reduce file sizes while maintaining acceptable image quality. If you prefer a code-based approach, Python’s image libraries offer suitable functionalities.
Although taking Python screenshots might appear simple, getting the most out of them requires some attention to detail. By following these best practices, your visuals will align with their intended purpose and be well-organized and clear in their presentation.
In today’s tech world, Python screenshots remain valuable for capturing real-time visuals. Python, with its various libraries, offers a straightforward way to get, edit, and use these images effectively. Whether you use Python libraries or specialized tools, Python provides many ways to take screenshots.
As demonstrated in this blog, combining these methods in automated testing can greatly improve quality assurance processes. It helps teams gather proof of their test automation in action. As you advance in building your test framework, incorporating Python-based screenshot techniques can be a game-changer in your testing toolbox.
Did you find this page helpful?
More Related Hubs
TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance