Skip to main content
SeleniumDecoded

Take a Full-Page Screenshot (Beyond the Viewport)

Capture the entire scrollable page, not just the visible viewport: native in Firefox, a CDP command in Chrome and Edge, and why stitching scrolled images fails.

Medium Verified against Selenium 4.52.0

Problem

getScreenshotAs captures only the viewport. For a failure report or a visual check of a long landing page, you need everything from header to footer. Scrolling and stitching several screenshots together produces duplicated sticky headers and seams.

Solution

Use the browser’s own full-page capture. Firefox exposes it directly through the driver. On Chrome and Edge, ask the DevTools protocol for a screenshot with captureBeyondViewport and a clip the size of the whole document.

Firefox (native)

Firefox full-page screenshot
Selenium 4 Stable
FirefoxDriver firefox = (FirefoxDriver) driver;
File png = firefox.getFullPageScreenshotAs(OutputType.FILE);
Files.copy(png.toPath(), Path.of("target/screenshots/full.png"), StandardCopyOption.REPLACE_EXISTING);
# driver is webdriver.Firefox
driver.get_full_page_screenshot_as_file("screenshots/full.png")
var firefox = (FirefoxDriver)driver;
Screenshot full = firefox.GetFullPageScreenshot();
full.SaveAsFile("screenshots/full.png");

The JavaScript bindings have no dedicated Firefox full-page helper; use the BiDi origin: "document" capture described under Variations.

Chrome and Edge (CDP)

Chromium full-page screenshot via CDP
Selenium 4 Medium
ChromiumDriver chrome = (ChromiumDriver) driver;
Map<String, Object> metrics = chrome.executeCdpCommand("Page.getLayoutMetrics", Map.of());
@SuppressWarnings("unchecked")
Map<String, Object> size = (Map<String, Object>) metrics.get("cssContentSize");
Map<String, Object> shot = chrome.executeCdpCommand("Page.captureScreenshot", Map.of(
"format", "png",
"captureBeyondViewport", true,
"clip", Map.of(
"x", 0, "y", 0,
"width", ((Number) size.get("width")).doubleValue(),
"height", ((Number) size.get("height")).doubleValue(),
"scale", 1)));
byte[] png = Base64.getDecoder().decode((String) shot.get("data"));
Files.write(Path.of("target/screenshots/full.png"), png);
import base64
metrics = driver.execute_cdp_cmd("Page.getLayoutMetrics", {})
size = metrics["cssContentSize"]
shot = driver.execute_cdp_cmd("Page.captureScreenshot", {
"format": "png",
"captureBeyondViewport": True,
"clip": {"x": 0, "y": 0, "width": size["width"], "height": size["height"], "scale": 1},
})
with open("screenshots/full.png", "wb") as f:
f.write(base64.b64decode(shot["data"]))
const fs = require('node:fs/promises');
const metrics = await driver.sendAndGetDevToolsCommand('Page.getLayoutMetrics', {});
const { width, height } = metrics.cssContentSize;
const shot = await driver.sendAndGetDevToolsCommand('Page.captureScreenshot', {
format: 'png',
captureBeyondViewport: true,
clip: { x: 0, y: 0, width, height, scale: 1 },
});
await fs.writeFile('screenshots/full.png', Buffer.from(shot.data, 'base64'));
var chrome = (ChromiumDriver)driver;
var metrics = (Dictionary<string, object>)chrome.ExecuteCdpCommand("Page.getLayoutMetrics", new());
var size = (Dictionary<string, object>)metrics["cssContentSize"];
var shot = (Dictionary<string, object>)chrome.ExecuteCdpCommand("Page.captureScreenshot", new()
{
["format"] = "png",
["captureBeyondViewport"] = true,
["clip"] = new Dictionary<string, object>
{
["x"] = 0, ["y"] = 0,
["width"] = Convert.ToDouble(size["width"]),
["height"] = Convert.ToDouble(size["height"]),
["scale"] = 1,
},
});
File.WriteAllBytes("screenshots/full.png", Convert.FromBase64String((string)shot["data"]));

Why It Works

Both approaches let the browser render the whole document into one image in a single pass, so fixed and sticky elements appear once, in their natural position, and there are no seams. Page.getLayoutMetrics returns the full document size in CSS pixels; captureBeyondViewport tells Chrome to render content outside the visible area instead of clipping it.

Variations

  • WebDriver BiDi. The browsingContext.captureScreenshot command accepts origin: "document", which captures the full page in any browser that supports BiDi. It is the long-term cross-browser answer; check your binding’s BiDi browsing context API for the exact method, since names have shifted between releases.
  • Through Selenium Grid. executeCdpCommand needs a local ChromiumDriver. On a RemoteWebDriver, wrap it with Augmenter (Java) or use BiDi, which works over Grid by design.

Gotchas

  • Lazy-loaded images below the fold may be blank because they were never scrolled into view. Scroll to the bottom and back (or wait for img elements to have complete === true) before capturing.
  • 100vh layouts. Hero sections sized with vh units can stretch to the full document height when Chrome expands the viewport for capture. If the image looks distorted, set an explicit window size first.
  • Very tall pages (over roughly 16,000 px) can hit GPU texture limits and produce truncated or blank images. Capture sections, or reduce scale.
  • Inner scroll containers. If the page body does not scroll but an inner div does, the document is only viewport-tall and a full-page capture adds nothing. Screenshot the container element instead (see Screenshot an Element to a File).

Learn the theory