Skip to content

Your 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.

The first page
The page at the end of the recording: Your first program
Your first program. A recording of tutorials/first-program/run.go in Chrome/158.0.8127.88. Step through it to see the line of Go, the page and the protocol messages of each step.
The steps of the recording
  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 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.

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
}
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)
	}
}
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. In a folder with the four files, go run . prints:

Output
title: The first page
heading: Hello from chromedp
greeting: Hello, Gopher! The button was clicked.

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 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.

Make a folder with the four files of the tutorial, from the repository of this site, and run it:

Terminal window
go mod init example.com/first-program
go get github.com/chromedp/chromedp@latest
go run .