Fill in a form
This program fills in a sign-up form, submits it, and prints the answer that the page shows.
Sign up

tutorials/forms/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
- line 14
if err := chromedp.Do(ctx, chromedp.Navigate(url)); err != nil { - line 19
if err := chromedp.Do(ctx, chromedp.SendKeys(chromedp.CSS("#name"), "Ada Lovelace")); err != nil { - line 22
if err := chromedp.Do(ctx, chromedp.SendKeys(chromedp.CSS("#email"), "ada@example.com")); err != nil { - line 28
if err := chromedp.Do(ctx, chromedp.SetValue(chromedp.CSS("#plan"), "pro")); err != nil { - line 33
if err := chromedp.Do(ctx, chromedp.Click(chromedp.CSS("#terms"))); err != nil { - line 40
if err := chromedp.Do(ctx, chromedp.Click(chromedp.CSS(`button[type="submit"]`))); err != nil { - line 46
answer, err := chromedp.Run(ctx, chromedp.Text(chromedp.CSS("#answer"), chromedp.NodeVisible))
The program
Section titled “The program”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
}// 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)
}
}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:
answer: Welcome, Ada Lovelace! The pro plan is ready for ada@example.com.How it works
Section titled “How it works”chromedp.SendKeystypes 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.SetValuesets the value of a field at once, with no keys. For aselect, the value is thevalueof one of its options.chromedp.Clickchecks the checkbox, and then clicks the submit button.chromedp.Textwithchromedp.NodeVisiblewaits until the page shows its answer, and reads it.