Giter VIP home page Giter VIP logo

playwright-community / playwright-go Goto Github PK

View Code? Open in Web Editor NEW
1.8K 29.0 136.0 31.9 MB

Playwright for Go a browser automation library to control Chromium, Firefox and WebKit with a single API.

Home Page: https://playwright-community.github.io/playwright-go/

License: MIT License

Go 95.63% JavaScript 0.38% HTML 3.83% Shell 0.14% CSS 0.02%
playwright golang selenium browser-automation chromium hacktoberfest go headless headless-chrome firefox

playwright-go's Introduction

๐ŸŽญ Playwright for

Looking for maintainers and see here. Thanks!

PkgGoDev License Go Report Card Build Status Join Slack Coverage Status Chromium version Firefox version WebKit version

API reference | Example recipes

Playwright is a Go library to automate Chromium, Firefox and WebKit with a single API. Playwright is built to enable cross-browser web automation that is ever-green, capable, reliable and fast.

Linux macOS Windows
Chromium 124.0.6367.29 โœ… โœ… โœ…
WebKit 17.4 โœ… โœ… โœ…
Firefox 124.0 โœ… โœ… โœ…

Headless execution is supported for all the browsers on all platforms.

Installation

go get -u github.com/playwright-community/playwright-go

Install the browsers and OS dependencies:

go run github.com/playwright-community/playwright-go/cmd/playwright@latest install --with-deps
# Or
go install github.com/playwright-community/playwright-go/cmd/playwright@latest
playwright install --with-deps

Alternatively you can do it inside your program via which downloads the driver and browsers:

err := playwright.Install()

Capabilities

Playwright is built to automate the broad and growing set of web browser capabilities used by Single Page Apps and Progressive Web Apps.

  • Scenarios that span multiple page, domains and iframes
  • Auto-wait for elements to be ready before executing actions (like click, fill)
  • Intercept network activity for stubbing and mocking network requests
  • Emulate mobile devices, geolocation, permissions
  • Support for web components via shadow-piercing selectors
  • Native input events for mouse and keyboard
  • Upload and download files

Example

The following example crawls the current top voted items from Hacker News.

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)
	}
	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("https://news.ycombinator.com"); err != nil {
		log.Fatalf("could not goto: %v", err)
	}
	entries, err := page.Locator(".athing").All()
	if err != nil {
		log.Fatalf("could not get entries: %v", err)
	}
	for i, entry := range entries {
		title, err := entry.Locator("td.title > span > a").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)
	}
}

Docker

Refer to the Dockerfile.example to build your own docker image.

More examples

How does it work?

Playwright is a Node.js library which uses:

  • Chrome DevTools Protocol to communicate with Chromium
  • Patched Firefox to communicate with Firefox
  • Patched WebKit to communicate with WebKit

These patches are based on the original sources of the browsers and don't modify the browser behaviour so the browsers are basically the same (see here) as you see them in the wild. The support for different programming languages is based on exposing a RPC server in the Node.js land which can be used to allow other languages to use Playwright without implementing all the custom logic:

The bridge between Node.js and the other languages is basically a Node.js runtime combined with Playwright which gets shipped for each of these languages (around 50MB) and then communicates over stdio to send the relevant commands. This will also download the pre-compiled browsers.

Is Playwright for Go ready?

We are ready for your feedback, but we are still covering Playwright Go with the tests.

Resources

playwright-go's People

Contributors

canstand avatar eriner avatar hongcha98 avatar jiong-han avatar kizaski avatar kumaraditya303 avatar lcmtwn avatar masaushi avatar mxschmitt avatar ninadingole avatar noperator avatar pjcalvo avatar sacrond avatar samhennessy avatar shinshin86 avatar stevenjohnstone avatar sttaran avatar zengxs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

playwright-go's Issues

Missing types for page error events.

Hi, I can't see the type definition of the page errors.
For example:

page, _ := browserContext.NewPage()
page.Once("pageerror", func(err ???) {
	c.Logger().Errorf("an error on the page was detected: %s", err)
})

I also recommend using custom function types e.g

type RequestFailedEventHandler = func(request *Request)

Interface vs Struct

Thanks for this great library and your hardwork to make it great.

From https://pkg.go.dev/github.com/mxschmitt/playwright-go#pkg-index, the methods show out if the type is struct(like Playwright):

type Playwright
    func Run(options ...*RunOptions) (*Playwright, error)
    func (c *Playwright) Dispose()
    func (p *Playwright) Stop() error

But No method shows out if the type is defined as interface(like Page), we need to go to the type definition to find the available methods.

