Open In App

How to capture Screen Shot in Selenium WebDriver?

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”)




from selenium import webdriver
 
#accesing the chrome browser
dr = webdriver.Chrome()
 
#providing the page url
 
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:

output

Capturing 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

Output

We 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




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
 
gfgScreenShot(pageUrl)

Output:

Output

Best 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:-

  1. 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.
  2. Capturing Screenshots when a test case fails: This will help us to easily solve the issue with an image view of the failure.
  3. 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.
  4. 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.
  5. 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.


Article Tags :