Playwright を golang から使う

  • playwright を Go で使える playwright-go を動かしたメモ
  • build したバイナリには Chromium などのエンジンは含まれず、バイナリ実行時にダウンロードが発生する
    • なのでクロスコンパイルして動かしても、バイナリを動かす環境にブラウザエンジンを動かす共有ライブラリが無い場合、ブラウザの起動に失敗してエラーとなる


# directory setup
mkdir my-playwright-go && cd my-playwright-go

# go modules setup
go mod init my-playwright-go
go get

# コード書くファイル作成
touch main.go


  • にアクセスして各記事のタイトルを出力するコード
  • 公式サンプルがいくつかあるので、参考にすると良いかも

    package main
    import (
    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(""); err != nil {
    		log.Fatalf("could not goto: %v", err)
    	entries, err := page.QuerySelectorAll(".athing")
    	if err != nil {
    		log.Fatalf("could not get entries: %v", err)
    	for i, entry := range entries {
    		titleElement, err := entry.QuerySelector("td.title > a")
    		if err != nil {
    			log.Fatalf("could not get title element: %v", err)
    		title, err := titleElement.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)


go run main.go


% go run main.go
2021/01/12 13:12:19 Downloading driver to /Users/myusername/Library/Caches/ms-playwright-go/0.171.0
2021/01/12 13:12:34 Downloaded driver successfully
2021/01/12 13:12:34 Downloading browsers...
2021/01/12 13:12:36 Downloaded browsers successfully
1: Theseus: A modern experimental OS written from scratch in Rust
2: Ubiquiti Networks Breach
3: Moderna is announcing three new vaccine programs. Seasonal flu, HIV, and Nipah
4: My ISP Is Killing My Idle SSH Sessions. Yours Might Be Too
5: Two Worlds: So Much Prosperity, So Much Skepticism
6: Simple Anomaly Detection Using Plain SQL
7: Steam's login method is kinda interesting
8: Europe Is Guaranteeing Citizens the Right to Repair
9: What Is a Gamma Squeeze in the Context of Stock Trading?
10: My personal wishlist for a decentralized social network
11: Show HN: Symmetrical Drawing Tool for Doodling
12: Sirum (YC W15 Nonprofit) hiring engineers to improve healthcare for families
13: A bit on scaling's database
14: Intruder at the top of the 20 meter amateur band?
15: Superintelligence cannot be contained: Lessons from Computability Theory
16: Stealing Your Private YouTube Videos, One Frame at a Time
17: Sinovac's vaccine efficacy less than 60% in Brazil trial -report
18: Getting Ready for Launch
19: Are Experts Real?
20: Google Sued by YouTube Rival Rumble over Search Rankings
21: OpenSocial Specification
22: Is the Schrödinger Equation True?
23: A 432-year-old manual on social distancing
24: iPhone 7 with dead NAND netbooting unmodified Ubuntu 20.04 via USB ethernet
25: We need a new media system
26: XTerm: It's Better Than You Thought
27: Invisible Internet Project (I2P)
28: A Brief History of Consumer Culture
29: How to Hear the ISS
30: Show HN: – Just paste a blog post link and get a thumbnail


% sw_vers
ProductName:    Mac OS X
ProductVersion: 10.15.7
BuildVersion:   19H114
% go version
go version go1.15.6 darwin/amd64


module playwright-go

go 1.15

require ( v0.171.1 // indirect

go.sum v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ= v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk= v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= v1.1.0/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= v0.171.1 h1:0XEOVQw8Zse+Zue+d7v8O+IfQnFIilWcK/uOGSo0U9Q= v0.171.1/go.mod h1:a3SD3v+56XMA0sDDxXJXy+QGnCfXrNZ/+4gwR5ioSgU= v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= v2.5.1 h1:7odma5RETjNHWJnR32wx8t+Io4djHE1PqxCFx3iiZ2w= v2.5.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=