Selenium Screenshot

3.0.0 · active · verified Fri Apr 17

Selenium-screenshot is a Python library that extends Selenium WebDriver capabilities to capture full-page and element-specific screenshots. It's particularly useful for web scraping, testing, and archiving web content. Version 3.0.0 introduced significant improvements, including robust full-page screenshot functionality via Chrome DevTools Protocol (CDP). The library has an active release cadence, with major and minor updates addressing bugs and adding features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a headless Chrome WebDriver using `webdriver_manager`, navigate to a page, and then use `selenium-screenshot` to capture both a full-page screenshot and a specific element's screenshot. Version 3.0.0's `full_Screenshot` method leverages Chrome DevTools Protocol (CDP) for more accurate full-page captures.

import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from Screenshot import Screenshot_Clipping

# Setup Chrome options for headless mode and window size
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--window-size=1920,1080') # Recommended for consistent headless screenshots

# Initialize WebDriver
# Using webdriver_manager to automatically handle driver downloads
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)

ob = Screenshot_Clipping.Screenshot()

try:
    driver.get('https://www.google.com')
    print(f"Navigated to {driver.current_url}")

    # Take a full-page screenshot using CDP (requires Chrome/Chromium-based browser)
    full_page_screenshot_path = ob.full_Screenshot(driver, save_path=os.getcwd(), image_name='google_full_page.png')
    print(f"Full-page screenshot saved to: {full_page_screenshot_path}")

    # Find an element and take its screenshot
    search_box = driver.find_element(By.NAME, 'q')
    element_screenshot_path = ob.get_element_screenshot(driver, search_box, save_path=os.getcwd(), image_name='google_search_box.png')
    print(f"Element screenshot saved to: {element_screenshot_path}")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    driver.quit()
    print("WebDriver closed.")

view raw JSON →