Python Script for Automated Long Screenshot Capture Using Selenium and Pillow
This guide explains how to set up the environment, configure Selenium WebDriver, and run a comprehensive Python script that scrolls through a webpage, captures segmented screenshots, stitches them together with Pillow, and produces a high‑resolution long screenshot, suitable for mobile or web content.
Environment Preparation Install the required libraries with pip install selenium pillow webdriver_manager . Download the appropriate WebDriver for your browser (e.g., ChromeDriver for Chrome), ensure the driver version matches the browser version, and place the driver in the system PATH or specify its path.
Example Code The following Python script uses Selenium to control a headless Chrome browser, scrolls to the bottom of a page, captures screenshots in segments, and stitches them together using Pillow to create a single long image.
1. Import required libraries
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from PIL import Image
import time, io, base64
# Set Chrome options
chrome_options = Options()
chrome_options.add_argument("--headless") # headless mode
chrome_options.add_argument("--disable-gpu") # disable GPU
chrome_options.add_argument("--window-size=1920x1080") # window size
# Initialize WebDriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
# Scroll to the end of the page
def scroll_to_end(driver, delay=0.5):
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(delay)
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
# Capture and stitch screenshots
def take_long_screenshot(driver, filename, width, height):
total_height = driver.execute_script("return document.body.parentNode.scrollHeight")
stitched_image = Image.new('RGB', (width, total_height))
segments = int(total_height / height) + 1
for i in range(segments):
current_height = i * height
driver.execute_script(f"window.scrollTo(0, {current_height});")
time.sleep(0.5)
screenshot = driver.get_screenshot_as_png()
screenshot = Image.open(io.BytesIO(base64.b64decode(screenshot.split(',')[1])))
stitched_image.paste(screenshot, (0, current_height))
stitched_image.save(filename)
return stitched_image
# Main function
def main(url, output_filename):
driver.get(url)
time.sleep(2)
scroll_to_end(driver)
width = driver.execute_script("return window.innerWidth")
height = driver.execute_script("return window.innerHeight")
long_screenshot = take_long_screenshot(driver, output_filename, width, height)
driver.quit()
if __name__ == "__main__":
url = "https://www.example.com"
output_filename = "long_screenshot.png"
main(url, output_filename)Detailed Explanation The script first imports all necessary modules, then configures Chrome to run in headless mode with a fixed window size. The scroll_to_end function repeatedly scrolls to the bottom of the page until no new height is detected. The take_long_screenshot function calculates the total page height, creates a blank image, and iteratively scrolls to each segment, captures a screenshot, and pastes it into the blank canvas. Finally, the main function orchestrates navigation to the target URL, scrolling, dimension retrieval, screenshot stitching, and cleanup.
Core Advantages The method enables batch processing of long web pages or chat logs with a single click, dramatically reducing the time from minutes to seconds, ensuring complete visual continuity, high‑resolution output, and a consistent visual style.
Conclusion By using the provided Python script, you can effortlessly generate long screenshots of mobile or web content, eliminating tedious manual stitching and boosting productivity in both everyday tasks and project workflows.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.