The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Use Selenium’s Node.js WebDriver to launch Chrome with headless options, open a page, and save driver.takeScreenshot() as a PNG. Selenium returns the image as a base64-encoded string, so write it with base64 decoding enabled. The example below also closes Chrome in a finally block so the browser is cleaned up if navigation or saving fails.
Install Selenium and prepare Chrome
Install the official JavaScript bindings with npm install selenium-webdriver. The Selenium JavaScript API currently states that Node.js 22 or newer is required; check its API documentation for the supported Node release lines when setting up a project.
As an Amazon Associate I earn from qualifying purchases.
Selenium Manager can automatically handle browser-driver setup in supported environments. If you manage ChromeDriver yourself, Selenium’s Chrome guide says its major version must match Chrome’s major version. The code uses Chrome’s --headless option, as in Selenium’s JavaScript example. The Chrome guide also lists --headless=new among commonly used arguments; choose an argument supported by the Chrome version and runtime you use.
Capture a page with Selenium
Save this as, for example, capture.js, then run node capture.js. Replace the example URL and output path as needed.
#1 Best Overall
const { Builder, Browser } = require('selenium-webdriver')
const chrome = require('selenium-webdriver/chrome')
const fs = require('node:fs/promises')
async function capture(url, outputPath = 'screenshot.png') {
const options = new chrome.Options().addArguments('--headless')
const driver = await new Builder()
.forBrowser(Browser.CHROME)
.setChromeOptions(options)
.build()
try {
await driver.get(url)
const pngBase64 = await driver.takeScreenshot()
await fs.writeFile(outputPath, pngBase64, 'base64')
} finally {
await driver.quit()
}
}
capture('https://example.com').catch((error) => {
console.error(error)
process.exitCode = 1
})
takeScreenshot() resolves to a base64-encoded PNG, not raw image bytes. Passing 'base64' as the write encoding decodes the string into a valid PNG file. The official Selenium JavaScript example uses this write pattern. This is an implementation pattern based on the documented API and example, not a claim that it was executed or tested here.
Capture a particular element
For a visible element rather than the page, find it and call takeScreenshot() on the returned WebElement:
Rank #2
const element = await driver.findElement({ css: '.product-card' })
const pngBase64 = await element.takeScreenshot()
await fs.writeFile('element.png', pngBase64, 'base64')
Use this inside the capture function’s try block, after navigation. Selenium documents element screenshots as the visible region within the element’s bounding rectangle; the result is not a promise of capturing hidden or off-screen portions of a tall element. See the WebDriver API and JavaScript example.
Free tools Windows power users keep installed
One-click scans. No signup required.
What Selenium captures—and what it does not guarantee
Selenium documents page screenshots as best effort, preferring the entire page, then the current window, the visible portion of the current frame, and finally the entire display containing the browser. The actual result depends on the page and driver; do not assume every page produces full-page pixels. For element screenshots, capture is limited to the visible part of that element’s bounding rectangle. These behaviors are described in the WebDriver API.
Rank #3
Choose Selenium or Chrome’s command-line capture
| Approach | Use it when | What to account for |
|---|---|---|
| Selenium WebDriver | You need scripted browser interaction, element capture, or screenshot steps inside automated tests. | Set up a WebDriver session and ensure Chrome and ChromeDriver are compatible, or use Selenium Manager’s supported setup behavior. |
| Chrome headless CLI | You need a direct one-off page screenshot and do not need Selenium automation. | It is a command-line capture rather than a WebDriver test flow. Chrome’s reference documents the command and output location. |
Chrome’s official headless command-line reference gives this example, which saves screenshot.png in the current working directory:
chrome --headless --screenshot --window-size=412,892 https://developer.chrome.com/
Run the browser remotely when needed
If the browser should run on a Selenium Grid or standalone server rather than on the machine running Node.js, Selenium’s JavaScript API documents configuring a Builder with a remote server URL. This can be useful when browser execution is managed separately or shared across a test environment. Consult the Selenium JavaScript API for the remote Builder options.
Troubleshoot common failures
- Chrome fails to start or the session cannot be created: Check that Chrome is installed and available in the environment. If you installed ChromeDriver manually, align its major version with Chrome’s; Selenium Manager may handle driver setup in supported environments. See the Chrome guide and API setup notes.
- The screenshot file is invalid or unreadable: Ensure you pass
'base64'towriteFile. The screenshot result is base64 text, so writing it as ordinary text does not decode the PNG. - The output is not a full-page image: Selenium describes page capture as best effort rather than a guarantee of full-page pixels. For a specific visible region, use an element screenshot; if full-page capture is essential, verify the output in the target browser and environment.
- Chrome starts but a page or element is not ready: Navigation completing does not establish that every page-specific asset or asynchronous element is ready for capture. Add an explicit wait for a condition relevant to your page before taking the screenshot; choose the condition based on the page rather than assuming a fixed delay works universally.
- The process leaves Chrome running after an error: Keep
driver.quit()in afinallyblock so it runs after errors in navigation or screenshot writing. If session creation itself fails before a driver is assigned, there is no established session to close.
Or skip the browser setup
ScreenshotNeo is a website screenshot API and MCP server. A single GET request can return an image or PDF without setting up Selenium locally. For example, this cURL request saves a WebP screenshot:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp
See the ScreenshotNeo documentation for request options. It accepts cookie/consent banners and removes known consent platforms, newsletter popups, and chat widgets before capture; these steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, and responses identify page verdict and billing status in headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf for AI agents and MCP clients. The Free plan includes 1,000 shots per month without a card; paid plans start at $5 for 3,000 shots.
Sign up free for 1,000 screenshots a month, with no card required.
Best Value
Frequently Asked Questions
Can Selenium save a screenshot as JPEG or WebP with this method?
No. Selenium’s documented WebDriver screenshot result is a base64-encoded PNG; the example writes that PNG to disk.
Can I use Selenium screenshots in a remote Grid session?
Yes. Selenium’s JavaScript API documents configuring a Builder to connect to a Grid or standalone server using a remote server URL.
Quick Recap
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.