type Page

Is it possible to define the Page as struct?

type Page struct {
    // non export fields
}

func (*Page) Click(selector string, options ...PageClickOptions) error
func (*Page) Fill(selector, text string, options ...FrameFillOptions) error
... ...

What do you think about it?
Thanks.

Can't Match  

I'm using the latest version github.com/mxschmitt/playwright-go v0.1100.0 // indirect

HTML code: For options with space:

<select name="cars" id="test1">
  <option value="t1">t1</option>
  <option value="t2">t 2</option>
  <option value="t3">t&nbsp;3</option>
  <option value="t4">t4</option>
</select>

Playwrite-go code:

// success
v,err:=page.SelectOption("#test1",playwright.SelectOptionValues{Labels: playwright.StringSlice("t 2")})
fmt.Println(v,err)

// failed
v,err=page.SelectOption("#test1",playwright.SelectOptionValues{Labels: playwright.StringSlice("t 3")})
fmt.Println(v,err)

Result:

C:\Andrew\prj\learn>go run select.go
2021/05/26 11:50:59 Downloading browsers...
2021/05/26 11:50:59 Downloaded browsers successfully
  pw:api navigating to "http://localhost:8888/", waiting until "load" +0ms
  pw:api   navigated to "http://localhost:8888/" +799ms
  pw:api   "load" event fired +28ms
  pw:api   "domcontentloaded" event fired +4ms
  pw:api waiting for selector "#test1" +12ms
  pw:api   selector resolved to visible <select id="test1" name="cars">โ€ฆ</select> +23ms
  pw:api   selecting specified option(s) +5ms
  pw:api     selected specified option(s) +11ms
[t2] <nil>
  pw:api waiting for selector "#test1" +37ms
  pw:api   selector resolved to visible <select id="test1" name="cars">โ€ฆ</select> +4ms
  pw:api   selecting specified option(s) +3ms
  pw:api     did not find some options - waiting...  +3ms
  pw:api   "networkidle" event fired +398ms
[] could not send message to server: Timeout 30000ms exceeded.
=========================== logs ===========================
waiting for selector "#test1"
  selector resolved to visible <select id="test1" name="cars">โ€ฆ</select>
  selecting specified option(s)
    did not find some options - waiting...
============================================================
Note: use DEBUG=pw:api environment variable to capture Playwright logs.

It similar to this issue: microsoft/playwright#3830

Download Stuck

I tried to download a csv file from a page and it stuck when execute download.Path() or download.Failure(), etc.

HTML Source code:

<a class="ActiveLink" title="CSV (comma delimited)" alt="CSV (comma delimited)" onclick="$find('ctl32').exportReport('CSV');" href="javascript:void(0)" >CSV (comma delimited)</a>

Go code:

download,err:=page.ExpectDownload(func() error {
	return page.Click("#ctl32_ctl05_ctl04_ctl00_Menu > div:nth-child(6) > a")
})
fmt.Println(err) // nil
fmt.Println(download.SuggestedFilename()) // "User Metrics.csv"

// Stuck Here
// =======

// stuck when execute one of the Failure(), Path() or SaveAs() functions.

// I can see the file is downloaded in the folder with a temporary name such as "32a609ac-c0c0-4e87-8bd6-ed4d69c6f474"
// And the file content is right, but the program stops here
fmt.Println(download.Failure())

// Press Ctrl + C from command line shows the following error message
exit status 2
The process tried to write to a nonexistent pipe.

How can I fix this problem?

Fix tags to work with go modules

Please fix the tagging in the project.

Only old tags are showing because they respect semantic versioning:

$ go list -m -versions github.com/mxschmitt/playwright-go
github.com/mxschmitt/playwright-go v0.171.0 v0.171.1

v0.1100 should probably be set to v0.1100.0 (I am not sure about the logic of the numbers you are using here).

Fix array struct generation

Currently interfaces get used if an array is passed. It should use the correct type and use an Slice there:

type BrowserNewContextStorageState struct {
	Cookies interface{} `json:"cookies"`
	Origins interface{} `json:"origins"`
}

Undefined time for get body

Hi,

When I try to get body of response event the implementation waiting undefined time to load and process, no continue and I have to force finish process

page.On("response", func(response playwright.Response) {
    if response.Request().ResourceType() == "xhr" {
	    tx, _ := response.Text()
	    fmt.Println(tx)
    }
})

[question] detect if a window alert is opened

Hi @mxschmitt ,

Hope you are all well !

Thanks for this amazing work with playwright-go :-)

I am working on a xss/sqli crawler and I was wondering if it is possible to catch a window alert triggered with playwright ?

