AW Dev Rethought

⚖️ There are two ways of constructing a software design: one way is to make it so simple that there are obviously no deficiencies - C.A.R. Hoare

⚡ Python Power Shots — 🌐 Capture Webpage Screenshot with Selenium


Description:

📌 Introduction

Ever wanted to capture a full webpage screenshot programmatically — not just what’s visible on screen?

With Selenium, Python can open a real browser, load any webpage, and save it as an image automatically.

This Power Shot shows how to capture any webpage as a PNG screenshot, making it perfect for documentation, monitoring UI changes, reports, or automation workflows.


🔎 Explanation

  • Selenium WebDriver controls a real browser (Chrome in this case).
  • The script runs Chrome in headless mode, so no UI pops up.
  • After loading the webpage, Selenium captures a full-page screenshot.
  • You can automate screenshots for dashboards, landing pages, or reports.
  • Works on macOS (including Apple Silicon), Windows, and Linux with the right driver.

✅ Key Takeaways

  • 🌐 Capture full webpage screenshots using Python.
  • 🤖 Automate UI snapshots without manual effort.
  • ⚙️ Works headlessly — perfect for servers and cron jobs.

Code Snippet:

"""
Programmer: python_scripts (Abhijith Warrier)

PYTHON SCRIPT TO CAPTURE A WEBPAGE SCREENSHOT USING SELENIUM 🐍🌐📸

This script opens a webpage in a headless Chrome browser and saves
a screenshot as an image. Useful for UI monitoring, reports, or
automated documentation.
"""

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time

# --- Step 1: Configure Chrome options ---
chrome_options = Options()
chrome_options.add_argument("--headless")          # Run without UI
chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument("--disable-gpu")

# --- Step 2: Set ChromeDriver path ---
# Make sure chromedriver is installed and matches your Chrome version
CHROMEDRIVER_PATH = "/usr/local/bin/chromedriver"  # Update if needed

service = Service(CHROMEDRIVER_PATH)
driver = webdriver.Chrome(service=service, options=chrome_options)

# --- Step 3: Open the target webpage ---
url = "https://awdevrethought.com"
driver.get(url)

# Give the page time to load
time.sleep(3)

# --- Step 4: Capture screenshot ---
output_file = "awdevrethought_screenshot.png"
driver.save_screenshot(output_file)

print(f"📸 Screenshot saved as '{output_file}'")

# --- Step 5: Close the browser ---
driver.quit()

Link copied!

Comments

Add Your Comment

Comment Added!