How to take a screenshot with Selenium?

Taking headless browser screenshots can be a useful debugging and data collection tool when web scraping. With Selenium and Python, to take screenshots the save_screenshot() method can be used to capture the whole page or a specific area:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://httpbin.dev/html")

# For whole page
#   we can save directly to a given filename
driver.save_screenshot('screenshot.png')
#   or retrieve to python objects
screenshot_png_bytes = driver.get_screenshot_as_png()
screenshot_base64_string = driver.get_screenshot_as_base64()

# For specific element we should find the element first and then capture it:
from selenium.webdriver.common.by import By
element = driver.find_element(By.CSS_SELECTOR, 'p')
element.screenshot('just-the-paragraph.png')

driver.close()

Note that when scraping dynamic pages screenshot command might run before page is fully loaded thus missing important details. For that refer to How to wait for page to load in Selenium?

For more, see web scraping with Selenium and Python

Related Articles

How to Scrape With Headless Firefox

Discover how to use headless Firefox with Selenium, Playwright, and Puppeteer for web scraping, including practical examples for each library.

HEADLESS-BROWSER
PUPPETEER
SELENIUM
NODEJS
PLAYWRIGHT
PYTHON
How to Scrape With Headless Firefox

Selenium Wire Tutorial: Intercept Background Requests

In this guide, we'll explore web scraping with Selenium Wire. We'll define what it is, how to install it, and how to use it to inspect and manipulate background requests.

PYTHON
HEADLESS-BROWSER
SELENIUM
TOOLS
Selenium Wire Tutorial: Intercept Background Requests

Web Scraping Dynamic Web Pages With Scrapy Selenium

Learn how to scrape dynamic web pages with Scrapy Selenium. You will also learn how to use Scrapy Selenium for common scraping use cases, such as waiting for elements, clicking buttons and scrolling.

PYTHON
SCRAPY
HEADLESS-BROWSER
SELENIUM
Web Scraping Dynamic Web Pages With Scrapy Selenium

Web Scraping with Selenium and Python

Introduction to web scraping dynamic javascript powered websites and web apps using Selenium browser automation library and Python.

PYTHON
SELENIUM
HEADLESS-BROWSER
Web Scraping with Selenium and Python

How to Scrape Dynamic Websites Using Headless Web Browsers

Introduction to using web automation tools such as Puppeteer, Playwright, Selenium and ScrapFly to render dynamic websites for web scraping

HEADLESS-BROWSER
PYTHON
SELENIUM
PUPPETEER
PLAYWRIGHT
How to Scrape Dynamic Websites Using Headless Web Browsers

Guide to SeleniumBase — A Better & Easier Selenium

SeleniumBase streamlines browser automation with simple syntax, cross-browser support, and robust features, perfect for testing and web scraping.

SELENIUM
HEADLESS-BROWSER
Guide to SeleniumBase — A Better & Easier Selenium