Thanks for any insights or inputs on that question.

Cheers,
X.

Is posible to attach a new page to an existing wb?

Hi,

I'm working with this library and the only thing that I can say is, GREAT JOB!!

I want to replicate the next diagram. Imagine that you have the same app A and B, but A is a daemon (service) and B is the interface. What I want to do is avoid to open the browser every time the user need to do something, I just want to have loaded the broswer and then just go to page that the user is requesting. That is possible?
image

Thanks in advance.

can't run on Big sur 11.2.1

Hi
i am just update my Mac OS to last version today
but i can't run on my program

this the problem is

2021/02/16 20:48:35 Error Browser%!(EXTRA *fmt.wrapError=could not send message: could not send message to server: No executable path is specified. Pass "executablePath" option directly.
Note: use DEBUG=pw:api environment variable and rerun to capture Playwright logs.)
exit status 1
browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
			Headless: playwright.Bool(false),
		})

i had change the browser chromium, firefox or webkit but nobody can work of them

thanks before

IsConnected state is not maintained

Hi, I assumed that IsConnected would represent the connection state between the browser instance and the client. According to the code it just set to true when a browser is created but not updated when the browser connection is closed by accident or manually.

Generate inline documentation

We can use the upstream documentation, strip out the JavaScript examples like we do in Python and sync it with our codebase.

Catch network events

Hi,

Hope you are all well !

I was wondering if it possible to catch all network requests with playwright for loading a page ?

Thanks for any insights or inputs on that.

Cheers,
Luc Michalski

How to get the option value from <Select>tag?

For example:

<select id="p-lang">
<option value="">Make a selection...</option>
<option value="go">Go</option>
<option value="html">HTML</option>
<option value="css">CSS</option>
<option selected="selected" value="php">PHP</option>
<option value="js">JavaScript</option>
</select>

How can I get the value "php" or the test "PHP" from the select tag?
Thanks.

[Bug]: Verify Storage State handling

Hey team,

I'm experimenting with playwright, and trying to save the authenticated state using the context.StorageState()
However, struggling to find the way to restore the stored state.

While investigating, found the reference in this doc, and API doc for NewContext says that there should be an Option that allows to restore the state, namely storageState.

However, browsing the sources it seems that it is not present in the current implementation.

My go.mod

// ...
github.com/mxschmitt/playwright-go v0.1100.0
// ...

If there's a workaround for this, I'd like to know it. I did try saving cookies, and loading them into context, but auth says NO and requires login.

Is there a way to regenerate with this option or are there any plans on adding it?

Thanks!

Feedback needed for future development

Hello,

