Skip to content

Instantly share code, notes, and snippets.

@diyism
Last active May 29, 2023 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diyism/149d828e46b82257b7d7d5104e04edd5 to your computer and use it in GitHub Desktop.
Save diyism/149d828e46b82257b7d7d5104e04edd5 to your computer and use it in GitHub Desktop.
playwright-go
# how to work in kali/debian/ubuntu22.04(jammy), ref: https://gist.github.com/diyism/4b892a4339f35561dd2ec48158a8c75d
# the playwright in playwright-go currently only supports ubuntu 20.04(focal)
$ sudo singularity build -s ubuntu_focal/ docker://ubuntu:focal
$ sudo singularity shell --writable ubuntu_focal/
$ pwd // /root
$ mkdir WorkSpace_sing_ubuntu_focal
$ cd WorkSpace_sing_ubuntu_focal
$ apt update
$ apt install wget nano
$ wget -qO- https://golang.org/dl/go1.19.3.linux-amd64.tar.gz | tar -xvz -C /usr/local
# add "go compiler path"(GOROOT/bin) and "go lib path"(GOPATH/bin) ":/usr/local/go/bin:/root/go/bin" into PATH
$ nano /etc/environment
$ export PATH=$PATH:/usr/local/go/bin:/root/go/bin
# version "main" is later than "latest", "main" is the developing, "latest" means latest release
# the "main" version will install browser driver "1.25.2" or else "1.20.0-beta-1647057403000"
$ go get github.com/playwright-community/playwright-go@main
$ go install github.com/playwright-community/playwright-go/cmd/playwright@main
$ playwright install --with-deps chromium
#to select chromium, which support CDPsession
#may show error in debian: E: Package 'ttf-unifont' has no installation candidate Failed to install browsers
# in debian, try:
$ sudo bash -c 'echo "deb http://ftp.us.debian.org/debian buster main non-free" >> /etc/apt/sources.list.d/fonts.list'
$ nano test.go
package main
import (
"fmt"
"log"
"github.com/playwright-community/playwright-go@main"
)
func main() {
//https://pkg.go.dev/github.com/mxschmitt/playwright-go
var tout float64=10000
pw,err:= playwright.Run()
browser, err := pw.Chromium.Launch()
contextOptions := playwright.BrowserNewContextOptions{UserAgent: playwright.String("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36"),
RecordVideo: &playwright.BrowserNewContextOptionsRecordVideo{Dir: playwright.String("videos/")},
}
context, _ := browser.NewContext(contextOptions)
page, err := context.NewPage()
//wget http://www.site-digger.com/uploads/stealth.min.js
err = page.AddInitScript(playwright.PageAddInitScriptOptions{Path: playwright.String("/root/WorkSpace_sing_ubuntu_focal/stealth.min.js")});
//_, err = page.Goto("https://amiunique.org/fp") //browser fingerprint
_, err = page.Goto("https://fingerprintjs.github.io/BotD/main/") //bot(headless browser) detector
//entries, err := page.QuerySelectorAll(".athing")
//entry, err := page.WaitForSelector("#summaryRes > span", playwright.PageWaitForSelectorOptions{State: playwright.WaitForSelectorStateAttached, Timeout: &tout})
//entry, err :=
_,err=page.WaitForSelector("#result-text", playwright.PageWaitForSelectorOptions{State: playwright.WaitForSelectorStateAttached, Timeout: &tout})
entry, err := page.QuerySelector("#detectors")
//entry, err := page.QuerySelector("#collected-data")
title, err := entry.TextContent() //.InnerText()
//title, err := page.Content()
fmt.Printf("%s\n", title)
err = browser.Close()
err = pw.Stop()
if err != nil {
log.Fatalf("could not stop Playwright: %v", err)
}
}
$ go mod init test
$ go get github.com/playwright-community/playwright-go@v0.2000.2-0.20221022152247-2586b3829688
$ go mod tidy
$ go run ./
$ mpv --speed=0.2 videos/*.webm
# playwright-go@v0.2000.2-0.20221022152247-2586b3829688 能在 debian test或kali 里运行,
# 旧 playwright-go v0.2000.1 只能在ubuntu 20.04(focal) 运行
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment