Playwright for Go a browser automation library to control Chromium, Firefox and WebKit with a single API.
🎭 Playwright for 
API reference | Example recipes
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 149.0.7827.55 | :whitecheckmark: | :whitecheckmark: | :whitecheckmark: | | WebKit 26.5 | :whitecheckmark: | :whitecheckmark: | :whitecheckmark: | | Firefox 151.0 | :whitecheckmark: | :whitecheckmark: | :whitecheckmark: |
Headless execution is supported for all the browsers on all platforms.
Installation
go get -u github.com/mxschmitt/playwright-go
Install the playwright driver and browsers (with OS dependencies if provide --with-deps). 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/playwright@v0.xxxx.x install --with-deps
Or
go install github.com/mxschmitt/playwright-go/cmd/playwright@v0.xxxx.x
playwright install --with-deps
Alternatively, you can download the driver and browsers in your code. But if your operating system lacks those browser dependencies, you still need to install them manually, because installing system dependencies requires privileges.
err := playwright.Install()
Capabilities
Playwright is built to automate the broad and growing set of web browser capabilities used by Single Page Apps and Progressive Web Apps.
- Scenarios that span multiple pages, domains and iframes
- Auto-wait for elements to be ready before executing actions (like click, fill)
- Intercept network activity for stubbing and mocking network requests
- Emulate mobile devices, geolocation, permissions
- Support for web components via shadow-piercing selectors
- Native input events for mouse and keyboard
- Upload and download files
Example
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) } }
Docker
Refer to the Dockerfile.example to build your own docker image.More examples
- Refer to helper_test.go for End-To-End testing
- Downloading files
- End-To-End testing a website
- Executing JavaScript in the browser
- Emulate mobile and geolocation
- Parallel scraping using a WaitGroup
- Rendering a PDF of a website
- Scraping HackerNews
- Take a screenshot
- Record a video
- Monitor network activity
How does it work?
Playwright is a Node.js library which uses:
- Chrome DevTools Protocol to communicate with Chromium
- Patched Firefox to communicate with Firefox
- Patched WebKit to communicate with WebKit
Is Playwright for Go ready?
We are ready for your feedback, but we are still covering Playwright Go with the tests.