# chromedp > Drive Chrome from Go over the Chrome DevTools Protocol. Tutorials with runnable programs and recordings of each step. The page: https://chromedp.dev/ chromedp is a Go package. It starts Chrome, or connects to a Chrome that runs already, and drives it: it loads pages, clicks, types, reads the page, takes screenshots, prints PDFs, and watches or changes the network. Programs use it to test web sites, to scrape them, and to make images and documents. Play the recording below. It shows a program while it drives Chrome: the line of Go that runs, the page at that moment, and the messages of the protocol that the line sends. Recording: A first program loads a page, reads it and clicks a button. It shows `tutorials/first-program/run.go` while it drives Chrome/158.0.8127.88. Its frames and protocol messages are in /recordings/first-program/timeline.json. The steps: 1. Line 15: `if err := chromedp.Do(ctx, chromedp.Navigate(url)); err != nil {` 2. Line 20: `title, err := chromedp.Run(ctx, chromedp.Title())` 3. Line 28: `heading, err := chromedp.Run(ctx, chromedp.Text(chromedp.CSS("h1")))` 4. Line 35: `if err := chromedp.Do(ctx, chromedp.Click(chromedp.CSS("#greet"))); err != nil {` 5. Line 41: `greeting, err := chromedp.Run(ctx, chromedp.Text(chromedp.CSS("#greeting"), chromedp.NodeVisible))` ## Learn chromedp 1. [Install](/start/install/) Go, Chrome and chromedp. 2. Write [your first program](/start/first-program/). 3. Read the [concepts](/concepts/contexts/): contexts, actions and selectors. 4. Follow a tutorial: [fill in a form](/tutorials/forms/), [take screenshots and PDFs](/tutorials/screenshots/), [read the console](/tutorials/console/) or [intercept requests](/tutorials/intercept/). Every tutorial is a complete program in the [repository of this site](https://github.com/chromedp/chromedp-site/tree/main/tutorials). A test runs each one in Chrome and checks its output. ## For coding agents [/llms.txt](/llms.txt) lists every page as Markdown, and [/llms-full.txt](/llms-full.txt) holds the whole site in one file. Add `.md` to the address of a page to get its Markdown, such as [/start/install.md](/start/install.md). See [For coding agents](/project/agents/). --- # Install > Install Go, Chrome and the chromedp module. The page: https://chromedp.dev/start/install/ You need three things: Go, a browser that speaks the Chrome DevTools Protocol, and the chromedp module. ## Go chromedp needs Go 1.25 or later. Get Go from [go.dev/dl](https://go.dev/dl/), then check the version: ```sh go version ``` ## A browser chromedp starts Chrome or Chromium on the computer that runs the program. It looks for the browser in these places, in order: - On Linux and the other Unix systems: `headless-shell`, `chromium`, `google-chrome` and the other names of Chrome on the `PATH`. - On macOS: Chromium, then Google Chrome, in `/Applications`. - On Windows: Chrome on the `PATH` and in its usual folders, then Microsoft Edge, which is built on Chromium. To use another browser, give its path with the option `chromedp.ExecPath`. See [Contexts and the browser](/concepts/contexts/). A server with no Chrome can run the container image [chromedp/headless-shell](https://hub.docker.com/r/chromedp/headless-shell), which holds a small build of Chrome. See [Connect to a browser](/guides/remote-browser/). ## The module Make a module for your program, and add chromedp to it: ```sh mkdir hello && cd hello go mod init example.com/hello go get github.com/chromedp/chromedp@latest ``` The packages of the protocol, such as `page` and `network`, are in the module `github.com/chromedp/cdproto`. `go get` adds it with chromedp. Next, write [your first program](/start/first-program/). --- # Your first program > Start Chrome, load a page, read it, click a button and read the result. The page: https://chromedp.dev/start/first-program/ This program starts Chrome, loads a page, reads the title and the heading of the page, clicks a button, and prints the greeting that the click shows. Recording: Your first program. It shows `tutorials/first-program/run.go` while it drives Chrome/158.0.8127.88. Its frames and protocol messages are in /recordings/first-program/timeline.json. The steps: 1. Line 15: `if err := chromedp.Do(ctx, chromedp.Navigate(url)); err != nil {` 2. Line 20: `title, err := chromedp.Run(ctx, chromedp.Title())` 3. Line 28: `heading, err := chromedp.Run(ctx, chromedp.Text(chromedp.CSS("h1")))` 4. Line 35: `if err := chromedp.Do(ctx, chromedp.Click(chromedp.CSS("#greet"))); err != nil {` 5. Line 41: `greeting, err := chromedp.Run(ctx, chromedp.Text(chromedp.CSS("#greeting"), chromedp.NodeVisible))` ## The program The program has three Go files and a page. `run.go` holds the work. `main.go` starts a web server for the page and the browser, then calls `run`. `page.go` serves `page.html`, so that the program needs no internet. ````go title="run.go" package main import ( "context" "fmt" "io" "github.com/chromedp/chromedp" ) // run loads the page at url, reads its title and its heading, clicks the // button, and prints the greeting that the click shows. func run(ctx context.Context, w io.Writer, url string) error { // Navigate loads the page and waits until it has loaded. if err := chromedp.Do(ctx, chromedp.Navigate(url)); err != nil { return fmt.Errorf("loading the page: %w", err) } // Run returns the value of an action. Title returns the title of the page. title, err := chromedp.Run(ctx, chromedp.Title()) if err != nil { return fmt.Errorf("reading the title: %w", err) } fmt.Fprintf(w, "title: %s\n", title) // Text waits for the element that the selector finds, and returns its // text. heading, err := chromedp.Run(ctx, chromedp.Text(chromedp.CSS("h1"))) if err != nil { return fmt.Errorf("reading the heading: %w", err) } fmt.Fprintf(w, "heading: %s\n", heading) // Click clicks the element like a person does, with the mouse. if err := chromedp.Do(ctx, chromedp.Click(chromedp.CSS("#greet"))); err != nil { return fmt.Errorf("clicking the button: %w", err) } // The click shows the greeting. NodeVisible makes Text wait until the // element is visible. greeting, err := chromedp.Run(ctx, chromedp.Text(chromedp.CSS("#greeting"), chromedp.NodeVisible)) if err != nil { return fmt.Errorf("reading the greeting: %w", err) } fmt.Fprintf(w, "greeting: %s\n", greeting) return nil } ```` ````go title="main.go" // Command first-program is the program of the tutorial "Your first program" // on https://chromedp.dev. It starts Chrome, loads a local page, reads its // title and its heading, clicks a button, and prints what the page shows. // // go run . package main import ( "context" "log" "net/http/httptest" "os" "time" "github.com/chromedp/chromedp" ) func main() { // Serve the page of the tutorial on this computer, so that the program // needs no internet. srv := httptest.NewServer(handler()) defer srv.Close() // The context holds the browser. The first call on it starts Chrome, and // cancel stops Chrome. ctx, cancel := chromedp.NewContext(context.Background()) defer cancel() // A timeout keeps the program from waiting for ever. ctx, cancel = context.WithTimeout(ctx, 30*time.Second) defer cancel() if err := run(ctx, os.Stdout, srv.URL); err != nil { log.Fatal(err) } } ```` ````go title="page.go" package main import ( _ "embed" "net/http" ) //go:embed page.html var page []byte // handler serves the page of the tutorial. func handler() http.Handler { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write(page) }) return mux } ```` The page of the program is [page.html](https://github.com/chromedp/chromedp-site/blob/main/tutorials/first-program/page.html). In a folder with the four files, `go run .` prints: ````text title="Output" title: The first page heading: Hello from chromedp greeting: Hello, Gopher! The button was clicked. ```` ## How it works `chromedp.NewContext` makes a context that holds the browser. It does not start Chrome yet. The first call on the context starts it, and `cancel` stops it. Always call `cancel`, with `defer`, so that Chrome stops when the program ends. Each step is an action: - `chromedp.Navigate` loads the page and waits until it has loaded. - `chromedp.Title` returns the title of the page. - `chromedp.Text` waits for the element that a selector finds, and returns its text. - `chromedp.Click` clicks the element with the mouse, as a person does. `chromedp.Run` runs one action and returns its value, such as the text. `chromedp.Do` runs actions that return no value. Both return an error when an action fails. [Actions](/concepts/actions/) says more. `chromedp.CSS("h1")` is a selector: it finds the first element that matches a CSS selector. `chromedp.NodeVisible` makes the query wait until the element is visible, because the greeting is hidden until the click. See [Selectors and waiting](/concepts/selectors/). ## Run it Make a folder with the four files of the tutorial, from the [repository of this site](https://github.com/chromedp/chromedp-site/tree/main/tutorials/first-program), and run it: ```sh go mod init example.com/first-program go get github.com/chromedp/chromedp@latest go run . ``` --- # Contexts and the browser > How a chromedp context holds a browser and a tab, when Chrome starts and stops, and how to set a timeout. The page: https://chromedp.dev/concepts/contexts/ A chromedp program keeps its browser in a `context.Context`. The context says which browser and which tab an action runs on, and when they stop. ## Start and stop the browser ```go ctx, cancel := chromedp.NewContext(context.Background()) defer cancel() ``` `chromedp.NewContext` makes the context. It does not start Chrome. The first call of `chromedp.Run` or `chromedp.Do` on the context starts Chrome and opens a tab. `cancel` closes the tab and stops Chrome. Call it with `defer`, or Chrome can keep running after the program ends. ## Tabs A context that you make from a chromedp context uses the same browser, and its first call opens a new tab: ```go tab, cancelTab := chromedp.NewContext(ctx) defer cancelTab() ``` Actions on one context run in one tab. Two goroutines can drive two tabs at the same time, each with its own context. `cancelTab` closes only the tab. ## Timeouts The first call on a context starts the browser, and the browser lives as long as the context of that call. So a timeout on the context of the first call also stops the browser when it ends. That is right for a short program, such as the tutorials of this site. To limit one action of a longer program, start the browser first, then derive the timeout from the context: ```go // The empty Do starts the browser on ctx, which has no timeout. if err := chromedp.Do(ctx); err != nil { return err } // The timeout limits only this action. tctx, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() text, err := chromedp.Run(tctx, chromedp.Text(chromedp.CSS("#result"))) ``` ## Options of the browser `chromedp.NewExecAllocator` sets how chromedp starts Chrome. Start from `chromedp.DefaultExecAllocatorOptions`, and add or change options: ```go opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.ExecPath("/usr/bin/chromium"), chromedp.WindowSize(1280, 800), chromedp.Flag("lang", "de"), ) allocCtx, cancelAlloc := chromedp.NewExecAllocator(context.Background(), opts...) defer cancelAlloc() ctx, cancel := chromedp.NewContext(allocCtx) defer cancel() ``` Chrome runs headless by default, with no window. To see the window, add the option `chromedp.WithVisibleWindow()` to `chromedp.NewContext`, or set the environment variable `CHROMEDP_VISIBLEWINDOW=1` with no change in the code. ## Log the protocol The option `chromedp.WithDebugf(log.Printf)` of `chromedp.NewContext` prints each message between the program and Chrome. The recordings of this site show the same messages. --- # Actions > Run and Do, actions that return a value, Steps, Evaluate, and calls of the protocol. The page: https://chromedp.dev/concepts/actions/ An action is one step on a tab, such as a click or a read of the page. chromedp runs actions with `chromedp.Run` and `chromedp.Do`. ## Run and Do `chromedp.Run` runs one action and returns its value and an error: ```go title, err := chromedp.Run(ctx, chromedp.Title()) ``` The type of the value comes from the action: `chromedp.Title` returns a `string`, `chromedp.Nodes` returns the nodes that a selector finds, and `chromedp.Screenshot` returns the bytes of an image. `chromedp.Do` runs actions that return no value, in order. It stops at the first error: ```go err := chromedp.Do(ctx, chromedp.Navigate(url), chromedp.Click(chromedp.CSS("#greet")), ) ``` `chromedp.Steps` joins actions into one action. A function that returns `chromedp.Steps(...)` gives a name to a sequence of steps that a program uses more than once. ## Evaluate JavaScript `chromedp.Evaluate` runs JavaScript in the page and decodes its result into the type that you name: ```go n, err := chromedp.Run(ctx, chromedp.Evaluate[int](`document.querySelectorAll("li").length`)) ``` ## Wait `chromedp.Sleep` waits for a time. Prefer an action that waits for the page, such as `chromedp.WaitVisible` or `chromedp.Poll`, because a page does not always take the same time. See [Selectors and waiting](/concepts/selectors/). ## Call the protocol An action of chromedp is a sequence of commands of the Chrome DevTools Protocol. The package `github.com/chromedp/cdproto` has every command, event and type of the protocol. `chromedp.Call` sends one command to the tab and returns its result: ```go import ( "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/page" ) // A command with no parameters takes cdp.Empty{}. layout, err := chromedp.Call(ctx, page.GetLayoutMetrics, cdp.Empty{}) ``` `chromedp.CallBrowser` sends a command to the browser and not to a tab. The tutorial [Intercept requests](/tutorials/intercept/) sends commands of the `Fetch` domain this way. The [documentation of the protocol](https://chromedevtools.github.io/devtools-protocol/) lists the domains. ## Write an action An action is a function. `chromedp.Func` makes an action from a function that returns an error: ```go scrollToEnd := chromedp.Func(func(ctx context.Context, t *chromedp.Target) error { _, err := chromedp.Call(ctx, input.DispatchKeyEvent, input.DispatchKeyEventParams{ Type: input.DispatchKeyEventTypeKeyDown, Key: "End", }) return err }) ``` --- # Selectors and waiting > Find elements with CSS, an id, a search or JavaScript, and wait for the page instead of sleeping. The page: https://chromedp.dev/concepts/selectors/ An action such as `chromedp.Click` or `chromedp.Text` takes a selector. The type of the selector says how chromedp finds the element. ## The selectors | Selector | What it finds | | --- | --- | | `chromedp.CSS("form input")` | the first element that matches a CSS selector | | `chromedp.CSSAll("li")` | every element that matches a CSS selector | | `chromedp.ID("name")` | the element with the id. The `#` is optional | | `chromedp.Search("//button")` | the elements that a search finds: plain text, a CSS selector or an XPath query | | `chromedp.JSPath("document.forms[0]")` | the elements that a JavaScript expression returns | A plain string, such as `chromedp.Click("#greet")`, is a `chromedp.Search`. Use `chromedp.CSS` when you mean CSS: it is faster and it finds what you expect. Use `chromedp.JSPath` only with your own expressions, because chromedp sends the expression to the page as it is. ## A query waits An action with a selector waits until the page has the element. It waits until the context ends, so set a [timeout](/concepts/contexts/#timeouts) on the context. Query options change what the action waits for: | Option | What the action waits for | | --- | --- | | none | the element exists in the page | | `chromedp.NodeVisible` | the element is visible | | `chromedp.AtLeast(3)` | at least three elements match. `AtLeast(0)` returns at once | | `chromedp.FromNode(node)` | the element is inside `node`, such as an iframe | ```go text, err := chromedp.Run(ctx, chromedp.Text(chromedp.CSS("#result"), chromedp.NodeVisible)) ``` The actions `chromedp.WaitVisible`, `chromedp.WaitReady` and `chromedp.WaitNotPresent` wait and do nothing else. ## Wait for a condition `chromedp.Poll` runs a JavaScript expression in the page until it returns a true value, and returns that value: ```go // Wait until the page has loaded its data. ok, err := chromedp.Run(ctx, chromedp.Poll[bool](`document.title === "done"`)) ``` It times out after 30 seconds. The option `chromedp.WithPollingTimeout` changes the time. The tutorial [Intercept requests](/tutorials/intercept/) uses `Poll`. ## Do not sleep `chromedp.Sleep` waits for a fixed time, so it is too short on a slow day and too long on a fast one. Wait for what the page shows instead: an element, a text, or a condition of `Poll`. --- # Fill in a form > Type into fields, choose an option, check a box and submit a form, then read the answer of the page. The page: https://chromedp.dev/tutorials/forms/ This program fills in a sign-up form, submits it, and prints the answer that the page shows. Recording: Fill in a form. It shows `tutorials/forms/run.go` while it drives Chrome/158.0.8127.88. Its frames and protocol messages are in /recordings/forms/timeline.json. The steps: 1. Line 14: `if err := chromedp.Do(ctx, chromedp.Navigate(url)); err != nil {` 2. Line 19: `if err := chromedp.Do(ctx, chromedp.SendKeys(chromedp.CSS("#name"), "Ada Lovelace")); err != nil {` 3. Line 22: `if err := chromedp.Do(ctx, chromedp.SendKeys(chromedp.CSS("#email"), "ada@example.com")); err != nil {` 4. Line 28: `if err := chromedp.Do(ctx, chromedp.SetValue(chromedp.CSS("#plan"), "pro")); err != nil {` 5. Line 33: `if err := chromedp.Do(ctx, chromedp.Click(chromedp.CSS("#terms"))); err != nil {` 6. Line 40: `if err := chromedp.Do(ctx, chromedp.Click(chromedp.CSS(`button[type="submit"]`))); err != nil {` 7. Line 46: `answer, err := chromedp.Run(ctx, chromedp.Text(chromedp.CSS("#answer"), chromedp.NodeVisible))` ## The program ````go title="run.go" package main import ( "context" "fmt" "io" "github.com/chromedp/chromedp" ) // run fills in the form of the page at url, submits it, and prints the // answer that the page shows. func run(ctx context.Context, w io.Writer, url string) error { if err := chromedp.Do(ctx, chromedp.Navigate(url)); err != nil { return fmt.Errorf("loading the page: %w", err) } // SendKeys types into a field, one key at a time, as a person does. if err := chromedp.Do(ctx, chromedp.SendKeys(chromedp.CSS("#name"), "Ada Lovelace")); err != nil { return fmt.Errorf("typing the name: %w", err) } if err := chromedp.Do(ctx, chromedp.SendKeys(chromedp.CSS("#email"), "ada@example.com")); err != nil { return fmt.Errorf("typing the email: %w", err) } // SetValue sets the value of a field at once. For a select, the value is // the value of one of its options. if err := chromedp.Do(ctx, chromedp.SetValue(chromedp.CSS("#plan"), "pro")); err != nil { return fmt.Errorf("choosing the plan: %w", err) } // Click checks a checkbox, as a person does. if err := chromedp.Do(ctx, chromedp.Click(chromedp.CSS("#terms"))); err != nil { return fmt.Errorf("accepting the terms: %w", err) } // Click the submit button, as a person does. The page then runs its // handler of the submit event. Submit calls form.submit() instead, which // sends the form and runs no handler. if err := chromedp.Do(ctx, chromedp.Click(chromedp.CSS(`button[type="submit"]`))); err != nil { return fmt.Errorf("submitting the form: %w", err) } // The page shows its answer when the form is submitted. NodeVisible // makes Text wait until the answer is visible. answer, err := chromedp.Run(ctx, chromedp.Text(chromedp.CSS("#answer"), chromedp.NodeVisible)) if err != nil { return fmt.Errorf("reading the answer: %w", err) } fmt.Fprintf(w, "answer: %s\n", answer) return nil } ```` ````go title="main.go" // Command forms is the program of the tutorial "Fill in a form" on // https://chromedp.dev. It fills in and submits a form on a local page, and prints the answer of the page. // // go run . package main import ( "context" "log" "net/http/httptest" "os" "time" "github.com/chromedp/chromedp" ) func main() { // Serve the page of the tutorial on this computer, so that the program // needs no internet. srv := httptest.NewServer(handler()) defer srv.Close() // The context holds the browser. The first call on it starts Chrome, and // cancel stops Chrome. ctx, cancel := chromedp.NewContext(context.Background()) defer cancel() // A timeout keeps the program from waiting for ever. ctx, cancel = context.WithTimeout(ctx, 30*time.Second) defer cancel() if err := run(ctx, os.Stdout, srv.URL); err != nil { log.Fatal(err) } } ```` ````go title="page.go" package main import ( _ "embed" "net/http" ) //go:embed page.html var page []byte // handler serves the page of the tutorial. func handler() http.Handler { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write(page) }) return mux } ```` The page of the program is [page.html](https://github.com/chromedp/chromedp-site/blob/main/tutorials/forms/page.html). In a folder with the four files, `go run .` prints: ````text title="Output" answer: Welcome, Ada Lovelace! The pro plan is ready for ada@example.com. ```` ## How it works - `chromedp.SendKeys` types into a field one key at a time, as a person does. The page sees each key, so a script that checks the field as the person types runs too. - `chromedp.SetValue` sets the value of a field at once, with no keys. For a `select`, the value is the `value` of one of its options. - `chromedp.Click` checks the checkbox, and then clicks the submit button. - `chromedp.Text` with `chromedp.NodeVisible` waits until the page shows its answer, and reads it. :::caution `chromedp.Submit` calls `form.submit()` in the page. It sends the form, but the browser runs no handler of the `submit` event. This page handles the form in its `submit` handler, so the program clicks the button instead, as a person does. ::: --- # Screenshots and PDFs > Take a screenshot of one element and of the whole page, and print the page to a PDF file. The page: https://chromedp.dev/tutorials/screenshots/ This program takes a screenshot of a card on the page and of the whole page, prints the page to a PDF file, and writes the three files to a folder in the temporary directory of the system. Recording: Screenshots and PDFs. It shows `tutorials/screenshots/run.go` while it drives Chrome/158.0.8127.88. Its frames and protocol messages are in /recordings/screenshots/timeline.json. The steps: 1. Line 26: `if err := chromedp.Do(ctx,` 2. Line 34: `card, err := chromedp.Run(ctx, chromedp.Screenshot(chromedp.CSS("#card")))` 3. Line 46: `full, err := chromedp.Run(ctx, chromedp.FullScreenshot(100))` 4. Line 57: `pdf, err := chromedp.Run(ctx, chromedp.PrintToPDF())` ## The program ````go title="run.go" package main import ( "bytes" "context" "fmt" "image" _ "image/png" "io" "os" "path/filepath" "github.com/chromedp/chromedp" ) // run takes a screenshot of the card of the page at url and of the whole // page, prints the page to a PDF file, and writes the three files to a folder // in the temporary directory. func run(ctx context.Context, w io.Writer, url string) error { dir := filepath.Join(os.TempDir(), "chromedp-screenshots") if err := os.MkdirAll(dir, 0o755); err != nil { return fmt.Errorf("making the folder: %w", err) } // A fixed size of the viewport gives screenshots of a fixed size. if err := chromedp.Do(ctx, chromedp.EmulateViewport(960, 600), chromedp.Navigate(url), ); err != nil { return fmt.Errorf("loading the page: %w", err) } // Screenshot takes a PNG image of one element. card, err := chromedp.Run(ctx, chromedp.Screenshot(chromedp.CSS("#card"))) if err != nil { return fmt.Errorf("taking the screenshot of the card: %w", err) } size, err := save(dir, "card.png", card) if err != nil { return err } fmt.Fprintf(w, "card.png: %d by %d pixels\n", size.X, size.Y) // FullScreenshot takes an image of the whole page, also the part below // the viewport. A quality of 100 gives a PNG image, and less gives JPEG. full, err := chromedp.Run(ctx, chromedp.FullScreenshot(100)) if err != nil { return fmt.Errorf("taking the screenshot of the page: %w", err) } size, err = save(dir, "page.png", full) if err != nil { return err } fmt.Fprintf(w, "page.png: %d pixels wide, and taller than the viewport: %t\n", size.X, size.Y > 600) // PrintToPDF prints the page as the print dialog of Chrome does. pdf, err := chromedp.Run(ctx, chromedp.PrintToPDF()) if err != nil { return fmt.Errorf("printing the page: %w", err) } if _, err := save(dir, "page.pdf", pdf); err != nil { return err } fmt.Fprintf(w, "page.pdf: a PDF file: %t\n", bytes.HasPrefix(pdf, []byte("%PDF-"))) fmt.Fprintf(w, "the files are in %s\n", filepath.Base(dir)) return nil } // save writes buf to the file name in dir. For an image, it returns its size // in pixels. func save(dir, name string, buf []byte) (image.Point, error) { if err := os.WriteFile(filepath.Join(dir, name), buf, 0o644); err != nil { return image.Point{}, fmt.Errorf("writing %s: %w", name, err) } if filepath.Ext(name) != ".png" { return image.Point{}, nil } cfg, _, err := image.DecodeConfig(bytes.NewReader(buf)) if err != nil { return image.Point{}, fmt.Errorf("reading %s: %w", name, err) } return image.Pt(cfg.Width, cfg.Height), nil } ```` ````go title="main.go" // Command screenshots is the program of the tutorial "Screenshots and PDFs" on // https://chromedp.dev. It takes a screenshot of an element and of the page, and prints the page to a PDF file. // // go run . package main import ( "context" "log" "net/http/httptest" "os" "time" "github.com/chromedp/chromedp" ) func main() { // Serve the page of the tutorial on this computer, so that the program // needs no internet. srv := httptest.NewServer(handler()) defer srv.Close() // The context holds the browser. The first call on it starts Chrome, and // cancel stops Chrome. ctx, cancel := chromedp.NewContext(context.Background()) defer cancel() // A timeout keeps the program from waiting for ever. ctx, cancel = context.WithTimeout(ctx, 30*time.Second) defer cancel() if err := run(ctx, os.Stdout, srv.URL); err != nil { log.Fatal(err) } } ```` ````go title="page.go" package main import ( _ "embed" "net/http" ) //go:embed page.html var page []byte // handler serves the page of the tutorial. func handler() http.Handler { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write(page) }) return mux } ```` The page of the program is [page.html](https://github.com/chromedp/chromedp-site/blob/main/tutorials/screenshots/page.html). In a folder with the four files, `go run .` prints: ````text title="Output" card.png: 400 by 240 pixels page.png: 960 pixels wide, and taller than the viewport: true page.pdf: a PDF file: true the files are in chromedp-screenshots ```` ## How it works - `chromedp.EmulateViewport` sets the size of the viewport, so that the screenshots have the same size on every computer. - `chromedp.Screenshot` takes a PNG image of the first element that the selector finds. - `chromedp.FullScreenshot` takes an image of the whole page, also the part below the viewport. A quality of 100 gives PNG, and less gives JPEG. - `chromedp.PrintToPDF` prints the page as the print dialog of Chrome does. With no option, it prints on Letter paper, in portrait. The `PDF` options, such as the size of the paper, change that. See the [documentation of PrintToPDF](https://pkg.go.dev/github.com/chromedp/chromedp#PrintToPDF). The recording does not show the files. The program prints their names and sizes. --- # Read the console > Read the console messages and the uncaught errors of a page, in the order that the page makes them. The page: https://chromedp.dev/tutorials/console/ This program loads a page, clicks a button that logs to the console and then throws an error, and prints each message until the page logs `done`. Recording: Read the console. It shows `tutorials/console/run.go` while it drives Chrome/158.0.8127.88. Its frames and protocol messages are in /recordings/console/timeline.json. The steps: 1. Line 18: `if err := chromedp.Do(ctx, chromedp.Navigate(url)); err != nil {` 2. Line 23: `if err := chromedp.Do(ctx, chromedp.Click(chromedp.CSS("#run"))); err != nil {` ## The program ````go title="run.go" package main import ( "context" "fmt" "io" "github.com/chromedp/chromedp" ) // run loads the page at url, clicks its button, and prints each message of // the console of the page, until the page logs "done". func run(ctx context.Context, w io.Writer, url string) error { // Console subscribes to the console of the page and returns an iterator. // Subscribe before the page loads, so that no message is lost. messages := chromedp.Console(ctx) if err := chromedp.Do(ctx, chromedp.Navigate(url)); err != nil { return fmt.Errorf("loading the page: %w", err) } // The button logs three messages, and then throws an error. if err := chromedp.Do(ctx, chromedp.Click(chromedp.CSS("#run"))); err != nil { return fmt.Errorf("clicking the button: %w", err) } // The loop reads the messages in the order that the page made them. A // break ends the subscription. for m, err := range messages { if err != nil { return fmt.Errorf("reading the console: %w", err) } fmt.Fprintf(w, "%s: %s\n", m.Type, m.Text) if m.Text == "done" { break } } return nil } ```` ````go title="main.go" // Command console is the program of the tutorial "Read the console" on // https://chromedp.dev. It reads the console messages and the errors of a local page while it clicks a button. // // go run . package main import ( "context" "log" "net/http/httptest" "os" "time" "github.com/chromedp/chromedp" ) func main() { // Serve the page of the tutorial on this computer, so that the program // needs no internet. srv := httptest.NewServer(handler()) defer srv.Close() // The context holds the browser. The first call on it starts Chrome, and // cancel stops Chrome. ctx, cancel := chromedp.NewContext(context.Background()) defer cancel() // A timeout keeps the program from waiting for ever. ctx, cancel = context.WithTimeout(ctx, 30*time.Second) defer cancel() if err := run(ctx, os.Stdout, srv.URL); err != nil { log.Fatal(err) } } ```` ````go title="page.go" package main import ( _ "embed" "net/http" ) //go:embed page.html var page []byte // handler serves the page of the tutorial. func handler() http.Handler { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write(page) }) return mux } ```` The page of the program is [page.html](https://github.com/chromedp/chromedp-site/blob/main/tutorials/console/page.html). In a folder with the four files, `go run .` prints: ````text title="Output" log: the page loaded info: the button was clicked warning: the disk is 91% full exception: Uncaught Error: the report is missing log: done ```` ## How it works `chromedp.Console` subscribes to the console of the page and returns an iterator. The subscription starts when `Console` returns, so the program calls it before it loads the page, and no message is lost. The `for` loop reads the messages in the order that the page makes them. A message has a type, such as `log`, `warning` or `exception`, and the text that the console of Chrome shows. A `break` ends the subscription. `chromedp.Events` subscribes to one event of the protocol in the same way, such as `network.ResponseReceived`. `chromedp.Listen` calls a function for each event of every type, in the order that Chrome sends them. See the [documentation of chromedp](https://pkg.go.dev/github.com/chromedp/chromedp#Listen). --- # Intercept requests > Pause the requests of a page with the Fetch domain, block one, and answer another with data of your own. The page: https://chromedp.dev/tutorials/intercept/ This program loads a page while it blocks the tracker script of the page and answers the API of the page with a greeting of its own. The server of the page sends other answers, which the page never sees. Recording: Intercept requests. It shows `tutorials/intercept/run.go` while it drives Chrome/158.0.8127.88. Its frames and protocol messages are in /recordings/intercept/timeline.json. The steps: 1. Line 32: `if _, err := chromedp.Call(ctx, fetch.Enable, fetch.EnableParams{` 2. Line 46: `if err := chromedp.Do(ctx, chromedp.Navigate(url)); err != nil {` 3. Line 74: `_, err = chromedp.Call(ctx, fetch.FailRequest, fetch.FailRequestParams{` 4. Line 79: `_, err = chromedp.Call(ctx, fetch.FulfillRequest, fetch.FulfillRequestParams{` 5. Line 51: `if _, err := chromedp.Run(ctx, chromedp.Poll[bool](`document.title === "done"`)); err != nil {` 6. Line 54: `text, err := chromedp.Run(ctx, chromedp.Text(chromedp.CSS("#out")))` ## The program ````go title="run.go" package main import ( "context" "fmt" "io" "iter" "strings" "github.com/chromedp/cdproto/fetch" "github.com/chromedp/cdproto/network" "github.com/chromedp/chromedp" ) // run loads the page at url while it blocks the tracker of the page and // answers its API with a greeting of its own, and prints what the page shows. func run(ctx context.Context, w io.Writer, url string) error { // An empty Do starts the browser on ctx. The handler below uses a // context that ends before ctx, and the browser must not end with it. if err := chromedp.Do(ctx); err != nil { return fmt.Errorf("starting the browser: %w", err) } // Subscribe to the paused requests before Fetch.enable, so that no // request is lost. handlerCtx, stop := context.WithCancel(ctx) defer stop() paused := chromedp.Events(handlerCtx, fetch.RequestPaused) // The browser pauses each request that matches a pattern, until the // program answers it. The page itself matches no pattern. if _, err := chromedp.Call(ctx, fetch.Enable, fetch.EnableParams{ Patterns: []*fetch.RequestPattern{ {URLPattern: "*/api/*"}, {URLPattern: "*/tracker.js"}, }, }); err != nil { return fmt.Errorf("enabling the Fetch domain: %w", err) } // The handler runs in its own goroutine, because the page waits for its // answers while the navigation below waits for the page. done := make(chan error, 1) go func() { done <- handle(ctx, paused) }() if err := chromedp.Do(ctx, chromedp.Navigate(url)); err != nil { return fmt.Errorf("loading the page: %w", err) } // The script of the page sets the title when it has the greeting. if _, err := chromedp.Run(ctx, chromedp.Poll[bool](`document.title === "done"`)); err != nil { return fmt.Errorf("waiting for the page: %w", err) } text, err := chromedp.Run(ctx, chromedp.Text(chromedp.CSS("#out"))) if err != nil { return fmt.Errorf("reading the page: %w", err) } fmt.Fprintf(w, "the page shows:\n%s\n", text) stop() return <-done } // handle answers each paused request: it blocks the tracker, answers the API // with a greeting, and lets any other request go on. func handle(ctx context.Context, paused iter.Seq2[fetch.EventRequestPaused, error]) error { for ev, err := range paused { if err != nil { // The subscription ends when run calls stop. return nil } switch { case strings.HasSuffix(ev.Request.URL, "/tracker.js"): _, err = chromedp.Call(ctx, fetch.FailRequest, fetch.FailRequestParams{ RequestID: ev.RequestID, ErrorReason: network.ErrorReasonBlockedByClient, }) case strings.Contains(ev.Request.URL, "/api/"): _, err = chromedp.Call(ctx, fetch.FulfillRequest, fetch.FulfillRequestParams{ RequestID: ev.RequestID, ResponseCode: 200, ResponseHeaders: []*fetch.HeaderEntry{{Name: "Content-Type", Value: "application/json"}}, Body: []byte(`{"text":"Hello from chromedp"}`), }) default: _, err = chromedp.Call(ctx, fetch.ContinueRequest, fetch.ContinueRequestParams{RequestID: ev.RequestID}) } if err != nil { return fmt.Errorf("answering %s: %w", ev.Request.URL, err) } } return nil } ```` ````go title="main.go" // Command intercept is the program of the tutorial "Intercept requests" on // https://chromedp.dev. It blocks one request of a page and answers another one with data of its own. // // go run . package main import ( "context" "log" "net/http/httptest" "os" "time" "github.com/chromedp/chromedp" ) func main() { // Serve the page of the tutorial on this computer, so that the program // needs no internet. srv := httptest.NewServer(handler()) defer srv.Close() // The context holds the browser. The first call on it starts Chrome, and // cancel stops Chrome. ctx, cancel := chromedp.NewContext(context.Background()) defer cancel() // A timeout keeps the program from waiting for ever. ctx, cancel = context.WithTimeout(ctx, 30*time.Second) defer cancel() if err := run(ctx, os.Stdout, srv.URL); err != nil { log.Fatal(err) } } ```` ````go title="page.go" package main import ( _ "embed" "net/http" ) //go:embed page.html var page []byte // handler serves the page of the tutorial, its API and its tracker. The // program answers the API and blocks the tracker, so the page shows none of // what the server sends for them. func handler() http.Handler { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write(page) }) mux.HandleFunc("/api/greeting", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Write([]byte(`{"text":"Hello from the server"}`)) }) mux.HandleFunc("/tracker.js", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/javascript") w.Write([]byte(`document.getElementById("tracker").textContent = "The tracker ran.";`)) }) return mux } ```` The page of the program is [page.html](https://github.com/chromedp/chromedp-site/blob/main/tutorials/intercept/page.html). In a folder with the four files, `go run .` prints: ````text title="Output" the page shows: The API says: Hello from chromedp The tracker did not run. ```` ## How it works The `Fetch` domain of the protocol pauses each request that matches a pattern, until the program answers it. The program: 1. Subscribes to the event `fetch.RequestPaused` with `chromedp.Events`, before it turns the domain on, so that no paused request is lost. 2. Turns the domain on with `fetch.Enable`, with a pattern for the API and one for the tracker. The page itself matches no pattern, so it loads as usual. 3. Answers each paused request in a goroutine of its own. The page waits for these answers while the navigation waits for the page, so the answers cannot come from the goroutine that navigates. The handler answers in one of three ways: | Command | What the page gets | | --- | --- | | `fetch.FailRequest` | an error, as if the network blocked the request | | `fetch.FulfillRequest` | the status, the headers and the body that the program gives | | `fetch.ContinueRequest` | the answer of the server, as if nothing paused the request | `chromedp.Poll` waits until the script of the page sets the title to `done`. Then the program stops the handler and prints the page. --- # Connect to a browser > Drive a Chrome that runs already, such as the container image chromedp/headless-shell, over a WebSocket. The page: https://chromedp.dev/guides/remote-browser/ By default, chromedp starts Chrome itself and talks to it over a pipe. It can also connect to a Chrome that runs already, on the same computer or on another one, over the WebSocket of its debugging port. ## Run Chrome in a container The image [chromedp/headless-shell](https://hub.docker.com/r/chromedp/headless-shell) holds a small build of Chrome that listens on port 9222: ```sh docker run -d -p 9222:9222 --rm --name headless-shell docker.io/chromedp/headless-shell ``` Podman takes the same arguments. A large page can need more shared memory than the default of the container. Add `--shm-size 2G` for it. ## Connect The package `github.com/chromedp/chromedp/remote` connects to the browser. Give it the address of the debugging port. It asks the browser for the address of its WebSocket: ```go import "github.com/chromedp/chromedp/remote" allocCtx, cancelAlloc := remote.NewAllocator(context.Background(), "ws://127.0.0.1:9222/") defer cancelAlloc() ctx, cancel := chromedp.NewContext(allocCtx) defer cancel() ``` The rest of the program does not change. `cancel` closes the tab, and the browser keeps running, because the program did not start it. :::caution The debugging port gives full control of the browser to anyone who reaches it. Do not open it to a network that you do not trust. ::: ## The browser must reach the page A browser in a container reaches the network of the container. A page on `localhost` of your computer is not on `localhost` of the container. Serve the page on an address that the container can reach, or run the program in the same container network. --- # Troubleshooting > What to do when the program cannot find Chrome, waits for ever, or leaves Chrome running. The page: https://chromedp.dev/guides/troubleshooting/ ## The program cannot find Chrome chromedp looks for Chrome in the places that [Install](/start/install/#a-browser) lists. If Chrome is somewhere else, give its path with `chromedp.ExecPath`. See [Options of the browser](/concepts/contexts/#options-of-the-browser). ## An action waits for ever A query waits until the page has the element, and it waits until the context ends. Check the selector in the developer tools of Chrome, and set a [timeout](/concepts/contexts/#timeouts) on the context, so that the program stops with an error. To see what the program and the browser say to each other, add `chromedp.WithDebugf(log.Printf)` to `chromedp.NewContext`. ## Chrome keeps running after the program Call the cancel function of `chromedp.NewContext` with `defer`. A program that ends with `log.Fatal` or `os.Exit` skips its deferred calls, so return the error from a function, and call `log.Fatal` only in `main`, after the deferred calls of that function have run. ## The page looks different in the program Chrome runs headless by default, with a viewport of its own size. Set the size with `chromedp.EmulateViewport`, or watch the browser with `chromedp.WithVisibleWindow()` or the environment variable `CHROMEDP_VISIBLEWINDOW=1`. ## A selector finds nothing in an iframe A query looks in the document of the page, and an iframe has a document of its own. Get the node of the iframe first, then query inside it with `chromedp.FromNode`. See [Selectors and waiting](/concepts/selectors/). ## Ask a question Ask in the [discussions of chromedp](https://github.com/chromedp/chromedp/discussions). Give a short program that shows the problem. --- # More examples > The programs of the chromedp/examples repository, each a complete program for one task. The page: https://chromedp.dev/guides/examples/ The repository [chromedp/examples](https://github.com/chromedp/examples) has larger programs, each for one task. Almost all of them run on your computer with no internet. This table comes from the doc comment of each program. The 46 programs of [chromedp/examples](https://github.com/chromedp/examples) v0.24.1. Run one with `go run github.com/chromedp/examples/@latest`. | Program | What it shows | | --- | --- | | [click](https://github.com/chromedp/examples/tree/v0.24.1/click) | Use a selector to click on an element | | [console](https://github.com/chromedp/examples/tree/v0.24.1/console) | Read the console of a page and its uncaught exceptions with chromedp.Console | | [contexts](https://github.com/chromedp/examples/tree/v0.24.1/contexts) | See the JavaScript contexts that a tab already has, with chromedp.WithListener | | [cookie](https://github.com/chromedp/examples/tree/v0.24.1/cookie) | Set an HTTP cookie on requests | | [dialogs](https://github.com/chromedp/examples/tree/v0.24.1/dialogs) | Handle the JavaScript dialogs of a page | | [distrust](https://github.com/chromedp/examples/tree/v0.24.1/distrust) | Check that a browser rejects the certificates of a compromised certificate authority | | [download_file](https://github.com/chromedp/examples/tree/v0.24.1/download_file) | Do headless file downloads | | [download_image](https://github.com/chromedp/examples/tree/v0.24.1/download_image) | Do headless image downloads | | [dragdrop](https://github.com/chromedp/examples/tree/v0.24.1/dragdrop) | Drag and drop with the actions DragAndDrop and DragAndDropXY | | [emulate](https://github.com/chromedp/examples/tree/v0.24.1/emulate) | Emulate a specific device such as an iPhone | | [eval](https://github.com/chromedp/examples/tree/v0.24.1/eval) | Evaluate JavaScript and retrieve the result | | [eventsiter](https://github.com/chromedp/examples/tree/v0.24.1/eventsiter) | Listen to the events of a page with iterators | | [exposefunc](https://github.com/chromedp/examples/tree/v0.24.1/exposefunc) | Call Go functions from a page with chromedp.ExposeFunc | | [extension](https://github.com/chromedp/examples/tree/v0.24.1/extension) | Install a browser extension, uBlock Origin Lite, into the browser that chromedp starts, and how to show that it blocks the ads of a page | | [fast](https://github.com/chromedp/examples/tree/v0.24.1/fast) | Measure the speed of the internet connection and show the result in the terminal | | [forecast](https://github.com/chromedp/examples/tree/v0.24.1/forecast) | Render the weather forecast of a city in the terminal | | [frames](https://github.com/chromedp/examples/tree/v0.24.1/frames) | Reach elements inside an iframe and inside a shadow root | | [geoip](https://github.com/chromedp/examples/tree/v0.24.1/geoip) | Look up the location of an IP address and show its map in the terminal | | [har](https://github.com/chromedp/examples/tree/v0.24.1/har) | Generate a HAR file from the network events of a page | | [headers](https://github.com/chromedp/examples/tree/v0.24.1/headers) | Add extra HTTP headers to browser requests | | [intercept](https://github.com/chromedp/examples/tree/v0.24.1/intercept) | Block, mock and change the requests of a page with the Fetch domain | | [keys](https://github.com/chromedp/examples/tree/v0.24.1/keys) | Send key events to an element | | [latlon](https://github.com/chromedp/examples/tree/v0.24.1/latlon) | Retrieve the latitude and the longitude of a map from the URL of the page with the navigation events of the page | | [logic](https://github.com/chromedp/examples/tree/v0.24.1/logic) | Combine actions and Go code in a function that reads a list from a page | | [multi](https://github.com/chromedp/examples/tree/v0.24.1/multi) | Use headless-shell and a container (Docker, Podman, other) | | [pdf](https://github.com/chromedp/examples/tree/v0.24.1/pdf) | Capture a PDF of a page | | [pdfoptions](https://github.com/chromedp/examples/tree/v0.24.1/pdfoptions) | Print a page to PDF files with different options of chromedp.PrintToPDF | | [pdfstream](https://github.com/chromedp/examples/tree/v0.24.1/pdfstream) | Print a page to a PDF file with a stream | | [popups](https://github.com/chromedp/examples/tree/v0.24.1/popups) | Work with the popups of a page and with several targets | | [proxy](https://github.com/chromedp/examples/tree/v0.24.1/proxy) | Authenticate to a proxy server that requires authentication | | [rawcall](https://github.com/chromedp/examples/tree/v0.24.1/rawcall) | Send protocol commands that chromedp has no action for | | [remote](https://github.com/chromedp/examples/tree/v0.24.1/remote) | Connect to an existing Chrome DevTools instance using a remote WebSocket URL | | [screencast](https://github.com/chromedp/examples/tree/v0.24.1/screencast) | Record the screen of a page as a series of JPEG images | | [screenshot](https://github.com/chromedp/examples/tree/v0.24.1/screenshot) | Take a screenshot of a specific element and of the entire browser viewport | | [selectors](https://github.com/chromedp/examples/tree/v0.24.1/selectors) | Choose the elements of a page with the typed selectors | | [session](https://github.com/chromedp/examples/tree/v0.24.1/session) | Save the state of a session and restore it in another browser | | [structeval](https://github.com/chromedp/examples/tree/v0.24.1/structeval) | Evaluate JavaScript into typed Go values | | [submit](https://github.com/chromedp/examples/tree/v0.24.1/submit) | Fill out and submit a form | | [subtree](https://github.com/chromedp/examples/tree/v0.24.1/subtree) | Populate and travel a subtree of the DOM | | [tabs](https://github.com/chromedp/examples/tree/v0.24.1/tabs) | Use several tabs of one browser | | [termcast](https://github.com/chromedp/examples/tree/v0.24.1/termcast) | Stream the screen of the browser to the terminal with terminal graphics | | [text](https://github.com/chromedp/examples/tree/v0.24.1/text) | Extract text from a specific element | | [timeline](https://github.com/chromedp/examples/tree/v0.24.1/timeline) | See the events of a page of every type in the order in which Chrome sent them | | [upload](https://github.com/chromedp/examples/tree/v0.24.1/upload) | Upload a file on a form | | [visible](https://github.com/chromedp/examples/tree/v0.24.1/visible) | Wait until an element is visible | | [workers](https://github.com/chromedp/examples/tree/v0.24.1/workers) | Run many jobs at the same time in one browser with a pool of goroutines | --- # About chromedp > The repositories of the project, where to ask, the license, and the versions that this site documents. The page: https://chromedp.dev/project/about/ chromedp is a Go package that drives Chrome and other browsers over the [Chrome DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/), with no external dependencies. ## The repositories | Repository | What it holds | | --- | --- | | [chromedp/chromedp](https://github.com/chromedp/chromedp) | the package | | [chromedp/cdproto](https://github.com/chromedp/cdproto) | the commands, events and types of the protocol, as Go code | | [chromedp/examples](https://github.com/chromedp/examples) | larger programs, each for one task | | [chromedp/docker-headless-shell](https://github.com/chromedp/docker-headless-shell) | the container image of a small Chrome | | [chromedp/chromedp-site](https://github.com/chromedp/chromedp-site) | this site | The API reference is on [pkg.go.dev](https://pkg.go.dev/github.com/chromedp/chromedp). ## Ask and report Ask questions and report bugs in the [discussions of chromedp](https://github.com/chromedp/chromedp/discussions). Give a short program that shows the problem. ## The versions of this site The tutorials use the versions of chromedp and cdproto that the file [go.mod](https://github.com/chromedp/chromedp-site/blob/main/go.mod) of this site names. A test runs every tutorial in Chrome with them. ## License chromedp is released under the [MIT license](https://github.com/chromedp/chromedp/blob/main/LICENSE). --- # For coding agents > The Markdown of each page, llms.txt and llms-full.txt, and how to use the tutorials as tested programs. The page: https://chromedp.dev/project/agents/ The site gives every page as Markdown, for coding agents and other programs that read it. | Address | What it holds | | --- | --- | | [/llms.txt](/llms.txt) | a list of every page, with a link to its Markdown | | [/llms-full.txt](/llms-full.txt) | every page as Markdown, in one file | | the address of a page with `.md`, such as [/start/install.md](/start/install.md) | the Markdown of that page | Each page links to its Markdown in its head, with ``. ## The tutorials are tested programs The Markdown of a tutorial holds the complete program and its expected output. The program is in the [tutorials](https://github.com/chromedp/chromedp-site/tree/main/tutorials) folder of the repository of the site, and a test runs it in Chrome and compares its output with the expected output. So a tutorial works with the version of chromedp in the [go.mod](https://github.com/chromedp/chromedp-site/blob/main/go.mod) of the site. The Markdown of a recording lists its steps: the line of Go of each step and its code. The file `timeline.json` of the recording holds the protocol messages of each step. ## Rules that help - Call the cancel function of `chromedp.NewContext` with `defer`. - Wait for the page with a query or `chromedp.Poll`, not with `chromedp.Sleep`. - Prefer the actions of chromedp to raw commands of the protocol. - Use `chromedp.CSS` for a CSS selector. A plain string is a search.