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

Use chromedp to control Chrome from Go: navigate to the page, capture either an element or the full page, and write the returned image bytes to a file. For a straightforward container setup, run the Go program with the chromedp project’s chromedp/headless-shell image, which is designed for this integration.

Choose the kind of screenshot you need

In chromedp, the screenshot action determines the image extent. Pick it before writing the capture code:

Goal Action What it captures
One page element chromedp.Screenshot(selector, &buf, chromedp.NodeVisible) The matching visible element.
Current viewport chromedp.CaptureScreenshot(&buf) The browser’s current viewport.
Whole page chromedp.FullScreenshot(&buf, 100) A full-page capture. The quality argument ranges from 0 to 100; 100 produces PNG, while lower values produce JPEG.

These actions are not interchangeable: a viewport capture does not include content below the visible area, while an element capture is limited to the selected node.

Build a Go screenshot program

Create a module and add chromedp:

go mod init example.com/htmlshot
go get github.com/chromedp/chromedp

Save this as main.go. It accepts the target URL as an argument, navigates to it, captures the whole page, and writes page.png in the working directory.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
CanaKit Raspberry Pi 5 Starter Kit PRO - Turbine Black (128GB Edition) (8GB RAM)
  • Includes Raspberry Pi 5 with 2.4Ghz 64-bit quad-core CPU (8GB RAM)
  • Includes 128GB Micro SD Card pre-loaded with 64-bit Raspberry Pi OS, USB MicroSD Card Reader
  • CanaKit Turbine Black Case for the Raspberry Pi 5
  • CanaKit Low Noise Bearing System Fan
  • Mega Heat Sink - Black Anodized
package main

import (
	"context"
	"fmt"
	"net/url"
	"os"

	"github.com/chromedp/chromedp"
)

func main() {
	if len(os.Args) != 2 {
		fmt.Fprintln(os.Stderr, "usage: htmlshot https://example.com")
		os.Exit(2)
	}

	target := os.Args[1]
	parsed, err := url.ParseRequestURI(target)
	if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") || parsed.Host == "" {
		fmt.Fprintln(os.Stderr, "URL must be an absolute http or https URL")
		os.Exit(2)
	}

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

	var buf []byte
	err = chromedp.Run(ctx,
		chromedp.Navigate(target),
		chromedp.FullScreenshot(&buf, 100),
	)
	if err != nil {
		fmt.Fprintln(os.Stderr, "capture failed:", err)
		os.Exit(1)
	}
	if len(buf) == 0 {
		fmt.Fprintln(os.Stderr, "capture returned no image data")
		os.Exit(1)
	}
	if err := os.WriteFile("page.png", buf, 0o644); err != nil {
		fmt.Fprintln(os.Stderr, "write page.png:", err)
		os.Exit(1)
	}
	fmt.Println("saved page.png")
}

The code uses chromedp.NewContext and cancels that context when the program exits. chromedp.Run reports navigation or capture errors; writing the bytes is a separate operation and should also be checked. The URL validation is a basic safeguard, not a complete policy for deciding which destinations an application is allowed to access.

Capture one visible element instead

Replace the full-page action with an element selector, for example:

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

The selector must match an element that is visible when the action runs. For the viewport rather than the full document, use chromedp.CaptureScreenshot(&buf).

Rank #2
CanaKit Raspberry Pi 5 16GB Starter Kit PRO - Turbine Black (128GB Edition) (16GB RAM)
  • Includes Raspberry Pi 5 16GB with 2.4Ghz 64-bit quad-core CPU (16GB RAM)
  • Includes 128GB Micro SD Card pre-loaded with 64-bit Raspberry Pi OS, USB MicroSD Card Reader
  • CanaKit Turbine Black Case for the Raspberry Pi 5
  • CanaKit Low Noise Bearing System Fan
  • Mega Heat Sink - Black Anodized

Set the output format deliberately

FullScreenshot at quality 100 returns PNG data; a lower quality value returns JPEG data. Give the output file an extension that matches the actual format. If you use a different capture action or format, verify the action’s output before naming the file.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

Run the program in Docker

The project’s docker.io/chromedp/headless-shell image is a headless browser runtime built for chromedp. The Go package is the control layer; Chrome or headless-shell is still needed to render the page. The chromedp documentation describes the headless-shell image as a simple path for a headless environment.

One straightforward pattern is to build the Go binary on your host and run it in the browser image. From the module directory:

Rank #3
CanaKit Raspberry Pi 5 Essentials Starter Kit (4GB RAM)
  • CanaKit Raspberry Pi 5 Essentials Starter Kit
GOOS=linux GOARCH=amd64 go build -o htmlshot .
docker run --rm --init --shm-size 2G 
  -v "$PWD:/work" -w /work 
  docker.io/chromedp/headless-shell:stable 
  ./htmlshot https://example.com