(upvote this microsoft/playwright#6856 instead)

since I've joined @microsoft we are considering moving this package over to the organisation. By that we would ensure that all features form the original Playwright project are integrate and bugs are fixed. Currently since it was a private project it got unfortunately not that much attention since my time was limited. For that we are currently evaluating what the use-cases are and how many people in which scenarios are using it.

Would be awesome if you could give us some feedback so you could help us with the decision. In more detail, answer the following questions:

  • For what are you using Playwright for Go?
  • Are you using it for personal use or in a company?
  • Do you have any future plans to extend the usage in the future?
  • Do you have any suggestions in terms of API design and if something should be changed for version 1.0?
  • What other libraries did you consider instead of Playwright for Go?
  • Where do you run it? e.g. Docker, Kubernetes, as a script etc.

Thank you!

(Thumbs up if you are interested in it)

Defunct process playwright.sh

I have discovered that playwright.Stop() lefts behind a defunct process.

It is possible to reproduce the problem doing this:

Create this simple Go program:

package main

import (
	"fmt"
	"time"

	"github.com/mxschmitt/playwright-go"
)

func main() {
    runOpts := &playwright.RunOptions{
		SkipInstallBrowsers: true,
	}
	
	pw, err := playwright.Run(runOpts)
	if err != nil {
		panic(err)
	}

	pw.Dispose()
	fmt.Println("after dispose")
	err = pw.Stop()
	fmt.Println("after stop: ", err)
	
	fmt.Println("after cleanup. Waiting...")
	time.Sleep(20*time.Second)
}

As you can see, I have made it wait 20secs at the end, enough to run check the mentioned defunct process.

I build the program with go build . and then I mount the binary in a docker container with the playwright runtime to test it:

$ docker run --name=bugdemo -ti -v $PWD/bugdemo:/mnt/bugdemo mcr.microsoft.com/playwright:focal bash

# /mnt/bugdemo
after dispose
after stop:  <nil>
after cleanup. Waiting...

I open another shell in the container and check the running processes:

$ docker exec -ti bugdemo bash

# ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0   4236  3368 pts/0    Ss   14:14   0:00 bash
root         106  0.1  0.0   4240  3416 pts/1    Ss   14:27   0:00 bash
root         114  0.0  0.0 1078984 6112 pts/0    Sl+  14:27   0:00 ./bugdemo
root         130  0.0  0.0      0     0 pts/0    Z+   14:27   0:00 [playwright.sh] <defunct>
root         133  4.0  0.3 568364 53268 pts/0    Sl+  14:27   0:00 /root/.cache/ms-playwright-go/1.10.0/node /root/.cache/ms-playwright-go/1.10.0/package/lib/cli/cli.js run-driver
root         140  0.0  0.0   5896  2932 pts/1    R+   14:27   0:00 ps aux

After 20secs, once the bugdemo process has finished, the defunct process disappears:

# ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0   4236  3368 pts/0    Ss+  14:14   0:00 bash
root         106  0.0  0.0   4240  3532 pts/1    Ss   14:27   0:00 bash
root         141  0.0  0.0   5896  2848 pts/1    R+   14:27   0:00 ps aux

I would expect pw.Stop() or pw.Dispose() to cleanup the playwright runtime completly.

Unable to set 'ExtraHttpHeaders'

If ExtraHttpHeaders value is set in BrowserNewContextOptions, then the following error occurs at runtime:

$ go run main.go
2021/04/16 12:13:46 Downloading browsers...
2021/04/16 12:13:46 Downloaded browsers successfully
2021/04/16 12:13:47 could not create context: %!w(*fmt.wrapError=&{could not send message: could not send message to server: extraHTTPHeaders: expected array, got object 0xc0001a60a0})
exit status 1

Used the following for testing:

package main

import (
        "log"

        "github.com/mxschmitt/playwright-go"
)

func assertErrorToNilf(message string, err error) {
        if err != nil {
                log.Fatalf(message, err)
        }
}

func main() {
        pw, err := playwright.Run()
        assertErrorToNilf("could not launch playwright: %w", err)
        browser, err := pw.Firefox.Launch(playwright.BrowserTypeLaunchOptions{
                Headless: playwright.Bool(false),
        })
        assertErrorToNilf("could not launch Firefox: %w", err)
        context, err := browser.NewContext(playwright.BrowserNewContextOptions{
                ExtraHttpHeaders: map[string]string{
                        "X-Dummy-Header": "test-header",
                },
        })
        assertErrorToNilf("could not create context: %w", err)
        page, err := context.NewPage()
        assertErrorToNilf("could not create page: %w", err)
        _, err = page.Goto("https://duckduckgo.com")
        assertErrorToNilf("could not goto: %w", err)
}

Using Plain Go Type

From example:

browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
    Headless: playwright.Bool(false),
})

Is it possible to use plain Go type bool like:

browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
    Headless: false,
})

You can convert false to playwright.Bool(false) in Launch() method.

This will be easier for end user.
Also the you may not need to export Bool() or similar type convert method as a public method.

Improve versioning with Git tags / GH releases.

Hello,
thanks for the lib It works great! I faced with some limitations:

Waiting for one of the selectors or alert windows

I want to use this library to automate my daily tasks, like giving a job id, go to a page find all the pallets of this job and process these pallets one by one. Or giving an asset id, I verify if all the processing data are valid for this asset, etc.

When I click a button, it may show out different content, or alert messages according to the data.
What's the proper way in playwright-go to process these situation?

What's the correct way to do this in playwright-go?

    await page.goto(loadLink)

    // if load id is valid, this PALLET DETAILS table will show out
    const hdTable = "#ctl00_ctl00_LoadPallet"
    // if load id is invalid, it will show warning window with this close button
    const closeButton = "#ctl00_ctl00_LoadGridClose"
    await Promise.race([
        page.click(closeButton).then(() => false),
        page.waitForSelector(hdTable).then(() => true)
    ]).then(value => {
        if (!value) {
            console.log("wrong load id: ", loadID)
            return
        }
    })

Thanks

Troubleshooting ElementHandle.Type

Code:

package main

import (
	"log"

	"github.com/mxschmitt/playwright-go"
)

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)
	}
	_, err = page.Goto("https://yujiri.xyz/test")
	if err != nil {
		log.Fatalf("could not goto: %v", err)
	}
	textfield, err := page.QuerySelector("input")
	if err != nil {
		log.Fatalf("could not get element: %v", err)
	}
	err = textfield.Type("test")
	if err != nil {
		log.Fatalf("Could not enter text: %v", err)
	}
}

