Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

Set the browser window height before capturing: driver.set_window_size(width, height), then call driver.save_screenshot() or driver.get_screenshot_as_file(). Selenium’s ordinary screenshot APIs capture the current window; they do not expose a separate screenshot-height or crop argument. If you need a particular page component instead of the whole window, locate it and use element.screenshot().

What “limit screenshot height” means in Selenium

Selenium has two separate operations:

  • Resize the browser window: changes the window and viewport dimensions, which can change responsive layout and what is visible.
  • Capture a screenshot: saves the current window (or a selected element) after the page has rendered.

The Python WebDriver API documents set_window_size(width, height, windowHandle='current'), with dimensions expressed in pixels. The screenshot methods then capture the current window. See the Selenium Python WebDriver API and Selenium’s window and tab documentation.

Set a fixed window height, then capture

This is the direct solution when you want a defined viewport/window size, such as 1024×600:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from selenium import webdriver

with webdriver.Chrome() as driver:
    driver.set_window_size(1024, 600)
    driver.get("https://example.com")
    driver.save_screenshot("screenshot.png")

The second argument, 600, is the requested window height. Replace the first argument with the width your test or rendering target requires. save_screenshot() writes a PNG file and returns a success value according to the driver implementation.

Use get_screenshot_as_file() instead

from selenium import webdriver

with webdriver.Chrome() as driver:
    driver.set_window_size(1280, 720)
    driver.get("https://example.com")
    ok = driver.get_screenshot_as_file("viewport.png")
    if not ok:
        raise RuntimeError("The driver could not save the screenshot")

The two file methods are alternatives. Choose one; do not call both unless you intentionally need two files.

Choose dimensions deliberately

  • Width controls layout as well as image width. A narrower value may activate a mobile breakpoint, wrap text, or move controls.
  • Height controls the visible vertical area. Content below the viewport is not included in an ordinary current-window screenshot.
  • Window and viewport are not always identical. Browser borders, operating-system decorations, headless mode, and driver behavior can affect the final pixel dimensions. Validate the resulting file in the browser/driver combination used by your CI system.

Wait for the page before capturing

Resizing does not wait for navigation, JavaScript, fonts, or images. Capture only after the state you need is present. For a deterministic element, an explicit wait is preferable to an arbitrary sleep:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

with webdriver.Chrome() as driver:
    driver.set_window_size(1024, 600)
    driver.get("https://example.com")

    WebDriverWait(driver, 15).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, "main"))
    )
    driver.save_screenshot("ready.png")

If the page changes after the element appears, wait for the specific image, chart, or status that defines readiness. A fixed delay can be useful for a known animation, but it is less reliable when network speed varies.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Capture only a bounded element

If “limit height” means “include only this card, table, or component,” resizing the entire browser is the wrong abstraction. Selenium documents an element screenshot API:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

with webdriver.Chrome() as driver:
    driver.get("https://example.com")
    panel = WebDriverWait(driver, 15).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, "section.results"))
    )
    panel.screenshot("results.png")

The file contains the located element’s rendered region rather than the whole current window. The element must be present and visible, and the driver must support element screenshots. The exact output can differ with browser, driver, device scale factor, transforms, and scrolling; inspect representative files in your target environment.

When an element screenshot is the better choice

  • You need a component with a natural, content-based height.
  • You want to exclude navigation, cookie notices, or surrounding whitespace.
  • The component is taller or shorter than the browser viewport.

If the element itself scrolls internally, Selenium captures the rendered element area; it does not automatically guarantee an image of every item hidden inside that scrollable region. You may need to change the element’s CSS or scroll it deliberately, then verify the result.

What Selenium does not promise automatically

set_window_size() is not a crop command. It changes browser geometry and can alter page rendering. The consulted Selenium documentation does not establish one universal, cross-browser full-page screenshot behavior. Full-page capabilities and resulting dimensions depend on the selected browser and driver, so consult that pair’s documentation and test it rather than assuming that a tall window or a normal screenshot produces a complete page.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Likewise, do not assume that a requested 600-pixel height always yields a 600-pixel image on every operating system. Headless and headed sessions, browser chrome, zoom, and device-pixel-ratio settings can change the relationship between requested window size, CSS viewport, and output pixels.

Reusable Python helper

