ScreenshotNeo

BlogHow-to

How to Measure Total Page Length with Selenium Java

Read document.scrollingElement.scrollHeight with Selenium Java to measure full page content, handle dynamic pages, frames, and common height-property mistakes.

By the ScreenshotNeo team30 September 20268 min read

How to Measure Total Page Length with Selenium Java

Use Selenium JavaScript execution to read document.scrollingElement.scrollHeight. It returns the document’s content height in CSS pixels, including content below the viewport. The value includes padding, excludes borders and margins, and is measured at the moment the script runs.

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

// driver is initialized and currently displays the target page.
long pageHeight = ((Number) ((JavascriptExecutor) driver)
    .executeScript("return document.scrollingElement.scrollHeight;"))
    .longValue();

System.out.println("Document content height: " + pageHeight + " CSS pixels");

Selenium’s JavascriptExecutor API runs the expression in the selected window and frame. Converting the result through Number handles Selenium’s Java return mapping safely: whole numbers are returned as Long and decimal numbers as Double.

1. Complete runnable Java example

The following Maven example opens a page, waits for its document body, executes the measurement, and prints the result. Add the Selenium Java dependency to your project, then provide a browser driver through Selenium Manager or your normal driver configuration.

Selenium reads the scrolling element's scrollHeight after the page reaches the required state.
Selenium reads the scrolling element's scrollHeight after the page reaches the required state.
import java.time.Duration;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class PageLength {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        try {
            driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(60));
            driver.get("https://example.com");

            new WebDriverWait(driver, Duration.ofSeconds(20)).until(
                (ExpectedCondition<Boolean>) d ->
                    (Boolean) ((JavascriptExecutor) d).executeScript(
                        "return document.readyState === 'interactive' || " +
                        "document.readyState === 'complete';")
            );

            Object rawHeight = ((JavascriptExecutor) driver).executeScript(
                "const scrolling = document.scrollingElement;" +
                "return scrolling ? scrolling.scrollHeight : null;"
            );

            if (!(rawHeight instanceof Number)) {
                throw new IllegalStateException("The document has no measurable scrolling element");
            }

            long pageHeight = ((Number) rawHeight).longValue();
            System.out.println("Document content height: " + pageHeight + " CSS pixels");
        } finally {
            driver.quit();
        }
    }
}

The Selenium WebDriver documentation covers driver setup and browser lifecycle. The height expression itself is browser JavaScript, so it does not require a special Selenium command.

2. Why scrollHeight is the right property

scrollHeight measures the full content extent of an element, including content that is currently outside its visible area because of scrolling. For the document, document.scrollingElement identifies the element that actually scrolls the page. In standards mode this is normally document.documentElement; in some quirks-mode cases it can be body. A document can also have no scrolling element, so production code should handle null.

Property What it measures Use it for total document length?
scrollHeight Content extent, including overflow below the viewport; includes padding and excludes border and margin. Yes
clientHeight Visible content area including padding, excluding borders, margins, and the scrollbar. No, it describes the viewport area.
offsetHeight Rendered layout size including padding, borders, and a scrollbar when present. Only when you specifically need the occupied box size.

MDN documents the behavior of Element.scrollHeight, Document.scrollingElement, and the related dimension properties in its CSS Object Model dimensions guide.

3. Measure after the page reaches the state you need

The number is a snapshot of the DOM when executeScript runs. A navigation event does not guarantee that client-rendered content, images, ads, or lazy sections have finished changing the layout.

Wait for a known application state

new WebDriverWait(driver, Duration.ofSeconds(30)).until(
    d -> d.findElement(By.cssSelector("main article"))
          .isDisplayed()
);

long height = ((Number) ((JavascriptExecutor) driver).executeScript(
    "return document.scrollingElement.scrollHeight;"
)).longValue();

Wait for a loading marker to disappear

new WebDriverWait(driver, Duration.ofSeconds(30)).until(
    d -> d.findElements(By.cssSelector(".loading-spinner")).isEmpty()
);

Wait for the height to stabilize

For pages that append content briefly after rendering, poll the height until repeated readings stop changing. Choose a timeout appropriate for the site rather than assuming a fixed delay.

JavascriptExecutor js = (JavascriptExecutor) driver;
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));

wait.until(d -> {
    long first = ((Number) js.executeScript(
        "return document.scrollingElement ? document.scrollingElement.scrollHeight : 0;"
    )).longValue();
    try {
        Thread.sleep(300);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        return false;
    }
    long second = ((Number) js.executeScript(
        "return document.scrollingElement ? document.scrollingElement.scrollHeight : 0;"
    )).longValue();
    return first == second;
});

A fixed sleep can be useful for a controlled test, but a state-based wait is more reliable because network and rendering times vary.

4. Lazy-loaded and infinite-scroll pages

A single scrollHeight read includes only nodes already present in the DOM. Infinite-scroll applications may not create later items until the viewport approaches the bottom. If the requirement is the eventual complete feed, progressively scroll and wait for the height to stop increasing.

Infinite-scroll pages may require controlled scrolling before the final height is meaningful.
Infinite-scroll pages may require controlled scrolling before the final height is meaningful.
JavascriptExecutor js = (JavascriptExecutor) driver;
long previous = 0;
int unchangedRounds = 0;

