python logo

selenium screenshot python


Python hosting: Host, run, and code Python in the cloud!

Selenium is a popular web automation framework, primarily used for web testing, web scraping, and browser automation tasks. It provides functionalities to capture automated screenshots of web pages, streamlining the process for developers and testers.

Related course: Browser Automation with Python Selenium

How to Take a Screenshot Using Selenium

Using Python, Selenium communicates with a web driver to initiate a browser session, navigate to a specific URL, and capture the visible content in a screenshot. This image can be saved locally on your machine for reference or testing purposes.

Screenshot using Python with Selenium

To capture a screenshot with Selenium, begin by launching a web driver. For demonstration purposes, let’s use the Chromium driver to open python.org:

1
2
3
4
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://python.org')

Next, invoke the save_screenshot() method to save the current browser window’s contents:

1
driver.save_screenshot("screenshot.png")

The above command will save the screenshot in the directory where the Python script is located.

If you’re using the Chromium browser for testing, your code might need to include ChromeOptions. A comprehensive example is provided below:

1
2
3
4
5
6
7
8
9
10
11
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()

Always remember to close the browser using driver.close() after your operations to free up system resources.

For other browsers, the method remains largely similar:

1
2
3
4
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://pythonspot.com')
driver.save_screenshot("screenshot.png")

Download Selenium Examples

Capture a Screenshot of Specific HTML Elements

Selenium allows users to target specific HTML elements and capture only that portion of the web page. This can be done by first taking a full-page screenshot and subsequently cropping it to the desired element’s dimensions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from selenium import webdriver
from PIL import Image

# Taking the 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")

# Cropping the screenshot to the specific element
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()

Download More Selenium Examples

Previous Topic: Selenium with PhantomJS | Next Topic: Selenium - Retrieving Images






Leave a Reply:




Alex 2022-01-25T13:37:36.291Z

Hi, you should recommend using the context manager 'with' instead of closing the driver. This ensures that the driver closes even though an error appears in the code above.

Qwinn 2021-11-30T03:45:05.281Z

Thank you so much! Very helpful, literally saved me days of manual work <3