- How to take screenshot of an element in Selenium Python?
- Examples
- My Form
- 1. Take screenshot of a form element
- 2. Take screenshot of a heading
- Summary
- selenium screenshot python
- Take screenshot Selenium
- Take screenshot of html element
- Taking Screenshot using Python Selenium WebDriver
- save_screenshot()
- get_screenshot_as_file()
- get_screenshot_as_png()
- 3 Responses
- How To Take A Screenshot Using Python & Selenium?
- What Is Selenium Python Bindings?
How to take screenshot of an element in Selenium Python?
In this tutorial, you will learn how to take the screenshot of a specific element in Selenium Python and save it to a specific path.
To take the screenshot of a single element in the webpage in Selenium Python, you can use screenshot() method of the element object.
Find the element, and call screenshot() method on the element object, and pass the location of the path as a string to which we would like to save the screenshot.
element.screenshot("path/to/screenshot.png")
Examples
In the following examples, we navigate to the following webpage, and take screenshots of the elements in it.
My Form
1. Take screenshot of a form element
In the following example, we initialize a Chrome webdriver, navigate to a specific URL, take the screenshot of the form with id ‘myform’, and save the screenshot to a location.
Python Program
from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.service import Service as ChromeService from selenium.webdriver.common.by import By # Setup chrome driver service = ChromeService(executable_path=ChromeDriverManager().install()) driver = webdriver.Chrome(service=service) driver.set_window_size(500, 500) # Navigate to the url driver.get('http://127.0.0.1:5500/index.html') # Find form element myform = driver.find_element(By.ID, 'myform') # Take a screenshot of the form element myform.screenshot("screenshot.png") # Close the driver driver.quit()
2. Take screenshot of a heading
In the following example, we initialize a Chrome webdriver, navigate to a specific URL, take the screenshot of the first heading h1 and save the screenshot as a PNG.
Python Program
from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.service import Service as ChromeService from selenium.webdriver.common.by import By # Setup chrome driver service = ChromeService(executable_path=ChromeDriverManager().install()) driver = webdriver.Chrome(service=service) driver.set_window_size(500, 500) # Navigate to the url driver.get('http://127.0.0.1:5500/index.html') # Find form element myform = driver.find_element(By.ID, 'myform') # Take a screenshot of the form element myform.screenshot("screenshot.png") # Close the driver driver.quit()
Summary
In this Python Selenium tutorial, we have given instructions on how to take the screenshot of a specific element in a webpage, and save it a to specific location, using screenshot() method of the element object.
selenium screenshot python
Selenium is a web automation framework that can be used for automated testing, web scraping and anything you can do with a web browser. We can use Selenium to take automated screenshots of a webpage.
Take screenshot Selenium
The way this works is that Python uses the selenium driver to open a module, then selenium will start the defined web browser and open the page url. It will then take a screenshot and save it to the local hard disk.
take screenshot using python code
We start a web driver (Chromium) and open the webpage python.org.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(‘https://python.org’)
driver.save_screenshot(«screenshot.png»)
The screenshot will be saved in the same directory as the program: the program path.
The full code is shown below. Now because I’ve tested with the chromium browser, it contains the ChromeOptions as parameter.
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument(‘—ignore-certificate-errors’)
options.add_argument(«—test-type»)
options.binary_location = «/usr/bin/chromium»
driver = webdriver.Chrome(chrome_options=options)
driver.get(‘https://python.org’)
driver.save_screenshot(«screenshot.png»)
driver.close()
Remember to call driver.close() otherwise the browser stays open after the program finishes.
So you could use a shorter version, if you use another web browser like
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(‘https://pythonspot.com’);
driver.save_screenshot(«screenshot.png»);
Take screenshot of html element
You can take a screenshot of a html element. The way this works is that you first take a screenshot of the whole page and then crop it to its html element size.
from selenium import webdriver
from PIL import Image
# take screenshot
driver = webdriver.Chrome();
driver.get(‘https://www.google.com’);
element = driver.find_element_by_id(«hplogo»);
location = element.location;
size = element.size;
driver.save_screenshot(«pageImage.png»);
# crop image
x = location[‘x’];
y = location[‘y’];
width = location[‘x’]+size[‘width’];
height = location[‘y’]+size[‘height’];
im = Image.open(‘pageImage.png’)
im = im.crop((int(x), int(y), int(width), int(height)))
im.save(‘element.png’)
driver.quit()
Taking Screenshot using Python Selenium WebDriver
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.
- save_screenshot(‘filename’)
- get_screenshot_as_file(‘filename’)
- 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
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.
How To Take A Screenshot Using Python & Selenium?
This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium Python Tutorial.
The goto software framework for any web developer looking for an open-source, free test automation tool is Selenium. It is used with various programming languages, including Java, Python, PHP, Perl, and C#. Selenium can also be used as a web-scraping tool or to create a human-replica bot to automate social-media or even test PDF files! Geeks at Google & Thoughtworks are highly credited for its development and maintenance.
In this Python Selenium screenshot tutorial, we are going to explore different ways of taking screenshots using Selenium’s Python bindings. Before we hop-on to capturing Python Selenium screenshots, let’s first acquaint ourselves with Selenium Python bindings.
What Is Selenium Python Bindings?
Selenium has different components; we have Selenium WebDriver, Selenium IDE, and Selenium Grid. Selenium Python bindings is an API interface to use Python with Selenium WebDriver for writing functional/acceptance tests. We shall be using these Python bindings for Selenium to capture full page screenshots, HTML element-specific screenshots and save it in our desired location.
Installing Dependencies
Before we learn how to use Selenium Python for taking screenshots, we need to install some dependencies. Below is a list of all that you need in your machine-
- Python
- Pip
- Selenium Python bindings
- GeckoDriver
- ChromeDriver
Note: Python 2 is redundant now. So, if your Linux or Mac system is having the older version, you may consider updating them to the latest stable versions.
- Chrome
- Firefox
- Edge
- Internet Explorer
Now let’s make a trial file named check_setup.py. Write the following code in it –