The output is page.png in the mounted working directory. The --shm-size 2G setting is a conditional troubleshooting measure: the image README recommends trying a larger shared-memory allocation such as this if the container crashes with BUS_ADRERR. It is not a documented universal requirement. --init follows the image README’s advice to help reap zombie processes.

Choose a browser image tag

The image README documents floating stable, beta, and dev channel tags, along with specific version tags. Channel images are pushed daily, so a floating channel can change as updates arrive. A specific version tag makes the browser version explicit and is generally the better choice when reproducibility matters. Check the project’s current tags before pinning or updating an image; versions and tag availability can change.

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

Browser lifecycle and separate Chrome processes

By default, chromedp launches a browser for the task and owns that browser’s lifecycle. Keep the context alive for navigation and capture; if the browser connection is lost, chromedp cancels the context. On Linux, chromedp force-kills Chrome child processes it started when the Go program exits.

Rank #4
SANOOV Raspberry Pi 5 4GB Kit, 4GB RAM Single Board Computer with Active Cooler and ABS Case, Complete Raspberry Pi 5 Starter Kit for IoT Robotics Retro Gaming
  • All-in-One Complete Kit: This SANOOV RPi 5 bundle comes with Raspberry Pi 5 4GB RAM single board, active cooler, durable ABS case and screwdriver. No extra parts needed, ready to use right out of the box for beginners and hobbyists
  • Powerful Single Board Computer: Equipped with 4GB RAM and high-performance processor, delivers fast running speed for 4K playback, AI projects, programming and daily computing tasks. SANOOV for raspberry pi 5 4GB is equipped with broadcom 64 quad-core Arm Cortex A76 processor with gigabit ethernet and upgraded with IEEE 802.11ac Wi-Fi, Bluetooth 5.0 dual-band 2.4Ghz and 5Ghz and Power Over Ethernet (POE). Upgrading delivers 2-3 x speed vs Pi 4, redefining the experience
  • Efficient Active Cooler: Effectively lowers operating temperature and prevents performance throttling. Runs quietly even under long-time heavy load, ensures stable operation all day long. SANOOV RPi 5 4GB kit offer an active cooler, which combines an aluminium heatsink with a high-performance PWM fan. Active cooler is fully compatible with the Pi OS, which can effectively reduce the temperature of RPi5 and ensure its good performance during long-term high load operation
  • Sturdy ABS Protective Case: Well-fitted for Raspberry Pi 5 board, can be secured with 4 screws to effectively protect the Pi 5 motherboard from damage, reserves full access to all ports and buttons. SANOOV uses ABS material to produce the case, which has a softer texture and feel. Meanwhile, SANOOV case adopts a layered design for easy disassembly and installation. (Tip: The Case cannot install M.2 HAT Add on Board and Solid State Drive!)
  • Wide Application & Full Compatibility: Seamlessly compatible with official OS and mainstream peripheral accessories for Raspberry Pi 5. Whether you are a beginner, student, electronics hobbyist or professional developer, this all-in-one kit meets your diverse needs. It excels in IoT projects, robotics design, retro gaming devices, home media servers and other DIY creations. Backed by a large global community, you can easily find guides, technical support and shared projects online

If Chrome is managed separately as a long-running process, chromedp also provides RemoteAllocator for connecting to it. In that arrangement, make the remote debugging endpoint reachable by the Go process that needs it, but do not expose it broadly without appropriate controls. The Go process and browser then have distinct lifecycles, so a browser restart or unreachable endpoint can interrupt captures.

Troubleshoot common failures

  • The container exits with BUS_ADRERR: try increasing shared memory, for example with --shm-size 2G. This is the image project’s stated remedy for that crash.
  • Chrome leaves zombie processes: run Docker with --init, as recommended by the headless-shell README, especially when using the image directly or as a base image.
  • chromedp.Run returns an error: report the error and check whether navigation completed and the browser connection remained available. A closed or killed browser can cancel the chromedp context.
  • The image is not found or the browser version changed: verify that the tag exists. Floating channel tags follow updates; use a specific available version tag when you need an explicit browser version.
  • The screenshot omits content or captures the wrong area: confirm whether the code uses element, viewport, or full-page capture. A selector action needs a visible matching node; a viewport action only captures the current view.
  • Device emulation appears different in a full-page shot: FullScreenshot overrides device emulation settings. Do not assume those settings remain in effect for that capture; reset device settings where needed, as noted in the screenshot example.
  • The file is missing despite successful capture: check the working directory and the mounted volume, then handle and inspect the error from os.WriteFile.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Performance, reliability, and cost considerations

Each capture requires a running browser runtime and a page navigation, so container startup, browser availability, and page load behavior are part of the implementation—not just the screenshot call. A browser launched for a short-lived program keeps lifecycle ownership simple; a separately managed browser can be reused, but adds a remote connection and its availability requirements.