Content of page:

<input>

Output:

2021/04/14 16:48:52 Downloading browsers...
2021/04/14 16:48:52 Downloaded browsers successfully
2021/04/14 16:48:54 Could not enter text: could not send message to server: text: expected string, got undefined
exit status 1

OS: Linux
Playwright-go: v0.1100.0

Unable to set Envs for browser

Hi

I'm wonder is it a bug or I'm doing something wrong? I'm trying to pass environment variable to the browser like:

	b, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
		Env: map[string]interface{}{
			"DISPLAY": ":99",
		},
	})

and getting FATAL Error found {"error": "could not send message: could not send message to server: env: expected array, got object"}

Version used: github.com/mxschmitt/playwright-go v0.171.1

UPDATE: I enabled DEBUGP=1 and got:

SEND>{"guid":"BrowserType@07f6a400aa7d2fba2442974fc31bc482","id":1,"method":"launch","params":{"env":{"DISPLAY":":99"}}}
RECV>{"id":1,"guid":"","method":"","params":null,"result":null,"error":{"error":{"name":"Error","message":"env: expected array, got object","stack":"Error: env: expected array, got object\n    at /snapshot/playwright-cli/node_modules/playwright/lib/protocol/validatorPrimitives.js:69:19\n    at /snapshot/playwright-cli/node_modules/playwright/lib/protocol/validatorPrimitives.js:63:16\n    at Object.BrowserTypeLaunchParams (/snapshot/playwright-cli/node_modules/playwright/lib/protocol/validatorPrimitives.js:81:27)\n    at DispatcherConnection._validateParams (/snapshot/playwright-cli/node_modules/playwright/lib/dispatchers/dispatcher.js:122:32)\n    at DispatcherConnection.dispatch (/snapshot/playwright-cli/node_modules/playwright/lib/dispatchers/dispatcher.js:146:36)\n    at Transport.onmessage (/snapshot/playwright-cli/lib/driver.js:48:61)\n    at Immediate._onImmediate (/snapshot/playwright-cli/node_modules/playwright/lib/protocol/transport.js:70:26)\n    at processImmediate (internal/timers.js:456:21)"}}}

How to get text content from iFrame?

After I click a button, the web page shows some text in iFrame.
I have the id of a label in this iFrame.
How can I get the text of the label in this iFrame?
PLease help.
Thanks

Page on response event: Response.Body() not working

my code:

`account.Pages.Active().On("response", func(response *playwright.Response) {
	if strings.Index(response.URL(), "authenticationstate") != -1 {
		out.Debug("Response detected", response.URL())
		accountAuthStatus := authState{}

		out.Debug("decoding json")
		jsonDecodeError := response.JSON(&accountAuthStatus)

		if jsonDecodeError != nil {
			out.Debug("Error decode", jsonDecodeError.Error())
		}
		out.Debug("printing result")
		out.Debug(accountAuthStatus.String())
	}
})`

response.JSON() freezes the code and not returns any value ,

Unable to return cookies from chromium with error "call of reflect.Value.Float on zero Value"

panic: reflect: call of reflect.Value.Float on zero Value

goroutine 9 [running]:
reflect.Value.Float(...)
C:/Program Files/Go/src/reflect/value.go:931
github.com/mxschmitt/playwright-go.remapValue(0x0, 0x0, 0x0, 0x130b260, 0xc00045b0c0, 0x182)
C:/Users/sicko/go/pkg/mod/github.com/mxschmitt/[email protected]/helpers.go:138 +0xc05
github.com/mxschmitt/playwright-go.remapValue(0x134de80, 0xc0026a78f0, 0x15, 0x13f6cc0, 0xc00045b080, 0x199)
C:/Users/sicko/go/pkg/mod/github.com/mxschmitt/[email protected]/helpers.go:158 +0x538
github.com/mxschmitt/playwright-go.remapMapToStruct(0x134de80, 0xc0026a78f0, 0x12e8e80, 0xc00045b080)
C:/Users/sicko/go/pkg/mod/github.com/mxschmitt/[email protected]/helpers.go:168 +0x167
github.com/mxschmitt/playwright-go.(*browserContextImpl).Cookies(0xc000268900, 0x0, 0x0, 0x0, 0x8, 0x0, 0x93e869, 0x9, 0x203000b)

Size

How can i set the size of the browser(width, height) on first launch and later resize programatically ?
Is this available as feature ?

ElementHandle.Type does not work with go modules

I am hitting this extremely weird issue where this code:

