Selenium Screenshot
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
-
ModuleNotFoundError: No module named 'Screenshot'
cause The `selenium-screenshot` package was not installed or was installed in an environment not accessible by the script.fixEnsure the package is correctly installed: `pip install selenium-screenshot`. Verify your Python environment (e.g., virtual environment) is active. -
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
cause The WebDriver could not find the element specified by the locator when trying to take an element screenshot.fixDouble-check the element locator (e.g., `By.ID`, `By.CSS_SELECTOR`, `By.NAME`). Use explicit waits (`WebDriverWait`) to ensure the element is present and visible before attempting to find it. The element might not exist, be hidden, or not yet loaded when the code executes. -
TypeError: Screenshot_Clipping.full_Screenshot() missing 1 required positional argument: 'driver'
cause The `full_Screenshot` method was called without passing the `driver` (WebDriver) instance as the first argument.fixEnsure you pass the WebDriver instance to the method: `ob.full_Screenshot(driver, save_path=..., image_name=...)`. -
Screenshots are cropped, cut off, or incomplete, especially for long pages.
cause This was a common issue in versions prior to 3.0.0. Even in v3.0.0+, it can happen if not using a Chrome-based browser, or if the page has extremely complex dynamic content or infinite scrolling that the library cannot fully capture.fixUpgrade to `selenium-screenshot` v3.0.0 or later. Use a Chrome/Chromium-based browser with the latest `chromedriver`. Ensure the browser window size is set adequately, especially in headless mode (`--window-size`). For extremely complex cases, you might need to manually scroll and stitch images, or use a dedicated headless browser service.
Warnings
- breaking Version 3.0.0 introduces significant changes to how full-page screenshots are captured, now primarily utilizing the Chrome DevTools Protocol (CDP). This may require adjustments to existing code, particularly if you were relying on older, less robust full-page methods. The API for `full_Screenshot` remains similar but its underlying implementation is different.
- gotcha `full_Screenshot` in v3.0.0+ is optimized for Chrome/Chromium-based browsers due to its reliance on the Chrome DevTools Protocol (CDP). While it might fall back to older methods for other browsers, full-page screenshot accuracy and reliability will be best with Chrome.
- gotcha When running Selenium in headless mode, especially with older versions or if screenshots appear inconsistent, setting a specific window size (`--window-size`) is crucial for predictable screenshot dimensions and content rendering, even though v3.0.0 improves headless handling.
- gotcha For `get_element_screenshot`, ensuring the target element is visible and correctly located on the page is critical. Issues with elements being off-screen, hidden, or dynamically loaded can lead to `NoSuchElementException` or incomplete screenshots.
Install
-
pip install selenium-screenshot
Imports
- Screenshot_Clipping
from selenium_screenshot import Screenshot_Clipping
from Screenshot import Screenshot_Clipping
Quickstart
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.")