go安裝playwright-go

狂奔的狼發表於2024-07-31

寫go 呼叫Playwright時,遇到 could not start Playwright: please install the driver (v1.45.1) and browsers first: %!w(<nil>) 報錯

解決方式:安裝驅動和瀏覽器依賴。

go run github.com/playwright-community/playwright-go/cmd/playwright install --with-deps

  

測試程式碼:

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)
    }
    defer pw.Stop()

    browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
        Headless: playwright.Bool(true),
    })
    if err != nil {
        log.Fatalf("could not launch browser: %v", err)
    }
    defer browser.Close()

    page, err := browser.NewPage()
    if err != nil {
        log.Fatalf("could not create page: %v", err)
    }

    _, err = page.Goto("https://playwright.dev")
    if err != nil {
        log.Fatalf("could not go to page: %v", err)
    }

    title, err := page.Title()
    if err != nil {
        log.Fatalf("could not get page title: %v", err)
    }
    fmt.Println("Page title:", title)
}

執行成功!!!

相關文章