Why Is My Website Screenshot the Wrong Size in Selenium?
A Selenium window size is not a promise about screenshot pixels or page coverage. Measure the rendered viewport and saved image, then check the capture scope.
Selenium’s set_window_size(width, height) sets the browser window dimensions; it does not directly set or guarantee the rendered page viewport or the pixel dimensions of the saved screenshot. The screenshot API captures the current browsing context, so first identify what is wrong: the PNG’s pixel dimensions, the visible portion of the page, or the site’s responsive layout. Then measure the window, page viewport, and image separately.
This guide uses Python. Selenium also has bindings for other languages; the relevant idea is the same: configure the browser window, inspect the rendered page after navigation, and verify the output file. See the official Selenium window documentation and Selenium Python API.
1. Identify which size is wrong
“Wrong size” can describe three different problems. Choose the matching symptom before changing settings:
| Symptom | What to measure | Likely area to investigate |
|---|---|---|
| The saved PNG has unexpected pixel width or height | Image file dimensions and page viewport dimensions | Browser-specific screenshot behavior or pixel scaling |
| The image has the expected dimensions but cuts off lower content | Whether the capture covers the viewport or full document | Screenshot scope and browser support |
| The page uses a mobile or otherwise unexpected layout | Rendered viewport after navigation | Window configuration, display limits, or responsive breakpoints |
A browser window includes more than the webpage’s content area in some configurations. The rendered viewport and the outer window are distinct measurements. Selenium also notes that screen resolution can affect rendering. Do not assume that setting one dimension determines the others.
2. Set the window and inspect the actual viewport
Here is a runnable Python example that sets the window, loads a page, reports the requested window size and rendered viewport, and saves a screenshot. Install Selenium with python -m pip install selenium and use a supported browser and driver setup.
from pathlib import Path
from selenium import webdriver
url = "https://example.com"
output = Path("screenshot.png")
requested_width = 1440
requested_height = 1000
options = webdriver.ChromeOptions()
# For headless runs, keep the same browser and driver configuration
# when comparing screenshots across runs.
# options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)
try:
driver.set_window_size(requested_width, requested_height)
driver.get(url)
# Read the rendered page viewport after navigation.
viewport = driver.execute_script("""
return {
width: window.innerWidth,
height: window.innerHeight,
devicePixelRatio: window.devicePixelRatio,
documentWidth: document.documentElement.scrollWidth,
documentHeight: document.documentElement.scrollHeight
};
""")
print("Requested window:", requested_width, "x", requested_height)
print("Reported window:", driver.get_window_size())
print("Rendered page:", viewport)
if not driver.save_screenshot(str(output)):
raise RuntimeError("WebDriver did not save the screenshot")
print("Saved:", output.resolve())
finally:
driver.quit()
The JavaScript measurement is evaluated in the page. innerWidth and innerHeight report the layout viewport dimensions in CSS pixels; the document dimensions help reveal content extending beyond it. devicePixelRatio is useful diagnostic context, but its presence alone does not prove why a Selenium screenshot has a particular pixel size. Compare it with the actual PNG dimensions and the behavior of your specific browser and driver.
To inspect the output file’s pixel dimensions, use a local image tool. For example, with Pillow installed (python -m pip install Pillow):
from PIL import Image
with Image.open("screenshot.png") as image:
print("PNG pixels:", image.width, "x", image.height)
3. Decide whether you need a viewport or full-page capture
The standard screenshot call captures the current browsing context. If the page below the visible area is missing, that may be expected for a viewport screenshot. Selenium documents a separate full-page screenshot method for Firefox; availability and method names are browser- and binding-specific. Do not assume that a Firefox-specific method works identically in Chrome or every language binding.
# Firefox example: use the Firefox WebDriver API for full-document capture.
from selenium import webdriver
options = webdriver.FirefoxOptions()
# options.add_argument("-headless")
driver = webdriver.Firefox(options=options)
try:
driver.set_window_size(1440, 1000)
driver.get("https://example.com")
driver.get_full_page_screenshot_as_file("full-page.png")
finally:
driver.quit()
Check the documentation for the exact browser, driver, Selenium binding, and version in your environment. Full-document capture can produce a much taller image than a viewport capture. If you need a consistent visual comparison, make the capture scope explicit and use the same method on every run.
4. Use a repeatable diagnosis
- Write down the intended result. Specify whether you expect a viewport image or the entire document, and whether your target dimensions refer to CSS pixels or output image pixels.
- Set the browser window before capture. Call
set_window_size(width, height)and recordget_window_size(). This configures the current window, not a guaranteed page viewport. - Navigate and wait for the page state you need. Measure
window.innerWidthandwindow.innerHeightafter navigation. If content loads asynchronously, wait for the relevant element or state before taking the screenshot. - Save and inspect the image. Record its actual pixel width and height. Compare those values with the viewport measurement rather than inferring them from the requested outer window dimensions.
- Keep the environment fixed. Record browser and driver versions, operating system or container, headless setting, display resolution, requested window size, viewport measurement, and screenshot method.
- Change one factor at a time. If the layout is wrong, investigate the measured viewport and responsive breakpoints. If the viewport is right but PNG dimensions differ, investigate browser-specific screenshot behavior and pixel scaling.
Selenium’s official guidance notes that screen resolution can affect rendering and provides window movement and resizing controls. In containers or virtual displays, verify the available screen configuration as well as the dimensions reported by the page.
5. Common causes and fixes
| What you see | Possible cause | What to do |
|---|---|---|
| Viewport is smaller than the requested window dimensions | The requested size describes the window, while browser chrome or the display environment affects the page area | Measure the page viewport after navigation. Adjust the window configuration based on that measurement, then verify again. |
| Page has a narrow or mobile layout | The rendered viewport crosses a responsive breakpoint, or the display/window setup differs from expectation | Log innerWidth and innerHeight for the capture run and compare with the breakpoint your site uses. |
| Bottom of page is absent | The call captured the current viewport, not the full document | Use a full-page method supported by your browser and binding, or capture the sections you need separately. |
| PNG pixel dimensions differ from CSS viewport dimensions | Browser or driver screenshot behavior, or pixel scaling, may be involved | Inspect the saved image dimensions and devicePixelRatio; reproduce with the same browser-driver combination. The available Selenium references do not establish one universal scaling rule. |
| Same script produces different layouts on different machines | Screen resolution or runtime environment changed | Keep browser, driver, OS/container, display, headless mode, and screenshot method consistent; log them with each artifact. |
| Screenshot is taken before the intended content appears | Navigation completed before client-rendered or lazy content was ready | Wait for a specific page condition or element before capture. Avoid relying on a fixed delay when a meaningful page condition is available. |
6. Performance, reliability, and cost considerations
For repeatable screenshots, the largest reliability gain comes from controlling the capture conditions: browser and driver versions, display environment, viewport measurement, page readiness, and capture scope. Full-page images can be much taller than viewport images, so consider whether your downstream storage, image processing, or visual comparison needs the whole document. Measure the result rather than assuming its dimensions.
Local Selenium avoids a per-capture screenshot API charge, but you operate the browser, driver, runtime, and any display environment yourself. For a small number of captures, this can be straightforward. For recurring or bulk capture, account for browser startup and maintenance, retries, concurrency, storage, and time spent diagnosing environment differences. No universal performance or cost comparison applies across machines and workloads.
Or skip the browser setup
ScreenshotNeo is a website screenshot API and MCP server for developers. It returns a PNG, JPEG, WebP, or PDF from one request. Its API accepts the URL and screenshot options; see the ScreenshotNeo API documentation for available parameters. For a quick request using the given endpoint and adapting the example target URL:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp
import requests
r = requests.get(
"https://api.screenshotneo.com/v1/shot",
params={"access_key": "YOUR_API_KEY", "url": "https://example.com"},
timeout=90,
)
r.raise_for_status()
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://example.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot request failed: ${res.status}`);
const bytes = new Uint8Array(await res.arrayBuffer());
await import('node:fs/promises').then(fs => fs.writeFile('shot.webp', bytes));
Cookie banners are accepted and removed before capture, along with known newsletter popups and chat widgets; each cleanup step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers report the page verdict and billing status. An MCP server gives AI agents tools to take screenshots, get page information, and capture PDFs. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000.
Create a free ScreenshotNeo account to get 1,000 screenshots a month with no card.
FAQ
Does set_window_size set the webpage viewport?
It sets the current browser window dimensions. Inspect the page’s rendered viewport after navigation to know the dimensions the webpage actually received.
Why does the screenshot omit content below the fold?
The standard screenshot captures the current browsing context. Use a full-document method supported by the browser and Selenium binding when you need the whole page.
Should the PNG dimensions always equal window.innerWidth and window.innerHeight?
Do not assume that they will. Compare the measured CSS viewport with the image’s pixel dimensions and check the browser-driver behavior in your environment.
Does device-pixel ratio explain every size mismatch?
No universal Selenium rule is established by the references used here. Treat pixel scaling as one diagnostic possibility and verify it against the actual browser, driver, and saved image.
What should I include when reporting a reproducible issue?
Include browser and driver versions, operating system or container, headless mode, display settings, requested window size, measured viewport, screenshot method, and saved image dimensions.


