Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use chromedp to launch or connect to a Chrome-based browser, navigate to a URL, capture the viewport, an element, or the full page, and write the returned image bytes to a file. For most whole-page captures, the workflow is chromedp.NewContext, chromedp.Navigate, chromedp.FullScreenshot, and os.WriteFile.

Choose the screenshot type you need

Result chromedp action What it captures
Visible browser area chromedp.CaptureScreenshot(&buf) The current viewport, not the page content outside it.
One page element chromedp.Screenshot(selector, &buf) The bounds of the first element matching the selector. It returns an error if no element matches.
Entire page chromedp.FullScreenshot(&buf, quality) A capture beyond the viewport, suitable for a full-page image.

The high-level helpers cover the usual cases. The underlying Chrome DevTools Protocol also exposes options such as format, quality, clipping, capture beyond the viewport, and speed optimization; see the cdproto Page bindings if you need a specialized capture.

Install chromedp and prepare the browser

In a Go module, add chromedp with go get:

go get github.com/chromedp/chromedp

chromedp drives a browser through the Chrome DevTools Protocol. You need a usable Chrome or Chromium browser environment. The current official examples demonstrate the API, but do not establish a version-pinned compatibility matrix for Go, chromedp, and Chrome; check the requirements for the specific versions you install.

Capture and save a full-page screenshot

This complete example navigates to a page, requests a full-page PNG, checks both operation and file errors, and saves the bytes as screenshot.png:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
package main

import (
	"context"
	"log"
	"os"

	"github.com/chromedp/chromedp"
)

func main() {
	ctx, cancel := chromedp.NewContext(context.Background())
	defer cancel()

	var buf []byte
	err := chromedp.Run(ctx,
		chromedp.Navigate("https://example.com"),
		chromedp.FullScreenshot(&buf, 100),
	)
	if err != nil {
		log.Fatal(err)
	}
	if err := os.WriteFile("screenshot.png", buf, 0o644); err != nil {
		log.Fatal(err)
	}
}

Run it from the module directory with go run .. The image is written to the process’s current working directory. FullScreenshot uses PNG when quality is 100; other quality values select JPEG. The implementation documents valid quality values as 0 through 100. See the chromedp project and its official screenshot example for the helper usage.

Capture the viewport or a single element

Visible viewport

Replace chromedp.FullScreenshot(&buf, 100) with:

chromedp.CaptureScreenshot(&buf)

This captures the currently visible viewport. It does not request content beyond the viewport.

First matching element

Use a CSS selector to capture an element’s bounds:

chromedp.Screenshot("main article", &buf)

The action targets the first matching node. If the selector matches nothing, the action fails; inspect the error rather than assuming a file was produced. To save the result, use the same os.WriteFile pattern from the full-page example.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Wait for the page state your screenshot requires

A successful navigation does not by itself guarantee that delayed images, client-rendered components, or other asynchronous content have finished loading. Add a wait that corresponds to the actual page condition you need—for example, waiting for a known selector before capturing—rather than relying on one fixed sleep for every site. The official screenshot example shows how actions are composed with navigation; the page’s own readiness requirements determine what additional wait is appropriate.

Account for device emulation

If your workflow uses device emulation or custom viewport settings, note that chromedp.FullScreenshot overrides device emulation settings. The official example advises: “Use device.Reset to reset the emulation and viewport settings.” If later actions depend on the original emulation or viewport, reset them after the full-page capture.

Troubleshoot common failures

  • No screenshot file appears: Check the error returned by chromedp.Run first, then check the separate error from os.WriteFile. The output path is relative to the current working directory unless you provide an absolute path.
  • Element capture fails: The selector may not match an element at capture time. Verify the selector and add a page-specific wait for the element if it appears asynchronously.
  • The screenshot is cut off at the viewport: Use FullScreenshot for the entire page. CaptureScreenshot captures only the visible viewport.
  • Images or app content are missing: Navigation may have completed before that content rendered. Wait for the relevant selector or readiness condition rather than assuming all resources are ready.
  • Viewport or device settings change unexpectedly: This can occur with FullScreenshot; reset emulation and viewport settings with device.Reset when your workflow requires their restoration.
  • Capture format differs from expectation: With FullScreenshot, quality 100 selects PNG, while other quality values select JPEG. Choose the value with the intended output format in mind.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If you need a screenshot without managing a local browser, ScreenshotNeo provides a website screenshot API and MCP server. This cURL request saves a screenshot of the target URL; replace YOUR_API_KEY with your key. See the ScreenshotNeo API documentation for request options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp
  • Cookie banners are accepted and removed, along with known consent banners, newsletter popups, and chat widgets, before capture; each cleanup step can be turned off.
  • Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed; response headers report the page verdict and billing status.
  • An MCP server gives AI agents tools for screenshots, page information, and PDF capture.
  • The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots.

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Does chromedp save the screenshot file automatically?

No. Capture actions return image bytes; write them to disk with os.WriteFile.

Can I capture a page as JPEG instead of PNG?

Yes. For FullScreenshot, use a quality value other than 100 to select JPEG.

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.