usernameBox, _ := page.QuerySelector("[name=username]")
usernameBox.Type("[email protected]")

does not work with go modules. Although it is able to select the input tag, it fails to populate it with the text passed to 'Type'. As soon as I run this using the old way, it starts working.

Complete code for reference:

    pw, err := playwright.Run()
	if err != nil {
		log.Fatalf("could not start playwright: %v", err)
		panic(err)
	}

	browserLaunchOptions := playwright.BrowserTypeLaunchOptions{
		Headless:      newFalse(),
		DownloadsPath: newDownloadPath(),
	}

	browser, err := pw.Chromium.Launch(browserLaunchOptions)
	if err != nil {
		log.Fatalf("could not launch browser: %v", err)
		panic(err)
	}
	browserContextOptions := playwright.BrowserNewContextOptions{
		AcceptDownloads: newTrue(),
	}

	page, err := browser.NewPage(browserContextOptions)
	dwnld, _ := page.ExpectDownload(func() error {
		if err != nil {
			log.Fatalf("could not create page: %v", err)
		}
		resp, err := page.Goto(linkToWebsite, playwright.PageGotoOptions{
			WaitUntil: playwright.WaitUntilStateNetworkidle,
		})
		Sleep(15, "Website loading")
		if err != nil {
			log.Fatalf("could not goto: %v", err)
		}
		log.Println(resp.Status())
		if !resp.Ok() {
			log.Fatalf("Invalid response returned by %v", linkToWebsite)
		}

		usernameBox, _ := page.QuerySelector("[name=username]")

		op := playwright.ElementHandleTypeOptions{
			Delay: newDelay(6),
		}
		usernameBox.Type("[email protected]", op)

		passwordBox, _ := page.QuerySelector("[name=password]")
		passwordBox.Type("******")

		loginBtn, _ := page.QuerySelector("button")
		loginBtn.Click()

		Sleep(15000000, "Logged in, waiting for the page to load")

		return nil
	})

Investigate into Must* methods

Handling the error each time is not nice, it might make sense to add or generate Must* methods which panic if an error gets returned.

Install only one browser?

Is is possible to install only one browser using the playwright-driver? I don't need all three downloaded.

Thanks

Not working on WSL 2

Hi,

I'm using playwright on this project hippokampe specifically in the api module. This app is already tested in vagrant and linux pc, with positive results, but in the case of WSL 2, it's not working properly.

I debugged the project, and I can conclude that the problem it's raised right here. I don't receive an error but blocks the execution, like an infinite loop.

Thanks

Auto generate API Interfaces

Currently the API in the interface are hardcoded, the big benefit we get when we autogenerate them and put also the documentation in a follow up PR there.

panic: interface conversion: interface {} is nil, not *playwright.channel

I'm getting panic exception instead of err if selector doesn't exist.

Everything works fine when an element exists on the page (<a> tag):

	linkElement, err := entry.QuerySelector("a")
	if err != nil {
		log.Fatalf("could not get link element: %v", err)
	}

But panic raised when it doesn't (<a class="someclass"> tag):

	linkElement, err := entry.QuerySelector("a.someclass")
	if err != nil {
		log.Fatalf("could not get link element: %v", err)
	}

panic: interface conversion: interface {} is nil, not *playwright.channel

Unable to write regexp for WaitForNavigation

I'm trying to see if the page gets redirected to a URL that looks like api-SOME_HASH.mywebsite.com. Unfortunately, none of the URL options work.

Here is the code:

	var timeout float64 = float64(10 * time.Second.Milliseconds())
	var url = `/mywebsite\.com/`
	_, err = page.WaitForNavigation(playwright.PageWaitForNavigationOptions{
		Timeout: &timeout,
		URL:     &url,
	})

The different options I tried for url:

  • "/mywebsite\.com/"
  • "mywebsite\.com"
  • "mywebsite.com"
  • "*mywebsite.com*"
  • ".*mywebsite\.com.*"
  • regexp.MustCompile(".*mywebsite\.com.*")

(Note: quotes must be substituted with backticks because of escaping)

All of the above result in the following error:

panic: (*string) 0xc0004049f0

goroutine 34 [running]:
github.com/mxschmitt/playwright-go.(*urlMatcher).Matches(0xc000404a00, 0xc00046e000, 0x276, 0x3)
	/Users/marksk/.local/share/go/pkg/mod/github.com/mxschmitt/[email protected]/helpers.go:198 +0x3fc
github.com/mxschmitt/playwright-go.(*frameImpl).WaitForNavigation.func1(0xc000439fc0, 0x1, 0x1, 0x0)
	/Users/marksk/.local/share/go/pkg/mod/github.com/mxschmitt/[email protected]/frame.go:165 +0x10e