for (int i = 0; i < 40 && unchangedRounds < 3; i++) {
    long current = ((Number) js.executeScript(
        "return document.scrollingElement.scrollHeight;"
    )).longValue();

    if (current == previous) {
        unchangedRounds++;
    } else {
        unchangedRounds = 0;
        previous = current;
    }

    js.executeScript("window.scrollTo(0, document.scrollingElement.scrollHeight);");
    Thread.sleep(500);
}

long finalHeight = ((Number) js.executeScript(
    "return document.scrollingElement.scrollHeight;"
)).longValue();

This loop has a safety limit because some feeds never settle. It also assumes the page loads more content when scrolled near the bottom. For a lazy image inside an existing element, waiting for the image or scrolling it into view may be enough.

5. Measuring one element instead of the whole page

If you need the height of a particular container, execute against that element. The document’s scrolling element may be much taller or shorter than the component you care about.

WebElement article = driver.findElement(By.cssSelector("article"));
long articleHeight = ((Number) ((JavascriptExecutor) driver).executeScript(
    "return arguments[0].scrollHeight;", article
)).longValue();

System.out.println("Article content height: " + articleHeight + " CSS pixels");

For the rendered border-box dimensions, Selenium’s WebElement.getSize() reports width and height. That answers a different question from the overflow-aware scrollHeight value.

6. Frames and windows

JavaScript runs in Selenium’s currently selected browsing context. If the content is inside an iframe, switch into it before measuring. To measure the top-level document again, switch back to default content.

driver.switchTo().frame(driver.findElement(By.cssSelector("iframe.content")));
long frameHeight = ((Number) ((JavascriptExecutor) driver).executeScript(
    "return document.scrollingElement ? document.scrollingElement.scrollHeight : 0;"
)).longValue();

driver.switchTo().defaultContent();

Cross-origin restrictions affect what page JavaScript can access, but Selenium’s script executes in the currently selected frame. Select the correct frame first and do not expect a script in the parent document to measure an iframe’s internal document automatically.

7. Units, rounding, and viewport considerations

  • The result is an integer number of CSS pixels.
  • It is not a physical measurement in millimeters or device pixels.
  • Changing the viewport width can trigger responsive layout changes and therefore change the total height.
  • Device pixel ratio and browser zoom do not turn the returned CSS-pixel value into a screenshot’s bitmap height.
  • Margins outside the scrolling element are not included in scrollHeight.

8. Common errors and fixes

Symptom Cause Fix
NullPointerException after reading scrollHeight document.scrollingElement is null in the current document context. Return and check null in JavaScript before converting to Number.
Height is only the visible viewport clientHeight was used instead of scrollHeight. Read document.scrollingElement.scrollHeight.
Height is unexpectedly small Measurement ran before client rendering, images, or lazy sections completed. Wait for a selector, loading marker, network-driven state, or stable repeated height.
Parent page height ignores iframe content JavaScript ran in the parent browsing context. Switch to the iframe, measure, then switch back.
Height changes on every run The page is still appending content, animating, or loading images. Wait for application readiness or poll until the value stabilizes.
Script execution error The driver was closed, the window changed, or the selected context disappeared. Confirm the window and frame are active and execute before quit().
Number conversion fails The expression returned an unexpected value such as null. Check instanceof Number and return a diagnostic value when absent.

9. Performance and reliability notes

Reading scrollHeight is a small, synchronous JavaScript operation. The expensive parts are usually navigation, JavaScript rendering, image decoding, and any scrolling needed to trigger lazy loading. Avoid polling more frequently than necessary, and stop once the page reaches the state your measurement represents.

For repeatable measurements:

  • Set a consistent browser window size and zoom.
  • Use the same user agent, locale, and test data.
  • Wait on application state instead of relying only on elapsed time.
  • Record the URL, frame context, viewport, timestamp, and readiness condition with the height.
  • Use a maximum wait and maximum scroll count for pages that continuously update.
  • Measure again after known layout-changing actions such as expanding accordions or dismissing banners.

10. Or skip the browser setup

If you need a screenshot or PDF of the resulting page rather than a value inside your test, ScreenshotNeo provides a GET endpoint that captures a URL as PNG, JPEG, WebP, or PDF. See the ScreenshotNeo API documentation for the request options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
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)
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(`HTTP ${res.status}`);
const image = Buffer.from(await res.arrayBuffer());

ScreenshotNeo removes 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 the response identifies the result with X-Page-Verdict and X-Billed headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients. The Free plan includes 1,000 screenshots per month without a card; paid plans start at $5 for 3,000 screenshots.

Create a free ScreenshotNeo account to get started.

11. Cost and operational considerations

Local Selenium runs consume your browser and machine resources; the page itself may also make network requests and load large assets. In a hosted capture service, cost depends on the provider’s billing rules and the capture options you use. ScreenshotNeo bills only clean shots and publishes the billed status in each response, which helps reconcile automated jobs.

12. FAQ

Can I measure page height without scrolling?

Yes. Reading scrollHeight does not require moving the scrollbar. Scrolling is only needed when the application loads additional content in response to scrolling.

Should I use document.body.scrollHeight?

Use document.scrollingElement.scrollHeight for document-level measurements. It follows the element that actually scrolls and handles standards and quirks documents more appropriately.

Does the result include CSS margins?

No. scrollHeight includes padding but excludes border and margin.

Why does the number differ between desktop and mobile tests?

Responsive breakpoints, font loading, viewport width, and content variations can change layout. Keep those inputs consistent when comparing results.

Can Selenium measure a cross-origin iframe?

Selenium can execute JavaScript in the frame after you switch into it. Select the frame first; the parent document’s script does not automatically return the frame document’s height.