Taking Screenshot using Python Selenium WebDriver

The Blog To Learn Selenium and Test Automation

Taking Screenshot using Python Selenium WebDriver

Hello, welcome to Python Selenium tutorials and in this post let’s see how to capture Screenshot in Python Selenium Webdriver

  • Ability to take screenshots is most important and desirable feature for bug analysis.
  • Sometimes, apart from bug analysis, one may want to capture screenshots to see the flow of test steps.
  • Screenshots help automation testers a lot when test cases fails, one can identify what went wrong in test script or application.

Selenium can take screenshots during execution and save it in a file. We need to type cast WebDriver instance to TakesScreenshot in case of Java/Selenium. Where as Python/Selenium directly calls methods to take screenshot and these methods support screenshot file as ‘.png’.

WebDriver offers total three APIs to take screenshot of a web page.

  1. save_screenshot(‘filename’)
  2. get_screenshot_as_file(‘filename’)
  3. get_screenshot_as_png()

First two APIs are used to take and store screenshots as ‘.png’ files.

Third API, get_screenshot_as_png(), returns a binary data. This binary data will create an image in memory and can be useful if we want to manipulate before saving it.

An important note to store screenshots is that save_screenshot(‘filename’) and get_screenshot_as_file(‘filename’) will work only when extension of file is ‘.png’
Otherwise content of the screenshot can’t be viewed and Python throws a warning message.

/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py:907: UserWarning: name used for saved screenshot does not match file type. It should end with a `.png` extension type. It should end with a `.png` extension, UserWarning

save_screenshot()

from selenium import webdriver

driver = webdriver.Firefox(executable_path='[Browser Driver Path]')
driver.get('[URL to Open]')
 
driver.find_element_by_id('username').send_keys('admin')
driver.find_element_by_id('password').send_keys('admin')
driver.find_element_by_id('login').click()
 
driver.save_screenshot('sample_screenshot_1.png')

get_screenshot_as_file()

from selenium import webdriver

driver = webdriver.Firefox(executable_path='[Browser Driver Path]')
driver.get('[URL to Open]')

driver.find_element_by_id('username').send_keys('admin')
driver.find_element_by_id('password').send_keys('admin')
driver.find_element_by_id('login').click()
 
driver.get_screenshot_as_file('sample_screenshot_2.png')

get_screenshot_as_png()

from selenium import webdriver
import StringIO
from PIL import Image 
 
driver = webdriver.Firefox(executable_path='[Browser Driver Path]')
driver.get('[URL to Open]')
 
driver.find_element_by_id('username').send_keys('admin')
driver.find_element_by_id('password').send_keys('admin')
driver.find_element_by_id('login').click()
 
screenshot = driver.get_screenshot_as_png()
 
size = (0, 0, 680, 400)
image = Image.open(StringIO.StringIO(screen))
region = image.crop(size)
region.save('sample_screenshot_3.jpg', 'JPEG', optimize=True, quality=95)

Here we are taking screenshot, cropping it to a particular size, and storing it a ‘.jpg’ file named sample_screenshot_3.jpg.

 

3 Responses

  1. F. says:

    Hi,

    hm, your post is interesting but what I am missing is, how to make a screenshot from the whole entire page, as you can do it with Phantom JS. Unfortunately, Phantom JS is depreciated, so whats the alternative?

    Another thing… where do you get the library “StringIO”? It seems, that it is not included in the regular anaconda data base or at PyPI, which is the reason I am not able to reproduce your examples.
    This makes your post interesting but somehow weak on the other side.

    Greetings.

Leave a Reply

Your email address will not be published. Required fields are marked *