The upstream examples document the capture flow, but do not establish a benchmark, a guaranteed capture time, or a specific resource requirement for every page. Heavy pages and container constraints can affect runtime and reliability. Start with the simplest launch model, check errors at navigation and file-writing boundaries, and adjust shared memory only when symptoms such as BUS_ADRERR warrant it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
RasTech Raspberry Pi 5 8GB Kit with Active Cooler and Pi5 Case
  • 【What you Get】You will get 1*Pi 5 8GB Single Board,1*RasTech Case,1*Active Cooler,1*Screwdriver,1*Installation instructions,12-month free warranty, lifetime service, 24-hour prompt and friendly response.
  • 【More Connectors】There are two USB 3.0 ports(5Gbps simultaneously) and two USB 2.0 ports, which triple total bandwidth ,support any combination of up to two cameras or displays. Peak SD card performance is doubled through support for the SDR104 high-speed mode. It provides a smooth desktop experience for you. Offer Gigabit Ethernet and a PCIe interface, along with dual-band Wi-Fi and Bluetooth 5.0/BLE wireless capability. The RasTech Pi 5 Kit use the new 27W 5.1V 5A USB-C power connector.
  • 【 Support Dual 4Kp60 Display 】Each of the two microHDMI sockets can control a 4K display at 60 Hertz, now support HDR, offering super HD video for media streaming projects. RPi 5 is the first RPi model that comes with a PCI Express port (PCIe 2.0 x1 with 500 MB/s) to attach SSDs (requires separate M.2 HAT).
  • 【 Excellent Chips And Applications】Pi 5 is a full-size Pi computer using silicon built in-house at Pi. The RP1 “southbridge” provides the bulk of the I/O capabilities for Pi 5. Pi 5 is more friendly and convenient in the development of Internet of Things, Web development, machine identification, automatic control and other electronic equipment applications and network.
  • 【 Faster CPU, Better GPU 】 Pi 5 features a Broadcom BCM2712 64-bit quad-core Arm Cortex-A76 processor running at 2.4GHz, it delivers a 2–3× increase in CPU performance relative to RaspberryPi 4. The 800MHz VideoCore VII GPU is compatible to OpenGL ES 3.1 and Vulkan 1.2, substantial uplift in graphics performance. Pi 5 Offers lightning-fast CPU speed, a PCI Express interface, a Real Time Clock (RTC) and a power button and runs significantly cooler than Pi 4.

Or skip the browser setup

ScreenshotNeo offers a one-request screenshot API and an MCP server. Its clean-shot flow accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; those steps can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and responses identify the page verdict and billing status in headers. AI agents can use its MCP tools, including take_screenshot, get_page_info, and capture_pdf.

For a PNG, JPEG, WebP, or PDF response, make a GET request to the API. The example saves the returned bytes as WebP; see the ScreenshotNeo API documentation for available parameters and formats.

curl -G "https://api.screenshotneo.com/v1/shot" 
  -d access_key=YOUR_API_KEY 
  --data-urlencode url=https://example.com 
  -o shot.webp

ScreenshotNeo’s Free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots. See ScreenshotNeo for product details, or sign up free to start with 1,000 screenshots a month and no card.

Frequently Asked Questions

Does chromedp.Navigate capture the entire HTML document by itself?

No. Navigation loads the page; a separate screenshot action captures an element, the viewport, or the full page.

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

Can I use chromedp without Chrome in the container?

No. chromedp controls Chrome through the DevTools Protocol; the browser runtime must be available, either launched for the task or managed separately.

Quick Recap

Bestseller No. 1
CanaKit Raspberry Pi 5 Starter Kit PRO - Turbine Black (128GB Edition) (8GB RAM)
CanaKit Raspberry Pi 5 Starter Kit PRO - Turbine Black (128GB Edition) (8GB RAM)
Includes Raspberry Pi 5 with 2.4Ghz 64-bit quad-core CPU (8GB RAM); CanaKit Turbine Black Case for the Raspberry Pi 5
$259.95
Bestseller No. 2
CanaKit Raspberry Pi 5 16GB Starter Kit PRO - Turbine Black (128GB Edition) (16GB RAM)
CanaKit Raspberry Pi 5 16GB Starter Kit PRO - Turbine Black (128GB Edition) (16GB RAM)
Includes Raspberry Pi 5 16GB with 2.4Ghz 64-bit quad-core CPU (16GB RAM); CanaKit Turbine Black Case for the Raspberry Pi 5
$419.99
Bestseller No. 3
CanaKit Raspberry Pi 5 Essentials Starter Kit (4GB RAM)
CanaKit Raspberry Pi 5 Essentials Starter Kit (4GB RAM)
CanaKit Raspberry Pi 5 Essentials Starter Kit
$189.99

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.