In this tutorial, we will learn how to save multiple screenshots using Selenium Python. Saving screenshots can help in the visual verification and debugging of your test automation scripts. Selenium is a popular web automation framework that allows you to control a web browser using Python or other programming languages.
Let’s look at the steps you need to follow to save multiple screenshots while performing browser automation using Selenium and Python.
Prerequisites
Before we proceed, make sure you have the following installed on your machine:
- Python: You can download Python from the official website.
- Selenium: Install Selenium using the following command:
pip install selenium
. - Webdriver: Download the appropriate webdriver for your chosen web browser. For this tutorial, we are assuming you are using Google Chrome. Download the ChromeDriver and add it to your system’s PATH.
Step 1: Import required libraries
For this tutorial, we need to import the following libraries:
1 2 3 |
from selenium import webdriver from selenium.webdriver.common.by import By import time |
Step 2: Define a function to save screenshots
Let’s create a function named save_screenshot
that accepts two arguments – the webdriver instance and a filename, and saves the screenshot with the given filename.
1 2 3 |
def save_screenshot(driver, filename): driver.save_screenshot(filename) print(f'Screenshot saved with filename: {filename}') |
Step 3: Initialize the webdriver instance
Now, initialize the webdriver instance (Chrome in our case) and set the desired window size for the browser.
1 2 |
driver = webdriver.Chrome() driver.set_window_size(1024, 768) |
Step 4: Use the save_screenshot function
Next, navigate to a webpage, and use the save_screenshot
function defined earlier to save multiple screenshots.
1 2 3 4 5 6 7 8 9 10 11 12 |
driver.get("https://www.example.com") # Save screenshot 1 save_screenshot(driver, "screenshot1.png") # Let's interact with the webpage and click on some button button = driver.find_element(By.XPATH, "//button[@id='some_id']") button.click() time.sleep(2) # Save screenshot 2 save_screenshot(driver, "screenshot2.png") |
Step 5: Clean up
Finally, quit the webdriver instance to close the browser.
1 |
driver.quit() |
Full code
Here’s the complete code for saving multiple screenshots using Selenium and Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
from selenium import webdriver from selenium.webdriver.common.by import By import time def save_screenshot(driver, filename): driver.save_screenshot(filename) print(f'Screenshot saved with filename: {filename}') driver = webdriver.Chrome() driver.set_window_size(1024, 768) driver.get("https://www.example.com") save_screenshot(driver, "screenshot1.png") button = driver.find_element(By.XPATH, "//button[@id='some_id']") button.click() time.sleep(2) save_screenshot(driver, "screenshot2.png") driver.quit() |
Expected output
Conclusion
In this tutorial, we learned how to save multiple screenshots in Selenium Python. This is particularly useful in visual verification and debugging, especially when dealing with web applications that involve multiple interactions and dynamic content. With this technique, you can easily save screenshots at different stages of your test automation script and inspect the flow of your tests visually.