README.md
<!-- GEN:chromium-version-badge --><!-- GEN:stop --> <!-- GEN:firefox-version-badge --><!-- GEN:stop --> <!-- GEN:webkit-version-badge --><!-- GEN:stop -->
Playwright is a Go library to automate Chromium, Firefox and WebKit with a single API. Playwright is built to enable cross-browser web automation that is ever-green, capable, reliable and fast.
| Linux | macOS | Windows | |
|---|---|---|---|
| Chromium <!-- GEN:chromium-version -->151.0.7922.34<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| WebKit <!-- GEN:webkit-version -->26.5<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| Firefox <!-- GEN:firefox-version -->153.0<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
Headless and headed execution is supported for all the browsers on all platforms.
go get -u github.com/mxschmitt/playwright-go
Install the Playwright driver and browsers (add --with-deps to also install the OS dependencies). Note that you should replace the version number 0.xxxx.x with the version used in your current go.mod. Each minor version upgrade requires a specific Playwright driver version.
go run github.com/mxschmitt/playwright-go/cmd/[email protected] install --with-deps
# Or
go install github.com/mxschmitt/playwright-go/cmd/[email protected]
playwright install --with-deps
Alternatively, you can download the driver and browsers from your code. If your operating system lacks the browser dependencies you still need to install them manually, because installing system dependencies requires privileges.
err := playwright.Install()
https://playwright.dev/docs/intro
The guides, concepts and API semantics are shared across all Playwright languages — only the code samples on that site are written in JavaScript.
https://pkg.go.dev/github.com/mxschmitt/playwright-go
The following example crawls the current top voted items from Hacker News.
package main
import (
"fmt"
"log"
"github.com/mxschmitt/playwright-go"
)
func main() {
pw, err := playwright.Run()
if err != nil {
log.Fatalf("could not start playwright: %v", err)
}
browser, err := pw.Chromium.Launch()
if err != nil {
log.Fatalf("could not launch browser: %v", err)
}
page, err := browser.NewPage()
if err != nil {
log.Fatalf("could not create page: %v", err)
}
if _, err = page.Goto("https://news.ycombinator.com"); err != nil {
log.Fatalf("could not goto: %v", err)
}
entries, err := page.Locator(".athing").All()
if err != nil {
log.Fatalf("could not get entries: %v", err)
}
for i, entry := range entries {
title, err := entry.Locator("td.title > span > a").TextContent()
if err != nil {
log.Fatalf("could not get text content: %v", err)
}
fmt.Printf("%d: %s\n", i+1, title)
}
if err = browser.Close(); err != nil {
log.Fatalf("could not close browser: %v", err)
}
if err = pw.Stop(); err != nil {
log.Fatalf("could not stop Playwright: %v", err)
}
}
GetByRole, GetByLabel, GetByPlaceholder, GetByText and GetByTestId instead of brittle CSS paths.Click and Fill wait for the element to be actionable, and web-first assertions created via playwright.NewPlaywrightAssertions() retry until the condition is met. No arbitrary sleeps.BrowserContext is the equivalent of a brand new browser profile at near-zero overhead. Save the authentication state once with context.StorageState() and reuse it everywhere.context.Tracing() and inspect DOM snapshots, network traffic and console logs afterwards with playwright show-trace.page.Route(), or monitor all traffic of a page.Refer to the Dockerfile.example to build your own Docker image.
Playwright is a Node.js library which uses:
These patches are based on the original sources of the browsers and don't modify the browser behaviour, so the browsers are basically the same (see here) as you see them in the wild. The support for different programming languages is based on exposing a RPC server in the Node.js land which can be used to allow other languages to use Playwright without implementing all the custom logic.
The bridge between Node.js and the other languages is basically a Node.js runtime combined with Playwright which gets shipped for each of these languages (around 50MB) and then communicates over stdio to send the relevant commands. This will also download the pre-compiled browsers.
More comfortable in another programming language? Playwright is also available in