reflect.Value.call(0x1413a60, 0xc000404a10, 0x13, 0x14afcbd, 0x4, 0xc00028b8f0, 0x1, 0x1, 0x18, 0xc000464720, ...)
	/usr/local/go/src/reflect/value.go:476 +0x8e7
reflect.Value.Call(0x1413a60, 0xc000404a10, 0x13, 0xc0001c9458, 0x1, 0x1, 0x14060a0, 0x1, 0xc000464720)
	/usr/local/go/src/reflect/value.go:337 +0xb9
github.com/mxschmitt/playwright-go.waitForEvent.func1(0xc000439f60, 0x1, 0x1)
	/Users/marksk/.local/share/go/pkg/mod/github.com/mxschmitt/[email protected]/helpers.go:314 +0x206
reflect.Value.call(0x140b500, 0xc000406ed0, 0x13, 0x14afcbd, 0x4, 0xc00028b8a8, 0x1, 0x1, 0x14ae2e0, 0xc0001c9750, ...)
	/usr/local/go/src/reflect/value.go:476 +0x8e7
reflect.Value.Call(0x140b500, 0xc000406ed0, 0x13, 0xc00028b878, 0x1, 0x1, 0xc00028b878, 0xc0001c9810, 0x0)
	/usr/local/go/src/reflect/value.go:337 +0xb9
github.com/mxschmitt/playwright-go.(*eventEmitter).Emit.func1(0xc000404a40, 0x1, 0x1)
	/Users/marksk/.local/share/go/pkg/mod/github.com/mxschmitt/[email protected]/event_emitter.go:38 +0x1a5
github.com/mxschmitt/playwright-go.(*eventEmitter).Emit(0xc0001d38d8, 0x14b1d1f, 0x9, 0xc0001c98d8, 0x1, 0x1)
	/Users/marksk/.local/share/go/pkg/mod/github.com/mxschmitt/[email protected]/event_emitter.go:42 +0x252
github.com/mxschmitt/playwright-go.(*frameImpl).onFrameNavigated(0xc0001d38c0, 0xc000464690)
	/Users/marksk/.local/share/go/pkg/mod/github.com/mxschmitt/[email protected]/frame.go:185 +0x191
reflect.Value.call(0x140b6c0, 0xc000191850, 0x13, 0x14afcbd, 0x4, 0xc00028b860, 0x1, 0x1, 0x14ae2e0, 0x0, ...)
	/usr/local/go/src/reflect/value.go:476 +0x8e7
reflect.Value.Call(0x140b6c0, 0xc000191850, 0x13, 0xc00028b860, 0x1, 0x1, 0xc00045ca70, 0x9, 0x1)
	/usr/local/go/src/reflect/value.go:337 +0xb9
github.com/mxschmitt/playwright-go.(*eventEmitter).Emit.func1(0xc000191860, 0x1, 0x1)
	/Users/marksk/.local/share/go/pkg/mod/github.com/mxschmitt/[email protected]/event_emitter.go:38 +0x1a5
github.com/mxschmitt/playwright-go.(*eventEmitter).Emit(0xc00022f1f0, 0xc00045ca70, 0x9, 0xc0001c9dc0, 0x1, 0x1)
	/Users/marksk/.local/share/go/pkg/mod/github.com/mxschmitt/[email protected]/event_emitter.go:42 +0x252
github.com/mxschmitt/playwright-go.(*connection).Dispatch(0xc00029a070, 0xc0002c1cc0)
	/Users/marksk/.local/share/go/pkg/mod/github.com/mxschmitt/[email protected]/connection.go:73 +0x452
github.com/mxschmitt/playwright-go.(*transport).Start(0xc00028c0f0, 0x14d1740, 0xc0001acae0)
	/Users/marksk/.local/share/go/pkg/mod/github.com/mxschmitt/[email protected]/transport.go:44 +0xc6
github.com/mxschmitt/playwright-go.(*connection).Start(...)
	/Users/marksk/.local/share/go/pkg/mod/github.com/mxschmitt/[email protected]/connection.go:28
github.com/mxschmitt/playwright-go.Run.func1(0xc00029a070)
	/Users/marksk/.local/share/go/pkg/mod/github.com/mxschmitt/[email protected]/run.go:233 +0x32
created by github.com/mxschmitt/playwright-go.Run
	/Users/marksk/.local/share/go/pkg/mod/github.com/mxschmitt/[email protected]/run.go:232 +0x1d5
