Skip to content

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

The console
The page at the end of the recording: Read the console
Read the console. A recording of tutorials/console/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 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 {
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
}
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)
	}
}
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
log: the page loaded
info: the button was clicked
warning: the disk is 91% full
exception: Uncaught Error: the report is missing
log: done

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.