Scrapeless Wiki

How Does a Headless Browser Work?

Informational P1 how does a headless browser work
Overlaps a live page. A published page already targets this keyword: https://www.scrapeless.com/en/blog/what-is-a-headless-browser. Publishing both risks cannibalisation β€” decide before this goes live.

Learn how a headless browser runs a full engine without painting to a screen, how it is controlled over a debugging protocol, and what it costs to run.

A headless browser is a complete browser with the window removed. It parses HTML, applies CSS, executes JavaScript, builds a layout and composites frames β€” it simply never sends those frames to a display.

1. What "Headless" Removes

The misconception worth clearing first: headless does not mean a lighter engine or a partial implementation. Chrome in headless mode is Chrome.

What is removed is the presentation layer β€” the OS window, the toolbar, tabs, and the compositor's output to a screen. Everything below that runs unchanged:

  • HTML parsing and DOM construction
  • CSS cascade, style resolution and layout
  • JavaScript execution in the same engine
  • Network stack, cookies, storage, service workers
  • Rendering and rasterisation β€” into memory rather than onto a monitor

Because rendering still happens internally, screenshots work: the browser composites a frame and hands you the bitmap instead of painting it.

2. How It Is Controlled

You do not interact with a headless browser through a UI, so control happens over a debugging protocol.

  1. The browser starts with a remote debugging port open.
  2. Your script connects β€” usually a WebSocket, speaking Chrome DevTools Protocol.
  3. Commands go over that connection: navigate, evaluate JavaScript, click at coordinates, capture a screenshot, intercept a request.
  4. Events stream back: page loaded, request issued, console message, DOM mutation.

Playwright and Puppeteer are ergonomic wrappers over this. Selenium reaches the same engine through the W3C WebDriver protocol and a driver binary instead.

3. The Rendering Pipeline

Stage What happens Same when headless?
Fetch Resources retrieved over the network Yes
Parse HTML β†’ DOM, CSS β†’ CSSOM Yes
Script JavaScript executes, may mutate the DOM Yes
Layout Geometry computed for every box Yes
Paint Draw commands generated Yes
Composite Layers combined into a frame Yes
Display Frame sent to a monitor No

Only the last row differs. This is why headless mode costs almost as much CPU and memory as headful β€” you skip presenting a frame, not producing one.

4. Why It Is Used

  • Server environments. Servers have no display. Headless is what makes browsers runnable in a container or CI pipeline.
  • JavaScript-rendered pages. When content is assembled client-side, fetching HTML returns an empty shell. A browser executes the code that fills it.
  • Faithful interaction. Clicks, scrolls and form submissions produce the same events a user's would.
  • Screenshots and PDFs, generated from real layout.
  • Testing, where you want the real engine and none of the window.

5. What It Costs

A browser instance is expensive next to an HTTP request:

  • Memory: typically 100–300 MB per instance, more with heavy pages.
  • CPU: parsing, script execution and layout for every page.
  • Time: hundreds of milliseconds to seconds per page, against tens of milliseconds for a fetch.
  • Concurrency: dozens per machine, not thousands.

The practical rule: use a browser when the page genuinely requires one. Many pages that look client-rendered are fetching JSON from an endpoint you can call directly β€” check the Network tab before committing to a browser.

Where a browser is required, cost control means reusing instances across pages, isolating with contexts rather than fresh launches, and blocking images, fonts and analytics you do not need.

6. Headless Is Detectable

Historically, headless Chrome differed from headful in observable ways β€” a HeadlessChrome user agent, navigator.webdriver set, missing plugin arrays, software rendering exposed through WebGL, and no real window dimensions.

Chrome's newer headless mode closed much of that gap by running the same browser binary rather than a separate build. But differences remain, particularly around graphics: a browser reporting SwiftShader instead of a consumer GPU is a strong signal, and containers commonly have no GPU at all.

Detection also looks at behaviour. A page loaded with no mouse movement, no scroll, and interactions at exact element centres reads as automated regardless of the environment.

7. Real-World Examples

  • A scraper receiving an empty <div id="root"> from a fetch, and full content through a browser.
  • A CI suite running Chrome headless in a container with no display server.
  • A PDF invoice service rendering an HTML template and printing to PDF.
  • A monitoring job screenshotting a dashboard on a schedule.
  • A pipeline that dropped a browser entirely after finding the page's own JSON endpoint β€” an order of magnitude faster.

8. Summary

A headless browser is a full engine with the display removed. Parsing, styling, script execution, layout and rendering all proceed normally; only the final step of showing a frame is skipped, which is why screenshots still work and why the resource cost stays close to headful.

Control happens over a debugging protocol, wrapped by libraries such as Playwright and Puppeteer. Use one when a page truly needs JavaScript execution or genuine interaction β€” and check first whether the data is available from an endpoint the page itself calls, because that path is faster by an order of magnitude.