Use chromedp to open a page, choose whether to capture the viewport, a matching element, or the full page, then write the returned image bytes to disk. The right capture action depends on how much of the page you need.
Choose the capture that matches the image you need
| Desired result | chromedp action | What it captures |
|---|---|---|
| Visible viewport | chromedp.CaptureScreenshot(&buf) |
The current browser viewport, not content beyond it. |
| One element | chromedp.Screenshot(selector, &buf) |
The first element matching the selector. It returns an error if no node matches. |
| Entire page | chromedp.FullScreenshot(&buf, quality) |
A full-page capture that extends beyond the viewport. |
The examples below use the official chromedp screenshot API and example: screenshot.go and the screenshot example. They are from the projects’ current main branches, not a version-pinned compatibility matrix; check the APIs against the chromedp version in your Go module.
As an Amazon Associate I earn from qualifying purchases.
Capture and save a full-page screenshot
Create a chromedp context, navigate, run the full-page capture, and save its bytes. This complete example checks errors from both the browser actions and file output:
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →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)
}
}
Save the file with a .png extension when quality is 100: chromedp’s FullScreenshot selects PNG at that value and JPEG at other quality values from 0 through 100. Use a matching extension such as .jpg for JPEG output. The helper’s implementation and behavior are documented in chromedp’s screenshot source.
#1 Best Overall
Capture only the viewport or a specific element
Visible viewport
To capture only what is currently visible in the browser, replace the full-page action with chromedp.CaptureScreenshot:
var buf []byte
err := chromedp.Run(ctx,
chromedp.Navigate("https://example.com"),
chromedp.CaptureScreenshot(&buf),
)
if err != nil {
log.Fatal(err)
}
if err := os.WriteFile("viewport.png", buf, 0o644); err != nil {
log.Fatal(err)
}
One matching element
Pass a CSS selector to chromedp.Screenshot to capture the first matching node. For example, replace the capture action above with:
chromedp.Screenshot("main article", &buf)
Keep the surrounding chromedp.Run error check and file-writing step. If the selector matches no node, the action returns an error; choose a selector present on the target page or wait for that element to appear before capturing.
Wait for the page’s actual ready condition
Successful navigation does not establish that every delayed image or application-rendered component is ready for capture. The screenshot examples do not promise that all asynchronous content has finished rendering when navigation returns. If a page has a known readiness condition, add an appropriate wait action before the screenshot—for example, wait for the target selector rather than relying on the navigation step alone. A fixed delay may work for one page and fail for another, so use it only when a delay is genuinely the condition you need.
Full-page capture and device emulation
FullScreenshot captures beyond the viewport, but it also overrides device emulation settings. The official example warns: “Note: chromedp.FullScreenshot overrides the device’s emulation settings. Use device.Reset to reset the emulation and viewport settings.” If you have configured emulation and need those settings restored, follow that guidance and use device.Reset as appropriate for your workflow. See the official example.
Troubleshooting
- The screenshot action returns an error: Check the error from
chromedp.Run. For element capture, confirm the CSS selector matches an element on the page;Screenshoterrors when it cannot find one. - Content is missing from the image: Navigation alone does not guarantee that delayed images or app-rendered content is ready. Wait for a page-specific selector or other appropriate readiness condition before capture.
- The full-page result differs from the emulated viewport:
FullScreenshotoverrides device emulation settings. Reset them withdevice.Resetwhen needed. - The file cannot be written: Check the error returned by
os.WriteFile, verify the destination is writable, and ensure the filename extension reflects the selected image format. - The image format is unexpected: For
FullScreenshot, quality100selects PNG; other valid quality values select JPEG. Name the output file accordingly.
Protocol options for specialized captures
The high-level helpers cover viewport, element, and full-page captures. If they do not fit a specialized need, the Chrome DevTools Protocol’s Page screenshot options include format, quality, clipping, capture from surface, capture beyond the viewport, and speed optimization. The generated bindings document PNG as the default format and capture-beyond-viewport as off by default. See the Page protocol bindings for the lower-level options; their availability and behavior should be checked against the protocol version used by your project.
Rank #4
Or skip the browser setup
If you need a screenshot without managing a Go browser workflow, ScreenshotNeo provides a one-request API. The following cURL example saves a WebP screenshot of https://example.com. See the ScreenshotNeo API documentation.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp
ScreenshotNeo accepts cookie and consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each of those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers indicate the page verdict and whether the request was billed. Its MCP server gives AI agents tools for screenshots, page information, and PDF capture. The free plan includes 1,000 shots per month without a card; paid plans start at $5 for 3,000 shots. Learn more at ScreenshotNeo, or sign up for 1,000 free screenshots a month with no card.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Frequently Asked Questions
Can chromedp take a screenshot of an element selected by CSS?
Yes. Use `chromedp.Screenshot(selector, &buf)`; it captures the first matching element and returns an error if none matches.
Best Value
Does `chromedp.FullScreenshot` save a PNG at every quality setting?
No. It selects PNG at quality 100 and JPEG for other valid quality values.
Where can I find the underlying Chrome screenshot options?
The chromedp Page bindings document protocol-level options such as clipping and capture beyond the viewport: https://github.com/chromedp/cdproto/blob/master/page/page.go.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →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.




