How to capture Screen Shot in Selenium WebDriver?
Last Updated :
28 Apr, 2025
Selenium is an open-source framework used mainly in web automation tasks. Its sole purpose is in testing web applications but it's not the only thing we can do with selenium. Selenium can also be used in other web-related tasks (such as web scraping, web element interaction, etc). Since Selenium is open source, it has a strong community of developers that helps other users by providing documentation, GitHub support, and many more such things making new users comfortable with it. Selenium has support for multiple programming languages including Python, Java, JavaScript, c#, etc. In this article, we going to show one of its use cases i.e. taking screenshots with Selenium and WebDriver. So, hang tight, and let's deep dive into the article.
Capturing Screen Shot using get_screenshot_as_file()
In the first step, we have to install the Selenium package in Python. We can simply use pip (it's a common package manager in Python). We have to type this command in our command prompt.
pip install selenium
After typing this command in our command prompt, we can see downloading message starts displaying on the command prompt. Wait until the installation is completed.

Create a Python file. Open the Python file (you can use PyCharm, Python idle, Vs code, etc to open it). After we are done with opening the file, import WebDriver from the Selenium library.
from selenium import webdriver
Call webdriver.Chrome( ). This function will open our Chrome. We can specify the location of our webdriver's path in the function but it's optional. This function will automatically open Chrome if it's in the same directory. If it's in the other directory, you must mention the path of our webdriver.
dr = webdriver.Chrome()
Now, we have to specify the URL of the website/web page we wish to capture the screenshot. Now call get_screenshot_as_file(), it stores the file in the path provided as an argument.
dr.get_screenshot_as_file("C:\screengfg\scr.png")
Python3
from selenium import webdriver
#accesing the chrome browser
dr = webdriver.Chrome()
#providing the page url
pageUrl = "http://www.geeksforgeeks.org/"
dr.get(pageUrl)
#invoking the function to take screen shot and store it in mentioned location
dr.get_screenshot_as_file("C:\screengfg\scr.png")
#closing the browser
dr.quit()
Output:
outputCapturing Screenshots usingThird-party Libraries
Let's install the mentioned library in our system. We can install it similarly as we have done in our previous approach with pip.
pip install mss
OutputWe are also using the date time Python the library to set a distinguished name to our file. Now it's completely up to you to use it or set a static name for your file.
To install the date time library, we use:
pip install datetime
Output
Python3
from selenium import webdriver
from mss import mss
import datetime
def gfgScreenShot(pageUrl):
#opening the chrome
dr = webdriver.Chrome()
dr.get(pageUrl)
#getting todays date
today = datetime.date.today()
#setting up the name of the file
fileName = "C:\screengfg\screenshot"+str(today)+".png"
with mss() as sct:
screenshot = sct.shot(output=fileName)
print("Screen Shot Captured sucessfully !!!")
#closing the browser
dr.quit()
#providing the page url
pageUrl = "http://www.geeksforgeeks.org/"
gfgScreenShot(pageUrl)
Output:
OutputBest Practices for Capturing Screenshots
Selenium is used in testing. So, capturing and storing screenshots are the essentials in bug identification and testing. Let's see some of the best practices for capturing screenshots in Selenium WebDriver:-
- Providing a unique file name allows: Like in the above approach, giving a unique file name allows us to easily locate the file, not wasting much time. You can use Python's date module, as I had shown in the above approach for keeping the names dynamic.
- Capturing Screenshots when a test case fails: This will help us to easily solve the issue with an image view of the failure.
- Organizing the Screenshots: After we are done taking screenshots, next step is to organize them. We have to organize different screenshots into different directories. We can group them by date, test case, or other meaningful categories.
- Taking Screen Shots at Key Point Selenium: We can take screenshots at critical points of our test cases. This will help us to understand the various stages of our applications.
- Attaching screenshots in test reports: Applying this will help us to visualize test cases better.
Conclusion
Selenium is a famous open-source tool used in web automation tasks. Capturing screen shots is one of its numerous use cases. We can capture screens with selenium and webdriver by various methods. We can use Selenium built-in library, third-party library, etc. Capturing screenshots will help in bug identification, better understanding of our application's various stages, etc. In this article, we have discussed all the mentioned points and tried to elaborate them accurately, concisely, and clearly.
Similar Reads
Automation Testing - Software Testing Automated Testing means using special software for tasks that people usually do when checking and testing a software product. Nowadays, many software projects use automation testing from start to end, especially in agile and DevOps methods. This means the engineering team runs tests automatically wi
15+ min read
Automation Testing Roadmap: A Complete Guide to Automation Testing [2025] Test automation has become a vital aspect of the Software Development Life Cycle (SDLC), aimed at reducing the need for manual effort in routine and repetitive tasks. Although manual testing is crucial for ensuring the quality of a software product, test automation plays a significant role as well.
9 min read
How to Start Automation Testing from Scratch? Automation Testing is the practice of using automated tools and scripts to execute tests on software applications, reducing manual effort and increasing efficiency. Starting automation testing from scratch involves several key steps, including selecting the right automation tool, identifying test ca
8 min read
Benefits of Automation Testing Automation Testing is the process of using tools and scripts to automatically run tests on software, instead of performing them manually. It helps developers and testers save time, catch bugs early, and ensure that applications work as expected after every change. In this article, weâll explore the
4 min read
Stages of Automation Testing Life Cycle In this article, we will explore the phases and methodologies involved in automation testing and the phases of the automation testing lifecycle. We'll cover everything from scoping your test automation to creating a solid test plan and strategy. You'll also learn about setting up the perfect test en
12 min read
Top Automation Testing Books For 2024 In this article, we can explore the top 10 books for automation testing, providing a top-level view of each book's content material and why it's worth considering for everybody interested in this sector. Table of Content Top 10 Books for Automation Testing BooksConclusionFAQs on Top Automation Test
12 min read
Top Test Automation mistakes and Tips for QA teams to avoid them In the dynamic landscape of software testing, avoiding common test automation pitfalls is crucial for QA teams aiming for efficient and successful testing processes. This article delves into prevalent errors in test automation and provides valuable insights on how QA teams can steer clear of these m
7 min read
Essential Skills for a Successful Automation Tester In the domain of software testing, automation plays a crucial role in ensuring efficiency, accuracy, and speed. However, to be a successful automation tester, one must possess a specific set of skills beyond just technical proficiency. This article explores the essential skills required for automati
6 min read
Steps to Select the Right Test Automation tools Selecting the right test automation tools is critical for ensuring efficient and effective testing processes in software development projects. In this article, we will discuss the key steps involved in choosing the most suitable automation tools for your project needs. From understanding project req
5 min read
Best Test Automation Practices in 2024 Test Automation continues to evolve with new technologies and methodologies emerging each year. In 2024, staying updated with the latest best practices is crucial for efficient and effective testing processes. From robust test design to continuous integration and deployment, this article explores th
7 min read