When Playwright is run with the default settings, a separate browser for use with Playwright (e.g., Chromium) is installed and the process is executed using that browser.
If you want to run Playwright from Chrome already installed on your local machine, you can skip the installation and refer to Chrome on your local machine to run the process.
The following is sample code to achieve this.
(Most of the code below is based on the sample in the README of Playwright for Go.)
package main
import (
"fmt"
"log"
"github.com/playwright-community/playwright-go"
)
func main() {
runOption := &playwright.RunOptions{
SkipInstallBrowsers: true,
}
// Perform the installation of Playwright's Driver here.
// Note that this process can be skipped the second time or later because the Driver is already installed.
err := playwright.Install(runOption)
if err != nil {
log.Fatalf("could not install playwright dependencies: %v", err)
}
pw, err := playwright.Run()
if err != nil {
log.Fatalf("could not start playwright: %v", err)
}
option := playwright.BrowserTypeLaunchOptions{
Channel: playwright.String("chrome"),
}
browser, err := pw.Chromium.Launch(option)
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.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)
}
}
Top comments (0)