Playwright for Go 是一个浏览器自动化库,可使用单个 API 控制 Chromium、Firefox 和 WebKit。
Go to file
Can Stand 5afc7b75ea
feat: remove hardcoded subdirectories to RunOptions.DriverDirectory and PLAYWRIGHT_DRIVER_PATH (#501)
BREAKING CHANGE: When specifying the playwright driver directory via `RunOptions.DriverDirectory` or the `PLAYWRIGHT_DRIVER_PATH` environment variable, you need to point directly to the directory of the corresponding version. The previously hard-coded `ms-playwright-go/x.xx.x` subdirectory is now only applies to the default cache directory.
2024-11-12 19:06:31 +08:00
.github chore: roll to Playwright v1.47.1 (#487) 2024-09-17 00:43:47 +08:00
cmd/playwright chore: roll to Playwright v1.43.0 (#449) 2024-04-13 02:08:33 +08:00
docs devops: deploy to GitHub Pages manually (#170) 2021-08-03 16:07:50 +02:00
examples chore: use golangci-lint and gofumpt (#408) 2024-01-24 21:17:04 +08:00
internal/safe chore: require go v1.22 (#489) 2024-09-24 01:02:56 +08:00
patches feat: add WebSocket routing (#503) 2024-11-01 17:51:26 +08:00
playwright@dc80964a3f feat: add WebSocket routing (#503) 2024-11-01 17:51:26 +08:00
scripts chore: use golangci-lint and gofumpt (#408) 2024-01-24 21:17:04 +08:00
tests feat: add WebSocket routing (#503) 2024-11-01 17:51:26 +08:00
_config.yml chore: move under playwright-community GitHub organisation (#248) 2022-01-26 16:29:30 +01:00
.gitattributes chore: use new upstream LF test assets (#243) 2021-11-24 17:45:53 +01:00
.gitignore chore: roll to Playwright v1.45.1 (#472) 2024-07-03 15:23:34 +08:00
.gitmodules chore: point to upstream Playwright repository (#149) 2021-07-29 15:13:25 +02:00
.golangci.yaml chore: use golangci-lint and gofumpt (#408) 2024-01-24 21:17:04 +08:00
.nojekyll devops: deploy to GitHub Pages manually (#170) 2021-08-03 16:07:50 +02:00
404.html devops: deploy to GitHub Pages manually (#170) 2021-08-03 16:07:50 +02:00
apiresponse_assertions.go Automatically generate Go interfaces code, etc. (#367) 2023-08-24 12:44:28 +08:00
artifact.go chore: roll to Playwright v1.44.0 (#454) 2024-06-06 20:13:59 +02:00
assertions.go feat: roll to Playwright v1.41.1 (#407) 2024-01-24 09:07:05 +01:00
binding_call.go chore: simplify bindingCall error stack and refac some tests (#474) 2024-07-10 11:39:13 +08:00
browser_context.go feat: add WebSocket routing (#503) 2024-11-01 17:51:26 +08:00
browser_type.go chore: roll to Playwright v1.48.1 (#500) 2024-10-25 22:34:05 +08:00
browser.go chore: roll to Playwright v1.47.0 (#480) 2024-09-06 10:51:55 +02:00
cdp_session.go feat: added support for CDPSession (#128) 2021-06-07 23:57:06 +02:00
channel_owner.go feat: add WebSocket routing (#503) 2024-11-01 17:51:26 +08:00
channel.go feat: add WebSocket routing (#503) 2024-11-01 17:51:26 +08:00
clock.go chore: roll to Playwright v1.45.1 (#472) 2024-07-03 15:23:34 +08:00
connection.go feat: add WebSocket routing (#503) 2024-11-01 17:51:26 +08:00
console_message.go chore: roll to Playwright v1.39.0 (#381) 2023-10-27 21:52:09 +08:00
CONTRIBUTING.md chore: use golangci-lint and gofumpt (#408) 2024-01-24 21:17:04 +08:00
dialog.go chore: roll to Playwright v1.35.1 (#354) 2023-06-29 17:13:22 +02:00
Dockerfile.example docs: add Dockerfile example (#424) 2024-03-13 20:27:50 +08:00
download.go Automatically generate Go interfaces code, etc. (#367) 2023-08-24 12:44:28 +08:00
element_handle.go fix: Panics caused by BoundingBox API returning nil (#498) 2024-10-17 15:24:05 +08:00
errors.go chore: roll to playwright v1.42.1 (#423) 2024-03-02 13:34:50 +08:00
event_emitter_test.go Refactor some code using the new stdlib and generics, and add several tests (#404) 2024-01-09 19:43:29 +08:00
event_emitter.go chore: roll to Playwright v1.48.1 (#500) 2024-10-25 22:34:05 +08:00
fetch.go chore: roll to Playwright v1.48.1 (#500) 2024-10-25 22:34:05 +08:00
file_chooser.go chore: roll to Playwright v1.40.1 (#394) 2023-12-03 10:00:45 +08:00
frame_locator.go chore: require go v1.22 (#489) 2024-09-24 01:02:56 +08:00
frame.go chore: roll to Playwright v1.47.1 (#487) 2024-09-17 00:43:47 +08:00
generated-enums.go chore: roll to Playwright v1.45.1 (#472) 2024-07-03 15:23:34 +08:00
generated-interfaces.go feat: add WebSocket routing (#503) 2024-11-01 17:51:26 +08:00
generated-structs.go feat: add WebSocket routing (#503) 2024-11-01 17:51:26 +08:00
glob_test.go feat: roll to Playwright v1.41.1 (#407) 2024-01-24 09:07:05 +01:00
glob.go feat: roll to Playwright v1.41.1 (#407) 2024-01-24 09:07:05 +01:00
go.mod chore: roll to Playwright v1.48.1 (#500) 2024-10-25 22:34:05 +08:00
go.sum chore: roll to Playwright v1.48.1 (#500) 2024-10-25 22:34:05 +08:00
har_router.go feat: add WebSocket routing (#503) 2024-11-01 17:51:26 +08:00
helpers_test.go chore: use golangci-lint and gofumpt (#408) 2024-01-24 21:17:04 +08:00
helpers.go chore: roll to Playwright v1.45.1 (#472) 2024-07-03 15:23:34 +08:00
input_files_helper.go chore: roll to Playwright v1.45.1 (#472) 2024-07-03 15:23:34 +08:00
input.go chore: add multiple missing features (#353) 2023-06-26 23:11:57 +02:00
js_handle.go chore: roll to Playwright v1.47.0 (#480) 2024-09-06 10:51:55 +02:00
jsonPipe.go chore: use golangci-lint and gofumpt (#408) 2024-01-24 21:17:04 +08:00
LICENSE Initial commit 2020-08-16 14:46:15 +02:00
local_utils.go feat: add WebSocket routing (#503) 2024-11-01 17:51:26 +08:00
locator_assertions.go chore: roll to Playwright v1.44.0 (#454) 2024-06-06 20:13:59 +02:00
locator_helpers.go Roll to Playwright v1.38.1 (#373) 2023-10-09 16:44:19 +08:00
locator.go chore: roll to Playwright v1.48.1 (#500) 2024-10-25 22:34:05 +08:00
network.go Automatically generate Go interfaces code, etc. (#367) 2023-08-24 12:44:28 +08:00
objectFactory.go feat: add WebSocket routing (#503) 2024-11-01 17:51:26 +08:00
page_assertions.go chore: roll to Playwright v1.44.0 (#454) 2024-06-06 20:13:59 +02:00
page.go feat: add WebSocket routing (#503) 2024-11-01 17:51:26 +08:00
playwright.go chore: roll to Playwright v1.39.0 (#381) 2023-10-27 21:52:09 +08:00
README.md chore: roll to Playwright v1.48.1 (#500) 2024-10-25 22:34:05 +08:00
request.go chore: roll to Playwright v1.45.1 (#472) 2024-07-03 15:23:34 +08:00
response.go chore: use golangci-lint and gofumpt (#408) 2024-01-24 21:17:04 +08:00
route.go feat: add WebSocket routing (#503) 2024-11-01 17:51:26 +08:00
run_test.go fix: use PLAYWRIGHT_NODEJS_PATH in getNodeExecutable (#468) 2024-06-17 19:17:18 +02:00
run_unix.go chore: roll to Playwright v1.40.1 (#394) 2023-12-03 10:00:45 +08:00
run_win.go chore: roll to Playwright v1.40.1 (#394) 2023-12-03 10:00:45 +08:00
run.go feat: remove hardcoded subdirectories to RunOptions.DriverDirectory and PLAYWRIGHT_DRIVER_PATH (#501) 2024-11-12 19:06:31 +08:00
selectors.go feat: add WebSocket routing (#503) 2024-11-01 17:51:26 +08:00
stream.go chore: roll to Playwright v1.44.0 (#454) 2024-06-06 20:13:59 +02:00
tracing.go chore: roll to Playwright v1.48.1 (#500) 2024-10-25 22:34:05 +08:00
transport.go chore: roll to Playwright v1.48.1 (#500) 2024-10-25 22:34:05 +08:00
type_helpers.go feat: StorageState can convert to OptionalStorageState (#418) (close #414) 2024-02-09 22:53:56 +08:00
video.go Refactor some code using the new stdlib and generics, and add several tests (#404) 2024-01-09 19:43:29 +08:00
waiter_test.go Refactor some code using the new stdlib and generics, and add several tests (#404) 2024-01-09 19:43:29 +08:00
waiter.go chore: roll to Playwright v1.40.1 (#394) 2023-12-03 10:00:45 +08:00
web_error.go chore: roll to Playwright v1.40.1 (#394) 2023-12-03 10:00:45 +08:00
websocket_route.go feat: add WebSocket routing (#503) 2024-11-01 17:51:26 +08:00
websocket.go feat: RunOptions can specifying output/error streams (#426) 2024-03-13 19:36:35 +08:00
worker.go Automatically generate Go interfaces code, etc. (#367) 2023-08-24 12:44:28 +08:00
writable_stream.go chore: roll to Playwright v1.40.1 (#394) 2023-12-03 10:00:45 +08:00

🎭 Playwright for

Looking for maintainers and see here. Thanks!

PkgGoDev License Go Report Card Build Status Join Slack Coverage Status Chromium version Firefox version WebKit version

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 130.0.6723.31
WebKit 18.0
Firefox 131.0

Headless execution is supported for all the browsers on all platforms.

Installation

go get -u github.com/playwright-community/playwright-go

Install the browsers and OS dependencies:

go run github.com/playwright-community/playwright-go/cmd/playwright@latest install --with-deps
# Or
go install github.com/playwright-community/playwright-go/cmd/playwright@latest
playwright install --with-deps

Alternatively you can do it inside your program via which downloads the driver and browsers:

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 page, 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/playwright-community/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

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

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.

Is Playwright for Go ready?

We are ready for your feedback, but we are still covering Playwright Go with the tests.

Resources