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