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