Last active
October 28, 2024 18:19
-
-
Save diyism/149d828e46b82257b7d7d5104e04edd5 to your computer and use it in GitHub Desktop.
playwright-go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 install github.com/playwright-community/playwright-go/cmd/[email protected] | |
$ 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 mod edit -require github.com/playwright-community/[email protected] | |
$ go mod tidy | |
$ go run ./ | |
$ mpv --speed=0.2 videos/*.webm | |
# [email protected] 能在 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