DEV Community

Bhimashankar Patil
Bhimashankar Patil

Posted on • Edited on

18 1 1 2 1

How to Capture Full Webpage Screenshots with Selenium in Python: A Step-by-Step Guide

Image description

In the world of web development, testing, and documentation, capturing full-page screenshots of web pages can be a vital task. Whether you need to verify the visual appearance of a website, document bugs, or track website changes over time, automating the process of taking full webpage screenshots is invaluable. In this guide, we'll walk you through how to accomplish this using Python and Selenium.

Prerequisites :

1) Selenium Installed: Install the Selenium library using pip:

pip install selenium

2) Webdriver for Chrome: You'll need the Chrome WebDriver for Selenium.

Capturing Full Webpage Screenshots in Python

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import time
import os
from datetime import datetime

# Get the current timestamp for the image name
today = datetime.now()
image_name = today.strftime("%Y-%m-%d %H:%M:%S")

# Set the path where the screenshot will be saved
path = os.path.dirname(os.path.abspath(__file__))

# Configure Chrome WebDriver options
options = Options()
options.add_argument("--window-size=1920,1080")
options.add_argument("--start-maximized")
options.add_argument("--headless")  # Use headless mode for running in the background
options.add_argument("--disable-gpu")

# Initialize the Chrome WebDriver
driver = webdriver.Chrome(options=options)
driver.maximize_window()

# Navigate to the URL you want to capture
driver.get("your_webpage_url_here")

# Wait for the page to load (you can adjust the sleep time as needed)
time.sleep(1)

# Use JavaScript to get the full width and height of the webpage
width = driver.execute_script("return Math.max( document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth );")
height = driver.execute_script("return Math.max( document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight );")

# Set the window size to match the entire webpage
driver.set_window_size(width, height)

# Find the full page element (usually 'body') and capture the screenshot
full_page = driver.find_element(By.TAG_NAME, "body")
full_page.screenshot(f"{image_name}.png")

# Close the browser window
driver.quit()

Enter fullscreen mode Exit fullscreen mode

Conclusion
Capturing full webpage screenshots programmatically is a powerful tool for web developers, testers, and anyone involved in web-related tasks. With Python and Selenium, you can automate this process and save valuable time in your web development and testing workflows.

Feel free to customize the script and integrate it into your own projects to capture webpage screenshots automatically and efficiently.

Happy screenshotting!

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (1)

Collapse
 
priteshusadadiya profile image
Pritesh Usadadiya

[[..Pingback..]]
This article was curated as a part of #101st Issue of Software Testing Notes Newsletter.
softwaretestingnotes.substack.com/...
Web: softwaretestingnotes.com

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay