rod

package module
v0.39.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2020 License: MIT Imports: 26 Imported by: 0

README

Overview

GoDoc codecov goreport Discord Chat

logo

Rod is a High-level Devtools driver directly based on DevTools Protocol. It's designed for web automation and scraping. Rod also tries to expose low-level interfaces to users, so that whenever a function is missing users can easily send control requests to the browser directly.

Features

  • Fluent interface design to reduce verbose code
  • Chained context design, intuitive to timeout or cancel the long-running task
  • Debugging friendly, auto input tracing, remote monitoring headless browser
  • Automatically find or download browser
  • No external dependencies, CI tested on Linux, Mac, and Windows
  • High-level helpers like WaitStable, WaitRequestIdle, GetDownloadFile, Resource
  • Two-step WaitEvent design, never miss an event
  • Correctly handles nested iframes
  • No zombie browser process after the crash (how it works)

Examples

You can find examples from here or here.

For more detailed examples, please search the unit tests. Such as the usage of method HandleAuth, you can search all the *_test.go files that contain HandleAuth or HandleAuthE, for example, use Github online search in repository. You can also search the GitHub issues, they contain a lot of usage examples too.

If you have questions, please raise an issue or join the chat room.

How it works

Here's the common start process of Rod:

  1. Try to connect to a Devtools endpoint, if not found try to launch a local browser, if still not found try to download one, then connect again. The lib to handle it is here.

  2. Use the JSON-RPC to talk to the browser endpoint to control it. The client to handle it is here.

  3. The type definitions of the data transmitted via JSON-RPC are handled by this lib.

  4. To control a specific page, Rod will first inject a js helper script to it. Rod uses it to query and manipulate the page content. The js lib is here.

FAQ

Q: How to use Rod with docker

To let rod work with docker is very easy:

  1. Run the Rod image docker run -p 9222:9222 ysmood/rod

  2. Open another terminal and run a go program like this example

The Rod image can dynamically launch a browser for each remote driver with customizable browser flags. It's tuned for screenshots and fonts among popular natural languages. You can easily load balance requests to the cluster of this image, each container can create multiple browser instances at the same time.

Q: Does it support other browsers like Firefox or Edge

Rod should work with any browser that supports DevTools Protocol.

  • Microsoft Edge can pass all the unit tests.
  • Firefox is supporting this protocol.
  • Safari doesn't have any plan to support it yet.
  • IE won't support it.
Q: Why is it called Rod

Rod is related to puppetry, see Rod Puppet. So we are the puppeteer, the browser is the puppet, we use the rod to control the puppet. So in this sense, puppeteer.js sounds strange, we are controlling a puppeteer?

Q: How to contribute

Please check this doc.

Q: How versioning is handled

Semver is used.

Before v1.0.0 whenever the second section changed, such as v0.1.0 to v0.2.0, there must be some public API changes, such as changes of function names or parameter types. If only the last section changed, no public API will be changed.

Q: Why another puppeteer like lib

There are a lot of great projects, but no one is perfect, choose the best one that fits your needs is important.

  • selenium

    It's slower by design because it encourages the use of hard-coded sleep. When work with Rod, you generally don't use sleep at all. Therefore it's more buggy to use selenium if the network is unstable. It's harder to setup and maintain because of extra dependencies like a browser driver.

  • puppeteer

    With Puppeteer, you have to handle promise/async/await a lot. It requires a deep understanding of how promises works which are usually painful for QA to write automation tests. End to end tests usually requires a lot of sync operations to simulate human inputs, because Puppeteer is based on Nodejs all control signals it sends to the browser will be async calls, so it's unfriendly for QA from the beginning.

    Rod will only enable domain events when they are needed, puppeteer will always enable all the domains which will consume a lot of resources when driving a remote browser.

  • chromedp

    With Chromedp, you have to use their verbose DSL like tasks to handle the main logic, because Chromedp uses several wrappers to handle execution with context and options which makes it very hard to understand their code when bugs happen. The DSL like wrapper also make the Go type useless when tracking issues.

    It's painful to use Chromedp to deal with iframes, this ticket is still open after years.

    When a crash happens, Chromedp will leave the zombie browser process on Windows and Mac.

  • cypress

    Cypress is very limited, for closed shadow dom or cross-domain iframes it's almost unusable. Read their limitation doc for more details.

Documentation

Overview

Example (Basic)

Open github, search for "git"

package main

import (
	"fmt"
	"time"

	"github.com/ysmood/rod"
	"github.com/ysmood/rod/lib/input"
)

func main() {
	// launch and connect to a browser
	browser := rod.New().Connect()

	// Even you forget to close, rod will close it after main process ends
	defer browser.Close()

	// timeout will be passed to chained function calls
	page := browser.Timeout(time.Minute).Page("https://github.com")

	// make sure windows size is consistent
	page.Window(0, 0, 1200, 600)

	// use css selector to get the search input element and input "git"
	page.Element("input").Input("git").Press(input.Enter)

	// wait until css selector get the element then get the text content of it
	text := page.Element(".codesearch-results p").Text()

	fmt.Println(text)

}
Output:

Git is the most widely used version control system.
Example (Customize_browser_launch)

The launcher lib comes with a lot of default switches (flags) to launch browser, this example shows how to add or delete switches.

package main

import (
	"fmt"

	"github.com/ysmood/rod"
	"github.com/ysmood/rod/lib/launcher"
)

func main() {
	// set custom browser options
	// use IDE to check the doc of launcher.New you will find out more info
	url := launcher.New().
		Set("proxy-server", "127.0.0.1:8080"). // add a flag, here we set a http proxy
		Delete("use-mock-keychain").           // delete a flag
		Launch()

	browser := rod.New().ControlURL(url).Connect()
	defer browser.Close()

	// auth the proxy
	// here we use cli tool "mitmproxy --proxyauth user:pass" as an example
	browser.HandleAuth("user", "pass")

	// mitmproxy needs cert config to support https, use http here as an example
	fmt.Println(browser.Page("http://example.com/").Element("title").Text())

	// Skip
	
Output:

Example (Customize_retry_strategy)

Useful when you want to customize the element query retry logic

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/ysmood/kit"
	"github.com/ysmood/rod"
)

func main() {
	browser := rod.New().Timeout(time.Minute).Connect()
	defer browser.Close()

	page := browser.Page("https://github.com")

	backoff := kit.BackoffSleeper(30*time.Millisecond, 3*time.Second, nil)

	// here we use low-level api ElementE other than Element to have more options,
	// use backoff algorithm to do the retry
	el, err := page.Timeout(10*time.Second).ElementE(backoff, "", "input")
	if err == context.DeadlineExceeded {
		fmt.Println("we can't find the element before timeout")
	} else {
		kit.E(err)
	}

	// get element without retry
	el, err = page.ElementE(nil, "", "input")
	if rod.IsError(err, rod.ErrElementNotFound) {
		fmt.Println("element not found")
	} else {
		kit.E(err)
	}

	fmt.Println(el.Eval(`() => this.name`).String())

}
Output:

q
Example (Debug_mode)

Rod provides a lot of debug options, you can use set methods to enable them or use environment variables list at "lib/defaults".

package main

import (
	"fmt"
	"time"

	"github.com/ysmood/kit"
	"github.com/ysmood/rod"
	"github.com/ysmood/rod/lib/input"
	"github.com/ysmood/rod/lib/launcher"
)

func main() {
	url := launcher.New().
		Headless(false). // run browser on foreground, you can also use env "rod=show"
		Devtools(true).  // open devtools for each new tab
		Launch()

	browser := rod.New().
		Timeout(time.Minute).
		ControlURL(url).
		Trace(true).                 // show trace of each input action
		Slowmotion(2 * time.Second). // each input action will take 2 second
		Connect()

	// the monitor server that plays the screenshots of each tab, useful when debugging headlee mode
	browser.ServeMonitor(":9777")

	defer browser.Close()

	page := browser.Page("https://www.wikipedia.org/")

	page.Element("#searchLanguage").Select("[lang=zh]")
	page.Element("#searchInput").Input("热干面")
	page.Keyboard.Press(input.Enter)

	fmt.Println(page.Element("#firstHeading").Text())

	// get the image binary
	img := page.Element(`[alt="Hot Dry Noodles.jpg"]`)
	_ = kit.OutputFile("tmp/img.jpg", img.Resource(), nil)

	// pause the js execution
	// you can resume by open the devtools and click the resume button on source tab
	page.Pause()

	// Skip
	
Output:

Example (Direct_cdp)

Useful when rod doesn't have the function you want, you can call the cdp interface directly easily.

package main

import (
	"encoding/json"
	"fmt"
	"time"

	"github.com/ysmood/kit"
	"github.com/ysmood/rod"
	"github.com/ysmood/rod/lib/proto"
)

func main() {
	browser := rod.New().Timeout(time.Minute).Connect()
	defer browser.Close()

	// The code here is how SetCookies works
	// Normally, you use something like browser.Page("").SetCookies(...).Navigate(url)

	page := browser.Page("")

	// call cdp interface directly here
	// set the cookie before we visit the website
	// the "proto" lib contains every JSON schema you need to communicate with browser
	res, err := proto.NetworkSetCookie{
		Name:  "rod",
		Value: "test",
		URL:   "https://example.com",
	}.Call(page)
	kit.E(err)

	fmt.Println(res.Success)

	page.Navigate("https://example.com")

	// eval js on the page to get the cookie
	cookie := page.Eval(`() => document.cookie`).String()

	fmt.Println(cookie)

	// Or even more low-level way to use raw json to send request to browser.
	data, _ := json.Marshal(map[string]string{
		"name":  "rod",
		"value": "test",
		"url":   "https://example.com",
	})
	_, _ = browser.Call(page.GetContext(), string(page.SessionID), "Network.SetCookie", data)

}
Output:

true
rod=test
Example (Handle_events)

Shows how to subscribe events.

package main

import (
	"fmt"
	"time"

	"github.com/ysmood/rod"
	"github.com/ysmood/rod/lib/proto"
)

func main() {
	browser := rod.New().Timeout(time.Minute).Connect()
	defer browser.Close()

	go browser.EachEvent(func(e *proto.TargetTargetCreated) {
		// if it's not a page return
		if e.TargetInfo.Type != proto.TargetTargetInfoTypePage {
			return
		}

		// create a page from the page id
		page02 := browser.PageFromTargetID(e.TargetInfo.TargetID)

		// log "hey" on each newly created page
		page02.Eval(`() => console.log("hey")`)
	})()

	page01 := browser.Page("")

	// print all "console.log" outputs
	go page01.EachEvent(func(e *proto.RuntimeConsoleAPICalled) {
		page01.ObjectsToJSON(e.Args).Join(" ")
	})()

	// Subscribe events before they happen, run the "wait()" to start consuming the events.
	// Here we return an optional stop signal at the first event to stop the loop.
	wait := page01.EachEvent(func(e *proto.PageLoadEventFired) (stop bool) {
		return true
	})

	page01.Navigate("https://example.com")

	wait()

	// the above is the same as below
	if false {
		page01.WaitEvent(&proto.PageLoadEventFired{})()
	}

	fmt.Println("done")

}
Output:

done
Example (Hijack_requests)

Request interception example to modify request or response.

package main

import (
	"fmt"
	"time"

	"github.com/ysmood/rod"
)

func main() {
	browser := rod.New().Timeout(time.Minute).Connect()
	defer browser.Close()

	router := browser.HijackRequests()
	defer router.Stop()

	router.Add("*.js", func(ctx *rod.Hijack) {
		// Send request load response from real destination as the default value to hijack.
		// If you want to safe bandwidth and don't call it, you have to mock the entire response (status code, headers, body).
		ctx.LoadResponse()

		// override response body, we let all js log string "rod"
		ctx.Response.SetBody(ctx.Response.StringBody() + "\n document.title = 'hi' ")
	})

	go router.Run()

	browser.Page("https://www.wikipedia.org/").Wait(`() => document.title == 'hi'`)

	fmt.Println("done")

}
Output:

done
Example (Reuse_sessions)

Such as you logged in your github account and you want to reuse the login session, you may want to launch the browser like this example.

package main

import (
	"fmt"

	"github.com/ysmood/rod"
	"github.com/ysmood/rod/lib/launcher"
)

func main() {
	url := launcher.NewUserMode().Launch()

	browser := rod.New().ControlURL(url).Connect()

	browser.Page("https://github.com")

	fmt.Println("done")

	// Skip
	
Output:

Example (States)
package main

import (
	"fmt"
	"time"

	"github.com/ysmood/rod"
	"github.com/ysmood/rod/lib/proto"
)

func main() {
	browser := rod.New().Timeout(time.Minute).Connect()
	defer browser.Close()

	page := browser.Page("")

	// to detect if network is enabled or not
	fmt.Println(page.LoadState(&proto.NetworkEnable{}))

	_ = proto.NetworkEnable{}.Call(page)

	// to detect if network is enabled or not
	fmt.Println(page.LoadState(&proto.NetworkEnable{}))

}
Output:

false
true
Example (Wait_for_animation)

If a button is moving too fast, you cannot click it as a human, to perfectly simulate human inputs the click trigger by Rod are based on mouse point location, so usually you need wait a button is stable before you can click it.

package main

import (
	"fmt"
	"time"

	"github.com/ysmood/rod"
)

func main() {
	browser := rod.New().Timeout(time.Minute).Connect()
	defer browser.Close()

	page := browser.Page("https://getbootstrap.com/docs/4.0/components/modal/")

	page.WaitLoad().Element("[data-target='#exampleModalLive']").Click()

	saveBtn := page.ElementMatches("#exampleModalLive button", "Close")

	// wait until the save button's position is stable
	// and we don't wait more than 5s, saveBtn will also inherit the 1min timeout from the page
	saveBtn.Timeout(5 * time.Second).WaitStable().Click().WaitInvisible()

	fmt.Println("done")

}
Output:

done
Example (Wait_for_request)

Some page interaction finishes after some network requests, WaitRequestIdle is designed for it.

package main

import (
	"fmt"
	"time"

	"github.com/ysmood/rod"
)

func main() {
	browser := rod.New().Timeout(time.Minute).Connect()
	defer browser.Close()

	page := browser.Page("https://duckduckgo.com/")

	// the page will send a request to fetch the suggestions
	wait := page.WaitRequestIdle()
	page.Element("#search_form_input_homepage").Click().Input("test")
	time.Sleep(300 * time.Millisecond) // wait for js debounce
	wait()

	// we must be able to get several suggestion items
	fmt.Println(len(page.Elements(".search__autocomplete .acp")) > 0)

}
Output:

true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CancelPanic added in v0.9.3

func CancelPanic(err error)

CancelPanic graceful panic

func Event added in v0.31.0

func Event(msg *cdp.Event, evt proto.Payload) bool

Event helps to convert a cdp.Event to proto.Payload. Returns false if the conversion fails

func IsError added in v0.9.3

func IsError(err error, code ErrCode) bool

IsError type matches

func SprintFnThis added in v0.5.0

func SprintFnThis(js string) string

SprintFnThis wrap js with this

Types

type Array added in v0.29.0

type Array []interface{}

Array of any type

type Box added in v0.22.0

type Box struct {
	Top    float64 `json:"top"`
	Left   float64 `json:"left"`
	Width  float64 `json:"width"`
	Height float64 `json:"height"`
}

Box represents the element bounding rect

type Browser

type Browser struct {

	// BrowserContextID is the id for incognito window
	BrowserContextID proto.BrowserBrowserContextID
	// contains filtered or unexported fields
}

Browser represents the browser It doesn't depends on file system, it should work with remote browser seamlessly. To check the env var you can use to quickly enable options from CLI, check here: https://pkg.go.dev/github.com/ysmood/rod/lib/defaults

func New added in v0.13.0

func New() *Browser

New creates a controller

func (*Browser) Call

func (b *Browser) Call(ctx context.Context, sessionID, methodName string, params json.RawMessage) (res []byte, err error)

Call raw cdp interface directly

func (*Browser) CallContext added in v0.29.0

func (b *Browser) CallContext() (context.Context, proto.Client, string)

CallContext parameters for proto

func (*Browser) CancelTimeout added in v0.18.0

func (b *Browser) CancelTimeout() *Browser

CancelTimeout context

func (*Browser) Client added in v0.28.0

func (b *Browser) Client(c *cdp.Client) *Browser

Client set the cdp client

func (*Browser) Close

func (b *Browser) Close()

Close the browser and release related resources

func (*Browser) CloseE added in v0.4.1

func (b *Browser) CloseE() error

CloseE doc is similar to the method Close

func (*Browser) Connect added in v0.13.0

func (b *Browser) Connect() *Browser

Connect to the browser and start to control it. If fails to connect, try to run a local browser, if local browser not found try to download one.

func (*Browser) ConnectE added in v0.13.0

func (b *Browser) ConnectE() error

ConnectE doc is similar to the method Connect

func (*Browser) Context added in v0.12.0

func (b *Browser) Context(ctx context.Context) *Browser

Context creates a clone with a context that inherits the previous one

func (*Browser) ControlURL

func (b *Browser) ControlURL(url string) *Browser

ControlURL set the url to remote control browser.

func (*Browser) DisableDomain added in v0.39.1

func (b *Browser) DisableDomain(ctx context.Context, sessionID proto.TargetSessionID, method proto.Payload) (recover func())

DisableDomain and returns a recover function to restore previous state

func (*Browser) EachEvent added in v0.31.1

func (b *Browser) EachEvent(fn interface{}) (wait func())

EachEvent of the specified event type, if the fn returns true the event loop will stop. The fn can accpet multiple events, such as EachEvent(func(e1 *proto.PageLoadEventFired, e2 *proto.PageLifecycleEvent) {}), only one argument will be non-null, others will null.

func (*Browser) EnableDomain added in v0.38.0

func (b *Browser) EnableDomain(ctx context.Context, sessionID proto.TargetSessionID, method proto.Payload) (recover func())

EnableDomain and returns a recover function to restore previous state

func (*Browser) Event

func (b *Browser) Event() *goob.Observable

Event returns the observable for browser events

func (*Browser) GetContext added in v0.23.1

func (b *Browser) GetContext() context.Context

GetContext returns the current context

func (*Browser) HandleAuth added in v0.30.1

func (b *Browser) HandleAuth(username, password string)

HandleAuth for the next basic HTTP authentication. It will prevent the popup that requires user to input user name and password. Ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication

func (*Browser) HandleAuthE added in v0.30.1

func (b *Browser) HandleAuthE(username, password string) func() error

HandleAuthE for the next basic HTTP authentication. It will prevent the popup that requires user to input user name and password. Ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication

func (*Browser) HijackRequests added in v0.39.0

func (b *Browser) HijackRequests() *HijackRouter

HijackRequests creates a new router instance for requests hijacking. When use Fetch domain outside the router should be stopped. Enabling hijacking disables page caching, but such as 304 Not Modified will still work as expected.

func (*Browser) Incognito added in v0.23.1

func (b *Browser) Incognito() *Browser

Incognito creates a new incognito browser

func (*Browser) IncognitoE added in v0.23.1

func (b *Browser) IncognitoE() (*Browser, error)

IncognitoE creates a new incognito browser

func (*Browser) LoadState added in v0.38.0

func (b *Browser) LoadState(sessionID proto.TargetSessionID, method proto.Payload) (has bool)

LoadState into the method, seesionID can be empty.

func (*Browser) Page

func (b *Browser) Page(url string) *Page

Page creates a new tab

func (*Browser) PageE

func (b *Browser) PageE(url string) (*Page, error)

PageE doc is similar to the method Page

func (*Browser) PageFromTargetID added in v0.31.0

func (b *Browser) PageFromTargetID(targetID proto.TargetTargetID) *Page

PageFromTargetID creates a Page instance from a targetID

func (*Browser) PageFromTargetIDE added in v0.31.0

func (b *Browser) PageFromTargetIDE(targetID proto.TargetTargetID) (*Page, error)

PageFromTargetIDE creates a Page instance from a targetID

func (*Browser) Pages

func (b *Browser) Pages() Pages

Pages returns all visible pages

func (*Browser) PagesE

func (b *Browser) PagesE() (Pages, error)

PagesE doc is similar to the method Pages

func (*Browser) ServeMonitor added in v0.27.0

func (b *Browser) ServeMonitor(host string) *kit.ServerContext

ServeMonitor starts the monitor server The reason why not to use "chrome://inspect/#devices" is one target cannot be driven by multiple controllers.

func (*Browser) Slowmotion

func (b *Browser) Slowmotion(delay time.Duration) *Browser

Slowmotion set the delay for each control action, such as the simulation of the human inputs

func (*Browser) Timeout added in v0.7.2

func (b *Browser) Timeout(d time.Duration) *Browser

Timeout for chained sub-operations

func (*Browser) Trace

func (b *Browser) Trace(enable bool) *Browser

Trace enables/disables the visual tracing of the input actions on the page

func (*Browser) WaitEvent added in v0.7.2

func (b *Browser) WaitEvent(e proto.Payload) (wait func())

WaitEvent waits for the next event for one time. It will also load the data into the event object.

type Element

type Element struct {
	ObjectID proto.RuntimeRemoteObjectID
	// contains filtered or unexported fields
}

Element represents the DOM element

func (*Element) Box

func (el *Element) Box() *Box

Box returns the size of an element and its position relative to the main frame. It will recursively calculate the box with all ancestors. The spec is here: https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect

func (*Element) BoxE

func (el *Element) BoxE() (*Box, error)

BoxE doc is similar to the method Box

func (*Element) CallContext added in v0.29.0

func (el *Element) CallContext() (context.Context, proto.Client, string)

CallContext parameters for proto

func (*Element) CancelTimeout

func (el *Element) CancelTimeout() *Element

CancelTimeout context

func (*Element) Click

func (el *Element) Click() *Element

Click the element

func (*Element) ClickE

func (el *Element) ClickE(button proto.InputMouseButton) error

ClickE doc is similar to the method Click

func (*Element) Context added in v0.12.0

func (el *Element) Context(ctx context.Context) *Element

Context creates a clone with a context that inherits the previous one

func (*Element) Describe added in v0.7.0

func (el *Element) Describe() *proto.DOMNode

Describe returns the element info Returned json: https://chromedevtools.github.io/devtools-protocol/tot/DOM#type-Node

func (*Element) DescribeE added in v0.7.0

func (el *Element) DescribeE() (*proto.DOMNode, error)

DescribeE doc is similar to the method Describe

func (*Element) Element added in v0.5.0

func (el *Element) Element(selector string) *Element

Element returns the first child that matches the css selector

func (*Element) ElementByJS added in v0.5.0

func (el *Element) ElementByJS(js string, params ...interface{}) *Element

ElementByJS returns the element from the return value of the js

func (*Element) ElementByJSE added in v0.5.0

func (el *Element) ElementByJSE(js string, params Array) (*Element, error)

ElementByJSE doc is similar to the method ElementByJS

func (*Element) ElementE added in v0.5.0

func (el *Element) ElementE(selector string) (*Element, error)

ElementE doc is similar to the method Element

func (*Element) ElementMatches added in v0.9.0

func (el *Element) ElementMatches(selector, regex string) *Element

ElementMatches returns the first element in the page that matches the CSS selector and its text matches the regex. The regex is the js regex, not golang's.

func (*Element) ElementMatchesE added in v0.9.3

func (el *Element) ElementMatchesE(selector, regex string) (*Element, error)

ElementMatchesE doc is similar to the method ElementMatches

func (*Element) ElementX added in v0.9.0

func (el *Element) ElementX(xpath string) *Element

ElementX returns the first child that matches the XPath selector

func (*Element) ElementXE added in v0.9.0

func (el *Element) ElementXE(xpath string) (*Element, error)

ElementXE doc is similar to the method ElementX

func (*Element) Elements added in v0.5.0

func (el *Element) Elements(selector string) Elements

Elements returns all elements that match the css selector

func (*Element) ElementsByJS added in v0.5.0

func (el *Element) ElementsByJS(js string, params ...interface{}) Elements

ElementsByJS returns the elements from the return value of the js

func (*Element) ElementsByJSE added in v0.5.0

func (el *Element) ElementsByJSE(js string, params Array) (Elements, error)

ElementsByJSE doc is similar to the method ElementsByJS

func (*Element) ElementsE added in v0.5.0

func (el *Element) ElementsE(selector string) (Elements, error)

ElementsE doc is similar to the method Elements

func (*Element) ElementsX added in v0.9.0

func (el *Element) ElementsX(xpath string) Elements

ElementsX returns all elements that match the XPath selector

func (*Element) ElementsXE added in v0.9.0

func (el *Element) ElementsXE(xpath string) (Elements, error)

ElementsXE doc is similar to the method ElementsX

func (*Element) Eval

func (el *Element) Eval(js string, params ...interface{}) proto.JSON

Eval evaluates js function on the element, the first param must be a js function definition For example: el.Eval(`name => this.getAttribute(name)`, "value")

func (*Element) EvalE

func (el *Element) EvalE(byValue bool, js string, params Array) (*proto.RuntimeRemoteObject, error)

EvalE doc is similar to the method Eval

func (*Element) Focus added in v0.5.0

func (el *Element) Focus() *Element

Focus sets focus on the specified element

func (*Element) FocusE added in v0.5.0

func (el *Element) FocusE() error

FocusE doc is similar to the method Focus

func (*Element) Frame

func (el *Element) Frame() *Page

Frame creates a page instance that represents the iframe

func (*Element) FrameE

func (el *Element) FrameE() (*Page, error)

FrameE doc is similar to the method Frame

func (*Element) GetContext added in v0.23.1

func (el *Element) GetContext() context.Context

GetContext returns the current context

func (*Element) HTML

func (el *Element) HTML() string

HTML gets the outerHTML of the element

func (*Element) HTMLE

func (el *Element) HTMLE() (string, error)

HTMLE doc is similar to the method HTML

func (*Element) Has added in v0.24.3

func (el *Element) Has(selector string) bool

Has an element that matches the css selector

func (*Element) HasE added in v0.24.3

func (el *Element) HasE(selector string) (bool, error)

HasE doc is similar to the method Has

func (*Element) HasMatches added in v0.24.3

func (el *Element) HasMatches(selector, regex string) bool

HasMatches an element that matches the css selector and its text matches the regex.

func (*Element) HasMatchesE added in v0.24.3

func (el *Element) HasMatchesE(selector, regex string) (bool, error)

HasMatchesE doc is similar to the method HasMatches

func (*Element) HasX added in v0.24.3

func (el *Element) HasX(selector string) bool

HasX an element that matches the XPath selector

func (*Element) HasXE added in v0.24.3

func (el *Element) HasXE(selector string) (bool, error)

HasXE doc is similar to the method HasX

func (*Element) Input

func (el *Element) Input(text string) *Element

Input wll click the element and input the text. To empty the input you can use something like el.SelectAllText().Input("")

func (*Element) InputE

func (el *Element) InputE(text string) error

InputE doc is similar to the method Input

func (*Element) Next added in v0.5.0

func (el *Element) Next() *Element

Next returns the next sibling element

func (*Element) NextE added in v0.5.0

func (el *Element) NextE() (*Element, error)

NextE doc is similar to the method Next

func (*Element) Parent added in v0.5.0

func (el *Element) Parent() *Element

Parent returns the parent element

func (*Element) ParentE added in v0.5.0

func (el *Element) ParentE() (*Element, error)

ParentE doc is similar to the method Parent

func (*Element) Parents added in v0.25.2

func (el *Element) Parents(selector string) Elements

Parents that match the selector

func (*Element) ParentsE added in v0.25.2

func (el *Element) ParentsE(selector string) (Elements, error)

ParentsE that match the selector

func (*Element) Press

func (el *Element) Press(key rune) *Element

Press a key

func (*Element) PressE

func (el *Element) PressE(key rune) error

PressE doc is similar to the method Press

func (*Element) Previous added in v0.5.0

func (el *Element) Previous() *Element

Previous returns the previous sibling element

func (*Element) PreviousE added in v0.5.0

func (el *Element) PreviousE() (*Element, error)

PreviousE doc is similar to the method Previous

func (*Element) Release added in v0.12.0

func (el *Element) Release()

Release remote object on browser

func (*Element) ReleaseE added in v0.12.0

func (el *Element) ReleaseE() error

ReleaseE doc is similar to the method Release

func (*Element) Resource added in v0.10.0

func (el *Element) Resource() []byte

Resource returns the binary of the "src" properly, such as the image or audio file.

func (*Element) ResourceE added in v0.10.0

func (el *Element) ResourceE() ([]byte, error)

ResourceE doc is similar to the method Resource

func (*Element) Screenshot added in v0.22.0

func (el *Element) Screenshot(toFile ...string) []byte

Screenshot of the area of the element

func (*Element) ScreenshotE added in v0.22.0

func (el *Element) ScreenshotE(format proto.PageCaptureScreenshotFormat, quality int) ([]byte, error)

ScreenshotE of the area of the element

func (*Element) ScrollIntoView added in v0.27.3

func (el *Element) ScrollIntoView() *Element

ScrollIntoView scrolls the current element into the visible area of the browser window if it's not already within the visible area.

func (*Element) ScrollIntoViewE added in v0.27.3

func (el *Element) ScrollIntoViewE() error

ScrollIntoViewE doc is similar to the method ScrollIntoViewIfNeeded

func (*Element) Select

func (el *Element) Select(selectors ...string) *Element

Select the option elements that match the selectors, the selector can be text content or css selector

func (*Element) SelectAllText added in v0.16.4

func (el *Element) SelectAllText() *Element

SelectAllText selects all text

func (*Element) SelectAllTextE added in v0.16.8

func (el *Element) SelectAllTextE() error

SelectAllTextE doc is similar to the method SelectAllText

func (*Element) SelectE

func (el *Element) SelectE(selectors []string) error

SelectE doc is similar to the method Select

func (*Element) SelectText added in v0.16.4

func (el *Element) SelectText(regex string) *Element

SelectText selects the text that matches the regular expression

func (*Element) SelectTextE added in v0.16.4

func (el *Element) SelectTextE(regex string) error

SelectTextE doc is similar to the method SelectText

func (*Element) SetFiles added in v0.5.0

func (el *Element) SetFiles(paths ...string) *Element

SetFiles sets files for the given file input element

func (*Element) SetFilesE added in v0.5.0

func (el *Element) SetFilesE(paths []string) error

SetFilesE doc is similar to the method SetFiles

func (*Element) ShadowRoot added in v0.25.8

func (el *Element) ShadowRoot() *Element

ShadowRoot returns the shadow root of this element

func (*Element) ShadowRootE added in v0.25.8

func (el *Element) ShadowRootE() (*Element, error)

ShadowRootE returns the shadow root of this element

func (*Element) Text

func (el *Element) Text() string

Text gets the innerText of the element

func (*Element) TextE

func (el *Element) TextE() (string, error)

TextE doc is similar to the method Text

func (*Element) Timeout

func (el *Element) Timeout(d time.Duration) *Element

Timeout for chained sub-operations

func (*Element) Trace added in v0.9.0

func (el *Element) Trace(htmlMessage string) (removeOverlay func())

Trace with an overlay on the element

func (*Element) Visible added in v0.16.3

func (el *Element) Visible() bool

Visible returns true if the element is visible on the page

func (*Element) VisibleE added in v0.16.3

func (el *Element) VisibleE() (bool, error)

VisibleE doc is similar to the method Visible

func (*Element) Wait

func (el *Element) Wait(js string, params ...interface{}) *Element

Wait until the js returns true

func (*Element) WaitE

func (el *Element) WaitE(js string, params Array) error

WaitE doc is similar to the method Wait

func (*Element) WaitInvisible

func (el *Element) WaitInvisible() *Element

WaitInvisible until the element is not visible or removed

func (*Element) WaitInvisibleE

func (el *Element) WaitInvisibleE() error

WaitInvisibleE doc is similar to the method WaitInvisible

func (*Element) WaitStable added in v0.10.0

func (el *Element) WaitStable() *Element

WaitStable waits until the size and position are stable. Useful when waiting for the animation of modal or button to complete so that we can simulate the mouse to move to it and click on it.

func (*Element) WaitStableE added in v0.10.0

func (el *Element) WaitStableE(interval time.Duration) error

WaitStableE not using requestAnimation here because it can trigger to many checks, or miss checks for jQuery css animation.

func (*Element) WaitVisible

func (el *Element) WaitVisible() *Element

WaitVisible until the element is visible

func (*Element) WaitVisibleE

func (el *Element) WaitVisibleE() error

WaitVisibleE doc is similar to the method WaitVisible

type Elements added in v0.9.0

type Elements []*Element

Elements provides some helpers to deal with element list

func (Elements) Empty added in v0.9.3

func (els Elements) Empty() bool

Empty returns true if the list is empty

func (Elements) First added in v0.9.0

func (els Elements) First() *Element

First returns the first element, if the list is empty returns nil

func (Elements) Last added in v0.9.0

func (els Elements) Last() *Element

Last returns the last element, if the list is empty returns nil

type ErrCode added in v0.30.0

type ErrCode string

ErrCode for errors

const (
	// ErrExpectElement error code
	ErrExpectElement ErrCode = "expect js to return an element"
	// ErrExpectElements error code
	ErrExpectElements ErrCode = "expect js to return an array of elements"
	// ErrElementNotFound error code
	ErrElementNotFound ErrCode = "cannot find element"
	// ErrWaitJSTimeout error code
	ErrWaitJSTimeout ErrCode = "wait js timeout"
	// ErrSrcNotFound error code
	ErrSrcNotFound ErrCode = "element doesn't have src attribute"
	// ErrEval error code
	ErrEval ErrCode = "eval error"
	// ErrNavigation error code
	ErrNavigation ErrCode = "navigation failed"
)

type Error

type Error struct {
	Err     error
	Code    ErrCode
	Details interface{}
}

Error ...

func (*Error) Error

func (e *Error) Error() string

Error ...

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap ...

type Hijack added in v0.39.0

type Hijack struct {
	Request  *HijackRequest
	Response *HijackResponse
	OnError  func(error)

	// Skip to next handler
	Skip bool
}

Hijack context

func (*Hijack) LoadResponse added in v0.39.0

func (h *Hijack) LoadResponse()

LoadResponse will send request to the real destination and load the response as default response to override.

func (*Hijack) LoadResponseE added in v0.39.0

func (h *Hijack) LoadResponseE() error

LoadResponseE will send request to the real destination and load the response as default response to override.

type HijackRequest added in v0.39.0

type HijackRequest struct {
	// contains filtered or unexported fields
}

HijackRequest context

func (*HijackRequest) Body added in v0.39.0

func (ctx *HijackRequest) Body() string

Body of the request, devtools API doesn't support binary data yet, only string can be captured.

func (*HijackRequest) Header added in v0.39.0

func (ctx *HijackRequest) Header(key string) string

Header via a key

func (*HijackRequest) Headers added in v0.39.0

func (ctx *HijackRequest) Headers() proto.NetworkHeaders

Headers of request

func (*HijackRequest) JSONBody added in v0.39.0

func (ctx *HijackRequest) JSONBody() gjson.Result

JSONBody of the request

func (*HijackRequest) Method added in v0.39.0

func (ctx *HijackRequest) Method() string

Method of the request

func (*HijackRequest) SetBody added in v0.39.0

func (ctx *HijackRequest) SetBody(obj interface{})

SetBody of the request, if obj is []byte or string, raw body will be used, else it will be encoded as json.

func (*HijackRequest) SetHeader added in v0.39.0

func (ctx *HijackRequest) SetHeader(pairs ...string)

SetHeader via key-value pairs

func (*HijackRequest) SetMethod added in v0.39.0

func (ctx *HijackRequest) SetMethod(name string)

SetMethod of request

func (*HijackRequest) SetQuery added in v0.39.0

func (ctx *HijackRequest) SetQuery(pairs ...interface{})

SetQuery of the request, example Query(k, v, k, v ...)

func (*HijackRequest) SetURL added in v0.39.0

func (ctx *HijackRequest) SetURL(url string)

SetURL of the request

func (*HijackRequest) URL added in v0.39.0

func (ctx *HijackRequest) URL() *url.URL

URL of the request

type HijackResponse added in v0.39.0

type HijackResponse struct {
	// contains filtered or unexported fields
}

HijackResponse context

func (*HijackResponse) Body added in v0.39.0

func (ctx *HijackResponse) Body() []byte

Body of response

func (*HijackResponse) BodyE added in v0.39.0

func (ctx *HijackResponse) BodyE() ([]byte, error)

BodyE of response

func (*HijackResponse) Header added in v0.39.0

func (ctx *HijackResponse) Header(key string) string

Header via key

func (*HijackResponse) HeaderE added in v0.39.0

func (ctx *HijackResponse) HeaderE(key string) (string, error)

HeaderE via key

func (*HijackResponse) Headers added in v0.39.0

func (ctx *HijackResponse) Headers() http.Header

Headers of request

func (*HijackResponse) HeadersE added in v0.39.0

func (ctx *HijackResponse) HeadersE() (http.Header, error)

HeadersE of request

func (*HijackResponse) JSONBody added in v0.39.0

func (ctx *HijackResponse) JSONBody() gjson.Result

JSONBody of response

func (*HijackResponse) SetBody added in v0.39.0

func (ctx *HijackResponse) SetBody(obj interface{})

SetBody of response, if obj is []byte, raw body will be used, else it will be encoded as json

func (*HijackResponse) SetHeader added in v0.39.0

func (ctx *HijackResponse) SetHeader(pairs ...string)

SetHeader via key-value pairs

func (*HijackResponse) SetStatusCode added in v0.39.0

func (ctx *HijackResponse) SetStatusCode(code int)

SetStatusCode of response

func (*HijackResponse) StatusCode added in v0.39.0

func (ctx *HijackResponse) StatusCode() int

StatusCode of response

func (*HijackResponse) StatusCodeE added in v0.39.0

func (ctx *HijackResponse) StatusCodeE() (int, error)

StatusCodeE of response

func (*HijackResponse) StringBody added in v0.39.0

func (ctx *HijackResponse) StringBody() string

StringBody of response

type HijackRouter added in v0.39.0

type HijackRouter struct {
	// contains filtered or unexported fields
}

HijackRouter context

func (*HijackRouter) Add added in v0.39.0

func (r *HijackRouter) Add(pattern string, handler func(*Hijack))

Add a hijack handler to router, the doc of the pattern is the same as "proto.FetchRequestPattern.URLPattern". You can add new handler even after the "Run" is called.

func (*HijackRouter) AddE added in v0.39.0

func (r *HijackRouter) AddE(pattern string, handler func(*Hijack)) error

AddE a hijack handler to router, the doc of the pattern is the same as "proto.FetchRequestPattern.URLPattern". You can add new handler even after the "Run" is called.

func (*HijackRouter) Remove added in v0.39.0

func (r *HijackRouter) Remove(pattern string)

Remove handler via the pattern

func (*HijackRouter) RemoveE added in v0.39.0

func (r *HijackRouter) RemoveE(pattern string) error

RemoveE handler via the pattern

func (*HijackRouter) Run added in v0.39.0

func (r *HijackRouter) Run()

Run the router, after you call it, you shouldn't add new handler to it.

func (*HijackRouter) Stop added in v0.39.0

func (r *HijackRouter) Stop()

Stop the router

func (*HijackRouter) StopE added in v0.39.0

func (r *HijackRouter) StopE() error

StopE the router

type Keyboard

type Keyboard struct {
	// contains filtered or unexported fields
}

Keyboard represents the keyboard on a page, it's always related the main frame

func (*Keyboard) Down

func (k *Keyboard) Down(key rune)

Down holds key down

func (*Keyboard) DownE

func (k *Keyboard) DownE(key rune) error

DownE doc is similar to the method Down

func (*Keyboard) InsertText added in v0.5.0

func (k *Keyboard) InsertText(text string)

InsertText like paste text into the page

func (*Keyboard) InsertTextE added in v0.5.0

func (k *Keyboard) InsertTextE(text string) error

InsertTextE doc is similar to the method InsertText

func (*Keyboard) Press

func (k *Keyboard) Press(key rune)

Press a key

func (*Keyboard) PressE

func (k *Keyboard) PressE(key rune) error

PressE doc is similar to the method Press

func (*Keyboard) Up

func (k *Keyboard) Up(key rune)

Up releases the key

func (*Keyboard) UpE

func (k *Keyboard) UpE(key rune) error

UpE doc is similar to the method Up

type Mouse

type Mouse struct {
	// contains filtered or unexported fields
}

Mouse represents the mouse on a page, it's always related the main frame

func (*Mouse) Click

func (m *Mouse) Click(button proto.InputMouseButton)

Click will press then release the button

func (*Mouse) ClickE

func (m *Mouse) ClickE(button proto.InputMouseButton) error

ClickE doc is similar to the method Click

func (*Mouse) Down

func (m *Mouse) Down(button proto.InputMouseButton)

Down holds the button down

func (*Mouse) DownE

func (m *Mouse) DownE(button proto.InputMouseButton, clicks int64) error

DownE doc is similar to the method Down

func (*Mouse) Move

func (m *Mouse) Move(x, y float64)

Move to the absolute position

func (*Mouse) MoveE added in v0.6.0

func (m *Mouse) MoveE(x, y float64, steps int) error

MoveE to the absolute position with specified steps

func (*Mouse) Scroll added in v0.10.0

func (m *Mouse) Scroll(x, y float64)

Scroll with the relative offset

func (*Mouse) ScrollE added in v0.10.0

func (m *Mouse) ScrollE(offsetX, offsetY float64, steps int) error

ScrollE the relative offset with specified steps

func (*Mouse) Up

func (m *Mouse) Up(button proto.InputMouseButton)

Up release the button

func (*Mouse) UpE

func (m *Mouse) UpE(button proto.InputMouseButton, clicks int64) error

UpE doc is similar to the method Up

type Page

type Page struct {
	TargetID  proto.TargetTargetID
	SessionID proto.TargetSessionID
	FrameID   proto.PageFrameID

	// devices
	Mouse    *Mouse
	Keyboard *Keyboard
	// contains filtered or unexported fields
}

Page represents the webpage

func (*Page) AddScriptTag added in v0.24.0

func (p *Page) AddScriptTag(url string) *Page

AddScriptTag to page. If url is empty, content will be used.

func (*Page) AddScriptTagE added in v0.24.0

func (p *Page) AddScriptTagE(url, content string) error

AddScriptTagE to page. If url is empty, content will be used.

func (*Page) AddStyleTag added in v0.24.0

func (p *Page) AddStyleTag(url string) *Page

AddStyleTag to page. If url is empty, content will be used.

func (*Page) AddStyleTagE added in v0.24.0

func (p *Page) AddStyleTagE(url, content string) error

AddStyleTagE to page. If url is empty, content will be used.

func (*Page) CallContext added in v0.29.0

func (p *Page) CallContext() (context.Context, proto.Client, string)

CallContext parameters for proto

func (*Page) CancelTimeout

func (p *Page) CancelTimeout() *Page

CancelTimeout context

func (*Page) Close

func (p *Page) Close()

Close page

func (*Page) CloseE

func (p *Page) CloseE() error

CloseE page

func (*Page) Context added in v0.12.0

func (p *Page) Context(ctx context.Context) *Page

Context creates a clone with a context that inherits the previous one

func (*Page) Cookies added in v0.27.3

func (p *Page) Cookies(urls ...string) []*proto.NetworkCookie

Cookies returns the page cookies. By default it will return the cookies for current page. The urls is the list of URLs for which applicable cookies will be fetched.

func (*Page) CookiesE added in v0.27.3

func (p *Page) CookiesE(urls []string) ([]*proto.NetworkCookie, error)

CookiesE returns the page cookies. By default it will return the cookies for current page. The urls is the list of URLs for which applicable cookies will be fetched.

func (*Page) DisableDomain added in v0.39.1

func (p *Page) DisableDomain(method proto.Payload) (recover func())

DisableDomain and returns a recover function to restore previous state

func (*Page) EachEvent added in v0.31.1

func (p *Page) EachEvent(fn interface{}) (wait func())

EachEvent of the specified event type, if the fn returns true the event loop will stop. The fn can accpet multiple events, such as EachEventE(func(e1 *proto.PageLoadEventFired, e2 *proto.PageLifecycleEvent) {}), only one argument will be non-null, others will null.

func (*Page) Element

func (p *Page) Element(selector string) *Element

Element retries until returns the first element in the page that matches the CSS selector

func (*Page) ElementByJS added in v0.4.4

func (p *Page) ElementByJS(js string, params ...interface{}) *Element

ElementByJS retries until returns the element from the return value of the js function

func (*Page) ElementByJSE added in v0.4.4

func (p *Page) ElementByJSE(sleeper kit.Sleeper, thisID proto.RuntimeRemoteObjectID, js string, params Array) (*Element, error)

ElementByJSE returns the element from the return value of the js function. sleeper is used to sleep before retry the operation. If sleeper is nil, no retry will be performed. thisID is the this value of the js function, when thisID is "", the this context will be the "window". If the js function returns "null", ElementByJSE will retry, you can use custom sleeper to make it only retry once.

func (*Page) ElementE

func (p *Page) ElementE(sleeper kit.Sleeper, objectID proto.RuntimeRemoteObjectID, selector string) (*Element, error)

ElementE finds element by css selector

func (*Page) ElementFromObjectID added in v0.34.0

func (p *Page) ElementFromObjectID(id proto.RuntimeRemoteObjectID) *Element

ElementFromObjectID creates an Element from the remote object id.

func (*Page) ElementMatches added in v0.9.0

func (p *Page) ElementMatches(selector, regex string) *Element

ElementMatches retries until returns the first element in the page that matches the CSS selector and its text matches the regex. The regex is the js regex, not golang's.

func (*Page) ElementMatchesE added in v0.9.3

func (p *Page) ElementMatchesE(sleeper kit.Sleeper, objectID proto.RuntimeRemoteObjectID, selector, regex string) (*Element, error)

ElementMatchesE doc is similar to the method ElementMatches

func (*Page) ElementX added in v0.9.0

func (p *Page) ElementX(xpath string) *Element

ElementX retries until returns the first element in the page that matches the XPath selector

func (*Page) ElementXE added in v0.9.0

func (p *Page) ElementXE(sleeper kit.Sleeper, objectID proto.RuntimeRemoteObjectID, xpath string) (*Element, error)

ElementXE finds elements by XPath

func (*Page) Elements

func (p *Page) Elements(selector string) Elements

Elements returns all elements that match the css selector

func (*Page) ElementsByJS added in v0.4.4

func (p *Page) ElementsByJS(js string, params ...interface{}) Elements

ElementsByJS returns the elements from the return value of the js

func (*Page) ElementsByJSE added in v0.4.4

func (p *Page) ElementsByJSE(thisID proto.RuntimeRemoteObjectID, js string, params Array) (Elements, error)

ElementsByJSE is different from ElementByJSE, it doesn't do retry

func (*Page) ElementsE

func (p *Page) ElementsE(objectID proto.RuntimeRemoteObjectID, selector string) (Elements, error)

ElementsE doc is similar to the method Elements

func (*Page) ElementsX added in v0.9.0

func (p *Page) ElementsX(xpath string) Elements

ElementsX returns all elements that match the XPath selector

func (*Page) ElementsXE added in v0.9.0

func (p *Page) ElementsXE(objectID proto.RuntimeRemoteObjectID, xpath string) (Elements, error)

ElementsXE doc is similar to the method ElementsX

func (*Page) EnableDomain added in v0.38.0

func (p *Page) EnableDomain(method proto.Payload) (recover func())

EnableDomain and returns a recover function to restore previous state

func (*Page) Eval

func (p *Page) Eval(js string, params ...interface{}) proto.JSON

Eval js on the page. The first param must be a js function definition. For example page.Eval(`n => n + 1`, 1) will return 2

func (*Page) EvalE

func (p *Page) EvalE(byValue bool, thisID proto.RuntimeRemoteObjectID, js string, jsArgs Array) (*proto.RuntimeRemoteObject, error)

EvalE thisID is the remote objectID that will be the this of the js function, if it's empty "window" will be used. Set the byValue to true to reduce memory occupation.

func (*Page) GetContext added in v0.23.1

func (p *Page) GetContext() context.Context

GetContext returns the current context

func (*Page) GetDownloadFile added in v0.6.0

func (p *Page) GetDownloadFile(pattern string) func() []byte

GetDownloadFile of the next download url that matches the pattern, returns the file content.

func (*Page) GetDownloadFileE added in v0.6.0

func (p *Page) GetDownloadFileE(pattern string) func() ([]byte, error)

GetDownloadFileE of the next download url that matches the pattern, returns the file content. The handler will be used once and removed.

func (*Page) GetWindow added in v0.13.5

func (p *Page) GetWindow() *proto.BrowserBounds

GetWindow get window bounds

func (*Page) GetWindowE added in v0.13.5

func (p *Page) GetWindowE() (*proto.BrowserBounds, error)

GetWindowE doc is similar to the method GetWindow

func (*Page) HandleDialog added in v0.4.5

func (p *Page) HandleDialog(accept bool, promptText string) (wait func())

HandleDialog accepts or dismisses next JavaScript initiated dialog (alert, confirm, prompt, or onbeforeunload)

func (*Page) HandleDialogE added in v0.4.5

func (p *Page) HandleDialogE(accept bool, promptText string) func() error

HandleDialogE doc is similar to the method HandleDialog

func (*Page) Has

func (p *Page) Has(selector string) bool

Has an element that matches the css selector

func (*Page) HasE

func (p *Page) HasE(selector string) (bool, error)

HasE doc is similar to the method Has

func (*Page) HasMatches added in v0.9.3

func (p *Page) HasMatches(selector, regex string) bool

HasMatches an element that matches the css selector and its text matches the regex.

func (*Page) HasMatchesE added in v0.9.3

func (p *Page) HasMatchesE(selector, regex string) (bool, error)

HasMatchesE doc is similar to the method HasMatches

func (*Page) HasX added in v0.9.3

func (p *Page) HasX(selector string) bool

HasX an element that matches the XPath selector

func (*Page) HasXE added in v0.9.3

func (p *Page) HasXE(selector string) (bool, error)

HasXE doc is similar to the method HasX

func (*Page) HijackRequests added in v0.39.0

func (p *Page) HijackRequests() *HijackRouter

HijackRequests same as Browser.HijackRequests, but scoped with the page

func (*Page) IsIframe added in v0.11.0

func (p *Page) IsIframe() bool

IsIframe tells if it's iframe

func (*Page) LoadState added in v0.38.0

func (p *Page) LoadState(method proto.Payload) (has bool)

LoadState into the method.

func (*Page) Navigate

func (p *Page) Navigate(url string) *Page

Navigate to url

func (*Page) NavigateE

func (p *Page) NavigateE(url string) error

NavigateE doc is similar to the method Navigate

func (*Page) ObjectToJSON added in v0.39.0

func (p *Page) ObjectToJSON(obj *proto.RuntimeRemoteObject) proto.JSON

ObjectToJSON by remote object

func (*Page) ObjectToJSONE added in v0.39.0

func (p *Page) ObjectToJSONE(obj *proto.RuntimeRemoteObject) (proto.JSON, error)

ObjectToJSONE by object id

func (*Page) ObjectsToJSON added in v0.39.0

func (p *Page) ObjectsToJSON(list []*proto.RuntimeRemoteObject) proto.JSON

ObjectsToJSON by remote objects

func (*Page) Overlay added in v0.9.0

func (p *Page) Overlay(left, top, width, height float64, msg string) (remove func())

Overlay a rectangle on the main frame with specified message

func (*Page) PDF added in v0.25.2

func (p *Page) PDF() []byte

PDF prints page as PDF

func (*Page) PDFE added in v0.25.2

func (p *Page) PDFE(req *proto.PagePrintToPDF) ([]byte, error)

PDFE prints page as PDF

func (*Page) Pause added in v0.7.1

func (p *Page) Pause() *Page

Pause stops on the next JavaScript statement

func (*Page) PauseE added in v0.7.1

func (p *Page) PauseE() error

PauseE doc is similar to the method Pause

func (*Page) Release added in v0.12.0

func (p *Page) Release(objectID proto.RuntimeRemoteObjectID) *Page

Release remote object

func (*Page) ReleaseE added in v0.12.0

func (p *Page) ReleaseE(objectID proto.RuntimeRemoteObjectID) error

ReleaseE doc is similar to the method Release

func (*Page) Root added in v0.11.0

func (p *Page) Root() *Page

Root page of the iframe, if it's not a iframe returns itself

func (*Page) Screenshot added in v0.10.0

func (p *Page) Screenshot(toFile ...string) []byte

Screenshot the page and returns the binary of the image If the toFile is "", it will save output to "tmp/screenshots" folder, time as the file name.

func (*Page) ScreenshotE added in v0.10.0

func (p *Page) ScreenshotE(fullpage bool, req *proto.PageCaptureScreenshot) ([]byte, error)

ScreenshotE options: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-captureScreenshot

func (*Page) ScreenshotFullPage added in v0.34.2

func (p *Page) ScreenshotFullPage(toFile ...string) []byte

ScreenshotFullPage including all scrollable content and returns the binary of the image.

func (*Page) SetCookies added in v0.26.1

func (p *Page) SetCookies(cookies ...*proto.NetworkCookieParam) *Page

SetCookies of the page. Cookie format: https://chromedevtools.github.io/devtools-protocol/tot/Network#method-setCookie

func (*Page) SetCookiesE added in v0.26.1

func (p *Page) SetCookiesE(cookies []*proto.NetworkCookieParam) error

SetCookiesE of the page. Cookie format: https://chromedevtools.github.io/devtools-protocol/tot/Network#method-setCookie

func (*Page) SetExtraHeaders added in v0.29.1

func (p *Page) SetExtraHeaders(dict ...string) (cleanup func())

SetExtraHeaders whether to always send extra HTTP headers with the requests from this page. The arguments are key-value pairs, you can set multiple key-value pairs at the same time.

func (*Page) SetExtraHeadersE added in v0.29.1

func (p *Page) SetExtraHeadersE(dict []string) (func(), error)

SetExtraHeadersE whether to always send extra HTTP headers with the requests from this page.

func (*Page) SetUserAgent added in v0.29.2

func (p *Page) SetUserAgent(req *proto.NetworkSetUserAgentOverride) *Page

SetUserAgent Allows overriding user agent with the given string.

func (*Page) SetUserAgentE added in v0.29.2

func (p *Page) SetUserAgentE(req *proto.NetworkSetUserAgentOverride) error

SetUserAgentE Allows overriding user agent with the given string.

func (*Page) Sleeper added in v0.8.0

func (p *Page) Sleeper() kit.Sleeper

Sleeper returns the default sleeper for retry, it uses backoff and requestIdleCallback to wait

func (*Page) StopLoading added in v0.29.4

func (p *Page) StopLoading() *Page

StopLoading forces the page stop all navigations and pending resource fetches.

func (*Page) StopLoadingE added in v0.29.4

func (p *Page) StopLoadingE() error

StopLoadingE forces the page stop navigation and pending resource fetches.

func (*Page) Timeout

func (p *Page) Timeout(d time.Duration) *Page

Timeout for chained sub-operations

func (*Page) Viewport added in v0.13.5

func (p *Page) Viewport(width, height int64, deviceScaleFactor float64, mobile bool) *Page

Viewport overrides the values of device screen dimensions.

func (*Page) ViewportE added in v0.13.5

func (p *Page) ViewportE(params *proto.EmulationSetDeviceMetricsOverride) error

ViewportE doc is similar to the method Viewport

func (*Page) Wait added in v0.39.0

func (p *Page) Wait(js string, params ...interface{})

Wait js function until it returns true

func (*Page) WaitE added in v0.39.0

func (p *Page) WaitE(sleeper kit.Sleeper, thisID proto.RuntimeRemoteObjectID, js string, params Array) error

WaitE js function until it returns true

func (*Page) WaitEvent added in v0.5.0

func (p *Page) WaitEvent(e proto.Payload) (wait func())

WaitEvent waits for the next event for one time. It will also load the data into the event object.

func (*Page) WaitIdle added in v0.16.1

func (p *Page) WaitIdle() *Page

WaitIdle wait until the next window.requestIdleCallback is called.

func (*Page) WaitIdleE added in v0.16.1

func (p *Page) WaitIdleE(timeout time.Duration) (err error)

WaitIdleE doc is similar to the method WaitIdle

func (*Page) WaitLoad added in v0.13.2

func (p *Page) WaitLoad() *Page

WaitLoad wait until the `window.onload` is complete, resolve immediately if already fired.

func (*Page) WaitLoadE added in v0.13.2

func (p *Page) WaitLoadE() error

WaitLoadE doc is similar to the method WaitLoad

func (*Page) WaitOpen added in v0.37.0

func (p *Page) WaitOpen() (wait func() *Page)

WaitOpen to be created from a new window

func (*Page) WaitOpenE added in v0.37.0

func (p *Page) WaitOpenE() func() (*Page, error)

WaitOpenE doc is similar to the method WaitPage

func (*Page) WaitRequestIdle added in v0.16.3

func (p *Page) WaitRequestIdle(excludes ...string) (wait func())

WaitRequestIdle returns a wait function that waits until the page doesn't send request for 300ms. You can pass regular expressions to exclude the requests by their url.

func (*Page) WaitRequestIdleE added in v0.16.3

func (p *Page) WaitRequestIdleE(d time.Duration, includes, excludes []string) func()

WaitRequestIdleE returns a wait function that waits until no request for d duration. Use the includes and excludes regexp list to filter the requests by their url. Such as set n to 1 if there's a polling request.

func (*Page) Window added in v0.13.5

func (p *Page) Window(left, top, width, height int64) *Page

Window set the window location and size

func (*Page) WindowFullscreen added in v0.19.0

func (p *Page) WindowFullscreen() *Page

WindowFullscreen the window

func (*Page) WindowMaximize added in v0.19.0

func (p *Page) WindowMaximize() *Page

WindowMaximize the window

func (*Page) WindowMinimize added in v0.19.0

func (p *Page) WindowMinimize() *Page

WindowMinimize the window

func (*Page) WindowNormal added in v0.19.0

func (p *Page) WindowNormal() *Page

WindowNormal the window size

type Pages added in v0.22.2

type Pages []*Page

Pages provides some helpers to deal with page list

func (Pages) Find added in v0.22.2

func (ps Pages) Find(selector string) *Page

Find the page that has the specified element with the css selector

func (Pages) FindByURL added in v0.22.2

func (ps Pages) FindByURL(regex string) *Page

FindByURL returns the page that has the url that matches the regex

func (Pages) FindByURLE added in v0.34.2

func (ps Pages) FindByURLE(regex string) (*Page, error)

FindByURLE returns the page that has the url that matches the regex

Directories

Path Synopsis
fixtures
lib
cdp
defaults
Package defaults holds some commonly used options parsed from env var "rod".
Package defaults holds some commonly used options parsed from env var "rod".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL