+91 9790664230
kumar.arun211@gmail.com

Taking Screenshot using Python Selenium WebDriver

Blog to learn python selenium from java selenium

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)

1. 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');

2. 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');

3. 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.

 

2 Responses

  1. […] Previous: Previous post: Taking Screenshot using Python Selenium WebDriver […]

  2. […] articles for more details about taking screenshot and element […]

Leave a Reply

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

© 2018 Copyrights. All Rights Reserved. Arunkumar Velusamy