0
0
Selenium Pythontesting~5 mins

Full page screenshot in Selenium Python

Choose your learning style9 modes available
Introduction

Taking a full page screenshot helps you capture the entire webpage, not just the visible part. This is useful to check the full layout and content in one image.

You want to save a complete picture of a webpage for visual testing.
You need to compare the full page layout before and after a change.
You want to document a webpage exactly as it appears, including content below the fold.
You are debugging layout issues that happen outside the visible screen area.
Syntax
Selenium Python
driver.get_screenshot_as_file('filename.png')

This command saves the visible part of the page as an image file.

To capture the full page, you may need to use browser-specific options or scroll and stitch images.

Examples
Saves only the visible part of the webpage as 'visible_part.png'.
Selenium Python
driver.get_screenshot_as_file('visible_part.png')
Scrolls to the bottom and takes a screenshot, but still only visible area is captured.
Selenium Python
driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
driver.get_screenshot_as_file('scrolled_page.png')
Using Chrome DevTools Protocol (CDP) to capture full page screenshot (Chrome 59+).
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import base64
options = Options()
options.add_argument('--headless=new')
options.add_argument('--hide-scrollbars')
options.add_argument('--window-size=1920,1080')
driver = webdriver.Chrome(options=options)
screenshot = driver.execute_cdp_cmd('Page.captureScreenshot', {'captureBeyondViewport': True})
with open('full_page.png', 'wb') as file:
    file.write(base64.b64decode(screenshot['data']))
Sample Program

This script opens example.com in Chrome headless mode, waits for the page to load, uses Chrome DevTools Protocol to save a full page screenshot as 'full_page_example.png', and prints confirmation.

Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time
import base64

options = Options()
options.add_argument('--headless=new')  # Use new headless mode for full page screenshot support
options.add_argument('--window-size=1920,1080')

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

try:
    driver.get('https://example.com')
    time.sleep(2)  # Wait for page to load
    screenshot = driver.execute_cdp_cmd('Page.captureScreenshot', {'captureBeyondViewport': True})
    with open('full_page_example.png', 'wb') as f:
        f.write(base64.b64decode(screenshot['data']))
    print('Full page screenshot saved successfully.')
finally:
    driver.quit()
OutputSuccess
Important Notes

Full page screenshot can be captured using Chrome DevTools Protocol (CDP) driver.execute_cdp_cmd('Page.captureScreenshot', {'captureBeyondViewport': True}) (Chrome 59+). Requires import base64.

If your browser does not support full page screenshots natively, you may need to scroll and stitch images manually.

Always wait for the page to fully load before taking a screenshot to avoid incomplete captures.

Summary

Full page screenshots capture the entire webpage, not just what you see on screen.

Use Chrome DevTools Protocol like driver.execute_cdp_cmd('Page.captureScreenshot', {'captureBeyondViewport': True}) for easy full page capture.

Waiting for page load and using headless mode helps get clean, complete screenshots.