exit status 2
(node:16889) UnhandledPromiseRejectionWarning: Error: write EPIPE
    at afterWriteDispatched (internal/stream_base_commons.js:156:25)
    at writeGeneric (internal/stream_base_commons.js:147:3)
    at Socket._writeGeneric (net.js:787:11)
    at Socket._write (net.js:799:8)
    at doWrite (_stream_writable.js:403:12)
    at writeOrBuffer (_stream_writable.js:387:5)
    at Socket.Writable.write (_stream_writable.js:318:11)
    at Transport.send (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/protocol/transport.js:47:25)
    at DispatcherConnection.dispatcherConnection.onmessage (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/cli/driver.js:63:59)
    at DispatcherConnection.sendMessageToClient (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/dispatcher.js:136:14)
(node:16889) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:16889) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:16889) UnhandledPromiseRejectionWarning: Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
    at doWrite (_stream_writable.js:399:19)
    at writeOrBuffer (_stream_writable.js:387:5)
    at Socket.Writable.write (_stream_writable.js:318:11)
    at Transport.send (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/protocol/transport.js:47:25)
    at DispatcherConnection.dispatcherConnection.onmessage (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/cli/driver.js:63:59)
    at DispatcherConnection.sendMessageToClient (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/dispatcher.js:136:14)
    at PageDispatcher._dispatchEvent (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/dispatcher.js:72:26)
    at Page.<anonymous> (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/pageDispatcher.js:64:71)
    at Page.emit (events.js:314:20)
    at FrameManager.requestFinished (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/server/frames.js:239:24)
(node:16889) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:16889) UnhandledPromiseRejectionWarning: Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
    at doWrite (_stream_writable.js:399:19)
    at writeOrBuffer (_stream_writable.js:387:5)
    at Socket.Writable.write (_stream_writable.js:318:11)
    at Transport.send (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/protocol/transport.js:47:25)
    at DispatcherConnection.dispatcherConnection.onmessage (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/cli/driver.js:63:59)
    at DispatcherConnection.sendMessageToClient (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/dispatcher.js:136:14)
    at new Dispatcher (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/dispatcher.js:63:30)
    at new RequestDispatcher (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/networkDispatchers.js:32:9)
    at Function.from (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/networkDispatchers.js:25:26)
    at Page.<anonymous> (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/pageDispatcher.js:58:137)
(node:16889) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:16889) UnhandledPromiseRejectionWarning: Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed                                
    at doWrite (_stream_writable.js:399:19)
    at writeOrBuffer (_stream_writable.js:387:5)
    at Socket.Writable.write (_stream_writable.js:318:11)
    at Transport.send (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/protocol/transport.js:47:25)
    at DispatcherConnection.dispatcherConnection.onmessage (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/cli/driver.js:63:59)
    at DispatcherConnection.sendMessageToClient (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/dispatcher.js:136:14)
    at new Dispatcher (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/dispatcher.js:63:30)
    at new RequestDispatcher (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/networkDispatchers.js:32:9)
    at Function.from (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/networkDispatchers.js:25:26)
    at Page.<anonymous> (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/pageDispatcher.js:58:137)
(node:16889) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 4)
(node:16889) UnhandledPromiseRejectionWarning: Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
    at doWrite (_stream_writable.js:399:19)
    at writeOrBuffer (_stream_writable.js:387:5)
    at Socket.Writable.write (_stream_writable.js:318:11)
    at Transport.send (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/protocol/transport.js:47:25)
    at DispatcherConnection.dispatcherConnection.onmessage (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/cli/driver.js:63:59)
    at DispatcherConnection.sendMessageToClient (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/dispatcher.js:136:14)
    at new Dispatcher (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/dispatcher.js:63:30)
    at new RequestDispatcher (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/networkDispatchers.js:32:9)
    at Function.from (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/networkDispatchers.js:25:26)
    at Page.<anonymous> (/Users/marksk/Library/Caches/ms-playwright-go/1.10.0/package/lib/dispatchers/pageDispatcher.js:58:137)
(node:16889) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 5)

Am I doing something wrong? I would like to add here that I couldn't find any examples for URLs neither in the official playwright docs nor in playwright-go examples.

Add missing functionality/methods

Current report (node scripts/validate-interfaces.js):

Missing API interface functions:

  • BrowserContext.SetHTTPCredentials
  • BrowserContext.StorageState
  • ElementHandle.WaitForElementState
  • ElementHandle.WaitForSelector

Is this project continued?

Few days ago, there was a playwright-go link at playwright official page(Supported language).

But now, it's gone.

Could I use this this lib?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.