Wrapping the two operations makes the intended height explicit at each call site:

from pathlib import Path
from selenium import webdriver

def screenshot_at_size(url: str, output: str, width: int = 1024, height: int = 600) -> Path:
    if width <= 0 or height <= 0:
        raise ValueError("width and height must be positive")

    destination = Path(output)
    with webdriver.Chrome() as driver:
        driver.set_window_size(width, height)
        driver.get(url)
        if not driver.save_screenshot(str(destination)):
            raise RuntimeError(f"Could not save {destination}")
    return destination

screenshot_at_size("https://example.com", "example-1024x600.png")

Use a unique output path in parallel test runs to avoid workers overwriting one another. Keep the browser and driver versions compatible, and pin your test environment when pixel-level comparisons matter.

Common problems and fixes

The image is taller or shorter than requested

Check whether you are measuring CSS pixels or file pixels. Browser UI, headless mode, device scale factor, zoom, and driver implementation can account for differences. Log the window size and inspect driver.get_window_size() after resizing; then standardize the browser, OS image, and scale settings used for comparison.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The page layout changed after resizing

That is expected when responsive breakpoints depend on width or height. Set the final dimensions before navigation when possible, and use the same dimensions for every run. If you need the desktop layout, preserve a desktop width; changing only height cannot prevent width-based breakpoint changes caused elsewhere in your setup.

The screenshot is blank or captures an old state

Navigation or asynchronous rendering may not be complete. Wait for a meaningful selector, text state, image, or application-ready marker. Also check that the URL loaded successfully and that your selector is not inside an iframe or shadow root that requires separate handling.

set_window_size has no visible effect

Some remote, mobile, or driver-specific sessions handle window management differently. Confirm the active window handle, call driver.get_window_size(), and read the target browser/driver’s window-management notes. For mobile emulation, configure the emulated device or viewport through that driver’s supported options instead of assuming desktop window APIs apply identically.

The element screenshot fails

Typical causes are a stale element after a re-render, an element outside the current page state, a zero-size or hidden element, or a selector that matches the wrong node. Locate it after navigation, wait for visibility, and take the screenshot immediately. If the element is inside an iframe, switch into that frame first.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Only part of a long component appears

An element screenshot reflects the rendered element and its scroll state; it is not a general full-page capture feature. Expand the component, scroll it as required, or use a browser/driver-specific full-page facility after verifying its documented behavior.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

For an image endpoint that accepts a URL directly, ScreenshotNeo returns PNG, JPEG, WebP, or PDF from one GET request. Its API can remove cookie and consent banners, newsletter popups, and chat widgets before capture. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing result. It also provides an MCP server with take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients.

Use the ScreenshotNeo documentation for authentication and options. A basic call is:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

The equivalent Python request is:

import requests

r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
    timeout=90,
)
r.raise_for_status()
open("shot.webp", "wb").write(r.content)

And in Node.js:

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot failed: ${res.status}`);
const fs = await import('node:fs/promises');
await fs.writeFile('shot.webp', Buffer.from(await res.arrayBuffer()));

ScreenshotNeo includes full-page capture with lazy images loaded, element selection by CSS selector, custom viewport and device presets, retina scale, waits, custom CSS and JavaScript, request blocking, cookies and headers, caching, signed links, asynchronous jobs, bulk capture, PDF controls, and a usage API. The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots. Create a free ScreenshotNeo account.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Practical decision checklist

  • Use set_window_size(width, height) when you need a controlled viewport and current-window screenshot.
  • Use element.screenshot(path) when the desired output is one bounded component.
  • Wait for the page state that defines a valid capture.
  • Validate dimensions and rendering in the exact browser, driver, operating system, and scale configuration used in production or CI.
  • Verify any full-page behavior with the target browser/driver instead of treating window resizing as cropping.

Frequently Asked Questions

Can I pass a height directly to save_screenshot()?

No. The documented method captures the current window and does not provide a screenshot-specific height parameter. Set the window size first or capture a located element.

Does changing height capture the entire page?

Not necessarily. It changes the browser window and visible viewport. Full-page behavior is browser- and driver-specific and must be verified separately.

Which Selenium method captures one component?

Locate the component as a WebElement and call element.screenshot("file.png") after it is visible.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.