Full Page Screenshot with Selenium Python
Taking screenshots during automated testing is a common and useful practice. It helps developers and testers capture the state of an application at any given point, especially when debugging failures or validating UI elements. However, one limitation with Selenium in Python is that the default driver.save_screenshot() function only captures the visible part of the page, not the entire scrollable content.
In this blog, we'll explore how to capture full page screenshots using Selenium with Python, along with various approaches and tools that make it possible.
Why Full Page Screenshots Matter
In modern web applications, content often extends far beyond the visible viewport. This is especially true for:
Long product detail pages
Landing pages with scrollable sections
Dashboards with expandable widgets
Reports or charts spanning multiple screens
Capturing the entire page ensures that you don’t miss visual bugs or layout issues in parts of the page that aren’t immediately visible.
Using Selenium’s Built-in Method (Limited)
Selenium’s standard method:
python
driver.save_screenshot("screenshot.png")
Only captures what is visible in the current window. If you need to take a full-page screenshot, this method alone won’t suffice unless the page fits within the viewport.
Approach 1: Use Firefox with Selenium and get_full_page_screenshot_as_file()
Firefox, unlike Chrome, supports full-page screenshots natively via Selenium:
python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get("https://example.com")
driver.get_full_page_screenshot_as_file("fullpage_firefox.png")
driver.quit()
✅ Pros:
Simple and accurate
No third-party libraries
❌ Cons:
Only works with Firefox and geckodriver
May not support advanced page interactions
Approach 2: Scrolling and Stitching with PIL (for Chrome)
If you prefer using Chrome, you can programmatically scroll the page and stitch together screenshots using Python’s PIL (Pillow) library.
python
from selenium import webdriver
from PIL import Image
import time
driver = webdriver.Chrome()
driver.get("https://example.com")
# Get page height
scroll_height = driver.execute_script("return document.body.scrollHeight")
viewport_height = driver.execute_script("return window.innerHeight")
driver.set_window_size(1200, scroll_height)
time.sleep(2) # Let page adjust
driver.save_screenshot("fullpage_chrome.png")
driver.quit()
This trick sets the browser window height to match the full page height, ensuring that everything is visible before capturing.
✅ Pros:
Works with Chrome
Simple, no need for complex libraries
❌ Cons:
Might break on dynamic or lazy-loaded content
Can fail on very long pages
Approach 3: Use Third-Party Tools (e.g., seleniumbase)
seleniumbase is a powerful wrapper over Selenium that supports full-page screenshots with ease.
Install:
bash
pip install seleniumbase
Example usage:
python
from seleniumbase import BaseCase
class ScreenshotTest(BaseCase):
def test_full_page(self):
self.open("https://example.com")
self.save_full_page_screenshot("seleniumbase_fullpage.png")
✅ Pros:
Clean and reliable
Built-in image comparison tools
❌ Cons:
Adds dependency overhead
Best Practices
Always wait for the page to fully load before capturing.
Set a fixed window size for consistency.
Handle lazy loading by scrolling manually or pausing before screenshot.
Use headless mode for faster execution in CI/CD pipelines.
Conclusion
Capturing full-page screenshots in Selenium Python is achievable through various methods, from native Firefox support to programmatic scrolling and third-party tools. Whether you're validating layout consistency, reporting bugs, or generating visual documentation, full-page screenshots provide a complete picture of your application’s UI.
Learn Selenium with Pyhton Training Hyderabad
Read More: Generating HTML Reports with PyTest and Selenium
Read More: Capturing Browser Logs in Selenium Python
Read More: How to Handle Broken Links in Selenium Python
Visit IHUB Talent Institute Hyderabad
Comments
Post a Comment