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