Documentation ¶
Overview ¶
Package rod is a high-level driver directly based on DevTools Protocol.
Example ¶
This example opens https://github.com/, searches for "git", and then gets the header element which gives the description for Git.
package main import ( "fmt" "github.com/go-rod/rod" "github.com/go-rod/rod/lib/input" ) func main() { // Launch a new browser with default options, and connect to it. browser := rod.New().MustConnect() // Even you forget to close, rod will close it after main process ends. defer browser.MustClose() // Create a new page page := browser.MustPage("https://github.com") // We use css selector to get the search input element and input "git" page.MustElement("input").MustInput("git").MustType(input.Enter) // Wait until css selector get the element then get the text content of it. text := page.MustElement(".codesearch-results p").MustText() fmt.Println(text) // Get all input elements. Rod supports query elements by css selector, xpath, and regex. // For more detailed usage, check the query_test.go file. fmt.Println("Found", len(page.MustElements("input")), "input elements") // Eval js on the page page.MustEval(`() => console.log("hello world")`) // Pass parameters as json objects to the js function. This MustEval will result 3 fmt.Println("1 + 2 =", page.MustEval(`(a, b) => a + b`, 1, 2).Int()) // When eval on an element, "this" in the js is the current DOM element. fmt.Println(page.MustElement("title").MustEval(`() => this.innerText`).String()) }
Output: Git is the most widely used version control system. Found 5 input elements 1 + 2 = 3 Search · git · GitHub
Example (Context_and_EachEvent) ¶
package main import ( "context" "fmt" "time" "github.com/go-rod/rod" "github.com/go-rod/rod/lib/proto" ) func main() { browser := rod.New().MustConnect() defer browser.MustClose() page := browser.MustPage("https://github.com").MustWaitLoad() page, cancel := page.WithCancel() go func() { time.Sleep(time.Second) cancel() }() // It's a blocking method, it will wait until the context is cancelled page.EachEvent(func(e *proto.PageLifecycleEvent) {})() if page.GetContext().Err() == context.Canceled { fmt.Println("cancelled") } }
Output:
Example (Context_and_timeout) ¶
Rod use https://golang.org/pkg/context to handle cancellations for IO blocking operations, most times it's timeout. Context will be recursively passed to all sub-methods. For example, methods like Page.Context(ctx) will return a clone of the page with the ctx, all the methods of the returned page will use the ctx if they have IO blocking operations. Page.Timeout or Page.WithCancel is just a shortcut for Page.Context. Of course, Browser or Element works the same way.
package main import ( "math/rand" "time" "github.com/go-rod/rod" ) func main() { page := rod.New().MustConnect().MustPage("https://github.com") page. // Set a 5-second timeout for all chained methods Timeout(5 * time.Second). // The total time for MustWaitLoad and MustElement must be less than 5 seconds MustWaitLoad(). MustElement("title"). // Methods after CancelTimeout won't be affected by the 5-second timeout CancelTimeout(). // Set a 10-second timeout for all chained methods Timeout(10 * time.Second). // Panics if it takes more than 10 seconds MustText() // The two code blocks below are basically the same: { page.Timeout(5 * time.Second).MustElement("a").CancelTimeout() } { // Use this way you can customize your own way to cancel long-running task page, cancel := page.WithCancel() go func() { time.Sleep(time.Duration(rand.Int())) // cancel after randomly time cancel() }() page.MustElement("a") } }
Output:
Example (Customize_browser_launch) ¶
Shows how we can further customize the browser with the launcher library. Usually you use launcher lib to set the browser's command line flags (switches). Doc for flags: https://peter.sh/experiments/chromium-command-line-switches
package main import ( "fmt" "github.com/go-rod/rod" "github.com/go-rod/rod/lib/launcher" ) func main() { url := launcher.New(). Proxy("127.0.0.1:8080"). // set flag "--proxy-server=127.0.0.1:8080" Delete("use-mock-keychain"). // delete flag "--use-mock-keychain" MustLaunch() browser := rod.New().ControlURL(url).MustConnect() defer browser.MustClose() // So that we don't have to self issue certs for MITM browser.MustIgnoreCertErrors(true) // Adding authentication to the proxy, for the next auth request. // We use CLI tool "mitmproxy --proxyauth user:pass" as an example. go browser.MustHandleAuth("user", "pass")() // mitmproxy needs a cert config to support https. We use http here instead, // for example fmt.Println(browser.MustPage("https://mdn.dev/").MustElement("title").MustText()) }
Output:
Example (Customize_retry_strategy) ¶
Shows how to change the retry/polling options that is used to query elements. This is useful when you want to customize the element query retry logic.
package main import ( "context" "errors" "fmt" "time" "github.com/go-rod/rod" "github.com/go-rod/rod/lib/utils" ) func main() { browser := rod.New().MustConnect() defer browser.MustClose() page := browser.MustPage("https://github.com") // sleep for 0.5 seconds before every retry sleeper := func() utils.Sleeper { return func(context.Context) error { time.Sleep(time.Second / 2) return nil } } el, _ := page.Sleeper(sleeper).Element("input") fmt.Println(el.MustProperty("name")) // If sleeper is nil page.ElementE will query without retrying. // If nothing found it will return an error. el, err := page.Sleeper(rod.NotFoundSleeper).Element("input") if errors.Is(err, &rod.ErrElementNotFound{}) { fmt.Println("element not found") } else if err != nil { panic(err) } fmt.Println(el.MustProperty("name")) }
Output: q q
Example (Direct_cdp) ¶
When rod doesn't have a feature that you need. You can easily call the cdp to achieve it. List of cdp API: https://github.com/go-rod/rod/tree/main/lib/proto
package main import ( "context" "github.com/go-rod/rod" "github.com/go-rod/rod/lib/proto" ) func main() { page := rod.New().MustConnect().MustPage() // Rod doesn't have a method to enable AD blocking, // but you can call cdp interface directly to achieve it. // The two code blocks below are equal to enable AD blocking { _ = proto.PageSetAdBlockingEnabled{ Enabled: true, }.Call(page) } { // Interact with the cdp JSON API directly _, _ = page.Call(context.TODO(), "", "Page.setAdBlockingEnabled", map[string]bool{ "enabled": true, }) } }
Output:
Example (Disable_headless_to_debug) ¶
Shows how to disable headless mode and debug. Rod provides a lot of debug options, you can set them with setter methods or use environment variables. Doc for environment variables: https://pkg.go.dev/github.com/go-rod/rod/lib/defaults
package main import ( "fmt" "time" "github.com/go-rod/rod" "github.com/go-rod/rod/lib/input" "github.com/go-rod/rod/lib/launcher" "github.com/go-rod/rod/lib/utils" ) func main() { // Headless runs the browser on foreground, you can also use flag "-rod=show" // Devtools opens the tab in each new tab opened automatically l := launcher.New(). Headless(false). Devtools(true) defer l.Cleanup() // remove launcher.FlagUserDataDir url := l.MustLaunch() // Trace shows verbose debug information for each action executed // SlowMotion is a debug related function that waits 2 seconds between // each action, making it easier to inspect what your code is doing. browser := rod.New(). ControlURL(url). Trace(true). SlowMotion(2 * time.Second). MustConnect() // ServeMonitor plays screenshots of each tab. This feature is extremely // useful when debugging with headless mode. // You can also enable it with flag "-rod=monitor" launcher.Open(browser.ServeMonitor("")) defer browser.MustClose() page := browser.MustPage("https://github.com/") page.MustElement("input").MustInput("git").MustType(input.Enter) text := page.MustElement(".codesearch-results p").MustText() fmt.Println(text) utils.Pause() // pause goroutine }
Output:
Example (Download_file) ¶
package main import ( "github.com/go-rod/rod" "github.com/go-rod/rod/lib/utils" ) func main() { browser := rod.New().MustConnect() page := browser.MustPage("https://file-examples.com/index.php/sample-documents-download/sample-pdf-download/") wait := browser.MustWaitDownload() page.MustElementR("a", "DOWNLOAD SAMPLE PDF FILE").MustClick() _ = utils.OutputFile("t.pdf", wait()) }
Output:
Example (Error_handling) ¶
We use "Must" prefixed functions to write example code. But in production you may want to use the no-prefix version of them. About why we use "Must" as the prefix, it's similar to https://golang.org/pkg/regexp/#MustCompile
package main import ( "context" "errors" "fmt" "github.com/go-rod/rod" ) func main() { page := rod.New().MustConnect().MustPage("https://mdn.dev") // We use Go's standard way to check error types, no magic. check := func(err error) { var evalErr *rod.ErrEval if errors.Is(err, context.DeadlineExceeded) { // timeout error fmt.Println("timeout err") } else if errors.As(err, &evalErr) { // eval error fmt.Println(evalErr.LineNumber) } else if err != nil { fmt.Println("can't handle", err) } } // The two code blocks below are doing the same thing in two styles: // The block below is better for debugging or quick scripting. We use panic to short-circuit logics. // So that we can take advantage of fluent interface (https://en.wikipedia.org/wiki/Fluent_interface) // and fail-fast (https://en.wikipedia.org/wiki/Fail-fast). // This style will reduce code, but it may also catch extra errors (less consistent and precise). { err := rod.Try(func() { fmt.Println(page.MustElement("a").MustHTML()) // use "Must" prefixed functions }) check(err) } // The block below is better for production code. It's the standard way to handle errors. // Usually, this style is more consistent and precise. { el, err := page.Element("a") if err != nil { check(err) return } html, err := el.HTML() if err != nil { check(err) return } fmt.Println(html) } }
Output:
Example (Eval_reuse_remote_object) ¶
Shows how to share a remote object reference between two Eval
package main import ( "fmt" "github.com/go-rod/rod" ) func main() { page := rod.New().MustConnect().MustPage() fn := page.MustEvaluate(rod.Eval(`() => Math.random`).ByObject()) res := page.MustEval(`f => f()`, fn) // print a random number fmt.Println(res.Num()) }
Output:
Example (Handle_events) ¶
Shows how to listen for events.
package main import ( "fmt" "github.com/go-rod/rod" "github.com/go-rod/rod/lib/proto" ) func main() { browser := rod.New().MustConnect() defer browser.MustClose() page := browser.MustPage() done := make(chan struct{}) // Listen for all events of console output. go page.EachEvent(func(e *proto.RuntimeConsoleAPICalled) { fmt.Println(page.MustObjectsToJSON(e.Args)) close(done) })() wait := page.WaitEvent(&proto.PageLoadEventFired{}) page.MustNavigate("https://mdn.dev") wait() // EachEvent allows us to achieve the same functionality as above. if false { // Subscribe events before they happen, run the "wait()" to start consuming // the events. We can return an optional stop signal to unsubscribe events. wait := page.EachEvent(func(e *proto.PageLoadEventFired) (stop bool) { return true }) page.MustNavigate("https://mdn.dev") wait() } // Or the for-loop style to handle events to do the same thing above. if false { page.MustNavigate("https://mdn.dev") for msg := range page.Event() { e := proto.PageLoadEventFired{} if msg.Load(&e) { break } } } page.MustEval(`() => console.log("hello", "world")`) <-done }
Output: [hello world]
Example (Hijack_requests) ¶
Shows how to intercept requests and modify both the request and the response. The entire process of hijacking one request:
browser --req-> rod ---> server ---> rod --res-> browser
The --req-> and --res-> are the parts that can be modified.
package main import ( "fmt" "net/http" "github.com/go-rod/rod" ) func main() { browser := rod.New().MustConnect() defer browser.MustClose() router := browser.HijackRequests() defer router.MustStop() router.MustAdd("*.js", func(ctx *rod.Hijack) { // Here we update the request's header. Rod gives functionality to // change or update all parts of the request. Refer to the documentation // for more information. ctx.Request.Req().Header.Set("My-Header", "test") // LoadResponse runs the default request to the destination of the request. // Not calling this will require you to mock the entire response. // This can be done with the SetXxx (Status, Header, Body) functions on the // ctx.Response struct. _ = ctx.LoadResponse(http.DefaultClient, true) // Here we append some code to every js file. // The code will update the document title to "hi" ctx.Response.SetBody(ctx.Response.Body() + "\n document.title = 'hi' ") }) go router.Run() browser.MustPage("https://go-rod.github.io").MustWait(`() => document.title === 'hi'`) fmt.Println("done") }
Output: done
Example (Load_extension) ¶
package main import ( "fmt" "path/filepath" "github.com/go-rod/rod" "github.com/go-rod/rod/lib/launcher" ) func main() { extPath, _ := filepath.Abs("fixtures/chrome-extension") u := launcher.New(). // Must use abs path for an extension Set("load-extension", extPath). // Headless mode doesn't support extension yet. // Reason: https://bugs.chromium.org/p/chromium/issues/detail?id=706008#c5 // You can use XVFB to get rid of it: https://github.com/go-rod/rod/blob/main/lib/examples/launch-managed/main.go Headless(false). MustLaunch() page := rod.New().ControlURL(u).MustConnect().MustPage("http://mdn.dev") page.MustWait(`() => document.title === 'test-extension'`) fmt.Println("ok") // Skip
Output:
Example (Log_cdp_traffic) ¶
package main import ( "fmt" "github.com/go-rod/rod" "github.com/go-rod/rod/lib/cdp" "github.com/go-rod/rod/lib/launcher" "github.com/go-rod/rod/lib/utils" ) func main() { cdp := cdp.New(). // Here we can customize how to log the requests, responses, and events transferred between Rod and the browser. Logger(utils.Log(func(args ...interface{}) { switch v := args[0].(type) { case *cdp.Request: fmt.Printf("id: %d", v.ID) } })). Start(cdp.MustConnectWS(launcher.New().MustLaunch())) rod.New().Client(cdp).MustConnect().MustPage("http://mdn.dev") }
Output:
Example (Page_pdf) ¶
package main import ( "github.com/go-rod/rod" "github.com/go-rod/rod/lib/proto" "github.com/go-rod/rod/lib/utils" "github.com/ysmood/gson" ) func main() { page := rod.New().MustConnect().MustPage("https://github.com").MustWaitLoad() // simple version page.MustPDF("my.pdf") // customized version pdf, _ := page.PDF(&proto.PagePrintToPDF{ PaperWidth: gson.Num(8.5), PaperHeight: gson.Num(11), PageRanges: "1-3", }) _ = utils.OutputFile("my.pdf", pdf) }
Output:
Example (Page_screenshot) ¶
package main import ( "github.com/go-rod/rod" "github.com/go-rod/rod/lib/proto" "github.com/go-rod/rod/lib/utils" "github.com/ysmood/gson" ) func main() { page := rod.New().MustConnect().MustPage("https://github.com").MustWaitLoad() // simple version page.MustScreenshot("my.png") // customization version img, _ := page.Screenshot(true, &proto.PageCaptureScreenshot{ Format: proto.PageCaptureScreenshotFormatJpeg, Quality: gson.Int(90), Clip: &proto.PageViewport{ X: 0, Y: 0, Width: 300, Height: 200, Scale: 1, }, FromSurface: true, }) _ = utils.OutputFile("my.jpg", img) }
Output:
Example (Race_selectors) ¶
Show how to handle multiple results of an action. Such as when you login a page, the result can be success or wrong password.
package main import ( "fmt" "github.com/go-rod/rod" "github.com/go-rod/rod/lib/input" ) func main() { const username = "" const password = "" browser := rod.New().MustConnect() page := browser.MustPage("https://leetcode.com/accounts/login/") page.MustElement("#id_login").MustInput(username) page.MustElement("#id_password").MustInput(password).MustType(input.Enter) // It will keep retrying until one selector has found a match elm := page.Race().Element(".nav-user-icon-base").MustHandle(func(e *rod.Element) { // print the username after successful login fmt.Println(*e.MustAttribute("title")) }).Element("[data-cy=sign-in-error]").MustDo() if elm.MustMatches("[data-cy=sign-in-error]") { // when wrong username or password panic(elm.MustText()) } }
Output:
Example (Search) ¶
Example_search shows how to use Search to get element inside nested iframes or shadow DOMs. It works the same as https://developers.google.com/web/tools/chrome-devtools/dom#search
package main import ( "fmt" "github.com/go-rod/rod" ) func main() { browser := rod.New().MustConnect() defer browser.MustClose() page := browser.MustPage("https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe") // Click the zoom-in button of the OpenStreetMap page.MustSearch(".leaflet-control-zoom-in").MustClick() fmt.Println("done") }
Output: done
Example (States) ¶
Shows how to update the state of the current page. In this example we enable the network domain.
package main import ( "fmt" "github.com/go-rod/rod" "github.com/go-rod/rod/lib/proto" ) func main() { browser := rod.New().MustConnect() defer browser.MustClose() page := browser.MustPage() // LoadState detects whether the network domain is enabled or not. fmt.Println(page.LoadState(&proto.NetworkEnable{})) _ = proto.NetworkEnable{}.Call(page) // Check if the network domain is successfully enabled. fmt.Println(page.LoadState(&proto.NetworkEnable{})) }
Output: false true
Example (Wait_for_animation) ¶
Rod uses mouse cursor to simulate clicks, so if a button is moving because of animation, the click may not work as expected. We usually use WaitStable to make sure the target isn't changing anymore.
package main import ( "fmt" "github.com/go-rod/rod" ) func main() { browser := rod.New().MustConnect() defer browser.MustClose() page := browser.MustPage("https://getbootstrap.com/docs/4.0/components/modal/") page.MustWaitLoad().MustElement("[data-target='#exampleModalLive']").MustClick() saveBtn := page.MustElementR("#exampleModalLive button", "Close") // Here, WaitStable will wait until the button's position and size become stable. saveBtn.MustWaitStable().MustClick().MustWaitInvisible() fmt.Println("done") }
Output: done
Example (Wait_for_request) ¶
When you want to wait for an ajax request to complete, this example will be useful.
package main import ( "fmt" "github.com/go-rod/rod" ) func main() { browser := rod.New().MustConnect() defer browser.MustClose() page := browser.MustPage("https://www.wikipedia.org/").MustWaitLoad() // Start to analyze request events wait := page.MustWaitRequestIdle() // This will trigger the search ajax request page.MustElement("#searchInput").MustClick().MustInput("lisp") // Wait until there's no active requests wait() // We want to make sure that after waiting, there are some autocomplete // suggestions available. fmt.Println(len(page.MustElements(".suggestion-link")) > 0) }
Output: true
Index ¶
- Variables
- func NotFoundSleeper() utils.Sleeper
- func Try(fn func()) (err error)
- type Browser
- func (b *Browser) Call(ctx context.Context, sessionID, methodName string, params interface{}) (res []byte, err error)
- func (b *Browser) CancelTimeout() *Browser
- func (b *Browser) Client(c CDPClient) *Browser
- func (b *Browser) Close() error
- func (b *Browser) Connect() error
- func (b *Browser) Context(ctx context.Context) *Browser
- func (b *Browser) ControlURL(url string) *Browser
- func (b *Browser) DefaultDevice(d devices.Device) *Browser
- func (b *Browser) DisableDomain(sessionID proto.TargetSessionID, req proto.Request) (restore func())
- func (b *Browser) EachEvent(callbacks ...interface{}) (wait func())
- func (b *Browser) EnableDomain(sessionID proto.TargetSessionID, req proto.Request) (restore func())
- func (b *Browser) Event() <-chan *Message
- func (b *Browser) GetContext() context.Context
- func (b *Browser) GetCookies() ([]*proto.NetworkCookie, error)
- func (b *Browser) HandleAuth(username, password string) func() error
- func (b *Browser) HijackRequests() *HijackRouter
- func (b *Browser) IgnoreCertErrors(enable bool) error
- func (b *Browser) Incognito() (*Browser, error)
- func (b *Browser) LoadState(sessionID proto.TargetSessionID, method proto.Request) (has bool)
- func (b *Browser) Logger(l utils.Logger) *Browser
- func (b *Browser) Monitor(url string) *Browser
- func (b *Browser) MustClose()
- func (b *Browser) MustConnect() *Browser
- func (b *Browser) MustGetCookies() []*proto.NetworkCookie
- func (b *Browser) MustHandleAuth(username, password string) (wait func())
- func (b *Browser) MustIgnoreCertErrors(enable bool) *Browser
- func (b *Browser) MustIncognito() *Browser
- func (b *Browser) MustPage(url ...string) *Page
- func (b *Browser) MustPageFromTargetID(targetID proto.TargetTargetID) *Page
- func (b *Browser) MustPages() Pages
- func (b *Browser) MustSetCookies(cookies ...*proto.NetworkCookie) *Browser
- func (b *Browser) MustVersion() *proto.BrowserGetVersionResult
- func (b *Browser) MustWaitDownload() func() []byte
- func (b *Browser) NoDefaultDevice() *Browser
- func (b *Browser) Page(opts proto.TargetCreateTarget) (p *Page, err error)
- func (b *Browser) PageFromSession(sessionID proto.TargetSessionID) *Page
- func (b *Browser) PageFromTarget(targetID proto.TargetTargetID) (*Page, error)
- func (b *Browser) Pages() (Pages, error)
- func (b *Browser) RemoveState(key interface{})
- func (b *Browser) ServeMonitor(host string) string
- func (b *Browser) SetCookies(cookies []*proto.NetworkCookieParam) error
- func (b *Browser) Sleeper(sleeper func() utils.Sleeper) *Browser
- func (b *Browser) SlowMotion(delay time.Duration) *Browser
- func (b *Browser) Timeout(d time.Duration) *Browser
- func (b *Browser) Trace(enable bool) *Browser
- func (b *Browser) Version() (*proto.BrowserGetVersionResult, error)
- func (b *Browser) WaitDownload(dir string) func() (info *proto.PageDownloadWillBegin)
- func (b *Browser) WaitEvent(e proto.Event) (wait func())
- func (b *Browser) WithCancel() (*Browser, func())
- func (b *Browser) WithPanic(fail func(interface{})) *Browser
- type BrowserPool
- type CDPClient
- type Element
- func (el *Element) Attribute(name string) (*string, error)
- func (el *Element) BackgroundImage() ([]byte, error)
- func (el *Element) Blur() error
- func (el *Element) Call(ctx context.Context, sessionID, methodName string, params interface{}) (res []byte, err error)
- func (el *Element) CancelTimeout() *Element
- func (el *Element) CanvasToImage(format string, quality float64) ([]byte, error)
- func (el *Element) Click(button proto.InputMouseButton, clickCount int) error
- func (el *Element) ContainsElement(target *Element) (bool, error)
- func (el *Element) Context(ctx context.Context) *Element
- func (el *Element) Describe(depth int, pierce bool) (*proto.DOMNode, error)
- func (el *Element) Disabled() (bool, error)
- func (el *Element) Element(selector string) (*Element, error)
- func (el *Element) ElementByJS(opts *EvalOptions) (*Element, error)
- func (el *Element) ElementR(selector, jsRegex string) (*Element, error)
- func (el *Element) ElementX(xPath string) (*Element, error)
- func (el *Element) Elements(selector string) (Elements, error)
- func (el *Element) ElementsByJS(opts *EvalOptions) (Elements, error)
- func (el *Element) ElementsX(xpath string) (Elements, error)
- func (el *Element) Equal(elm *Element) (bool, error)
- func (el *Element) Eval(js string, params ...interface{}) (*proto.RuntimeRemoteObject, error)
- func (el *Element) Evaluate(opts *EvalOptions) (*proto.RuntimeRemoteObject, error)
- func (el *Element) Focus() error
- func (el *Element) Frame() (*Page, error)
- func (el *Element) GetContext() context.Context
- func (el *Element) GetSessionID() proto.TargetSessionID
- func (el *Element) GetXPath(optimized bool) (string, error)
- func (el *Element) HTML() (string, error)
- func (el *Element) Has(selector string) (bool, *Element, error)
- func (el *Element) HasR(selector, jsRegex string) (bool, *Element, error)
- func (el *Element) HasX(selector string) (bool, *Element, error)
- func (el *Element) Hover() error
- func (el *Element) Input(text string) error
- func (el *Element) InputTime(t time.Time) error
- func (el *Element) Interactable() (pt *proto.Point, err error)
- func (el *Element) KeyActions() (*KeyActions, error)
- func (el *Element) Matches(selector string) (bool, error)
- func (el *Element) MoveMouseOut() error
- func (el *Element) MustAttribute(name string) *string
- func (el *Element) MustBackgroundImage() []byte
- func (el *Element) MustBlur() *Element
- func (el *Element) MustCanvasToImage() []byte
- func (el *Element) MustClick() *Element
- func (el *Element) MustContainsElement(target *Element) bool
- func (el *Element) MustDescribe() *proto.DOMNode
- func (el *Element) MustDisabled() bool
- func (el *Element) MustDoubleClick() *Element
- func (el *Element) MustElement(selector string) *Element
- func (el *Element) MustElementByJS(js string, params ...interface{}) *Element
- func (el *Element) MustElementR(selector, regex string) *Element
- func (el *Element) MustElementX(xpath string) *Element
- func (el *Element) MustElements(selector string) Elements
- func (el *Element) MustElementsByJS(js string, params ...interface{}) Elements
- func (el *Element) MustElementsX(xpath string) Elements
- func (el *Element) MustEqual(elm *Element) bool
- func (el *Element) MustEval(js string, params ...interface{}) gson.JSON
- func (el *Element) MustFocus() *Element
- func (el *Element) MustFrame() *Page
- func (el *Element) MustGetXPath(optimized bool) string
- func (el *Element) MustHTML() string
- func (el *Element) MustHas(selector string) bool
- func (el *Element) MustHasR(selector, regex string) bool
- func (el *Element) MustHasX(selector string) bool
- func (el *Element) MustHover() *Element
- func (el *Element) MustInput(text string) *Element
- func (el *Element) MustInputTime(t time.Time) *Element
- func (el *Element) MustInteractable() bool
- func (el *Element) MustKeyActions() *KeyActions
- func (el *Element) MustMatches(selector string) bool
- func (el *Element) MustMoveMouseOut() *Element
- func (el *Element) MustNext() *Element
- func (el *Element) MustParent() *Element
- func (el *Element) MustParents(selector string) Elements
- func (el *Element) MustPrevious() *Element
- func (el *Element) MustProperty(name string) gson.JSON
- func (el *Element) MustRelease()
- func (el *Element) MustRemove()
- func (el *Element) MustResource() []byte
- func (el *Element) MustScreenshot(toFile ...string) []byte
- func (el *Element) MustScrollIntoView() *Element
- func (el *Element) MustSelect(selectors ...string) *Element
- func (el *Element) MustSelectAllText() *Element
- func (el *Element) MustSelectText(regex string) *Element
- func (el *Element) MustSetFiles(paths ...string) *Element
- func (el *Element) MustShadowRoot() *Element
- func (el *Element) MustShape() *proto.DOMGetContentQuadsResult
- func (el *Element) MustTap() *Element
- func (el *Element) MustText() string
- func (el *Element) MustType(keys ...input.Key) *Element
- func (el *Element) MustVisible() bool
- func (el *Element) MustWait(js string, params ...interface{}) *Element
- func (el *Element) MustWaitEnabled() *Element
- func (el *Element) MustWaitInteractable() *Element
- func (el *Element) MustWaitInvisible() *Element
- func (el *Element) MustWaitLoad() *Element
- func (el *Element) MustWaitStable() *Element
- func (el *Element) MustWaitVisible() *Element
- func (el *Element) MustWaitWritable() *Element
- func (el *Element) Next() (*Element, error)
- func (el *Element) Overlay(msg string) (removeOverlay func())
- func (el *Element) Page() *Page
- func (el *Element) Parent() (*Element, error)
- func (el *Element) Parents(selector string) (Elements, error)
- func (el *Element) Previous() (*Element, error)
- func (el *Element) Property(name string) (gson.JSON, error)
- func (el *Element) Release() error
- func (el *Element) Remove() error
- func (el *Element) Resource() ([]byte, error)
- func (el *Element) Screenshot(format proto.PageCaptureScreenshotFormat, quality int) ([]byte, error)
- func (el *Element) ScrollIntoView() error
- func (el *Element) Select(selectors []string, selected bool, t SelectorType) error
- func (el *Element) SelectAllText() error
- func (el *Element) SelectText(regex string) error
- func (el *Element) SetFiles(paths []string) error
- func (el *Element) ShadowRoot() (*Element, error)
- func (el *Element) Shape() (*proto.DOMGetContentQuadsResult, error)
- func (el *Element) Sleeper(sleeper func() utils.Sleeper) *Element
- func (el *Element) String() string
- func (el *Element) Tap() error
- func (el *Element) Text() (string, error)
- func (el *Element) Timeout(d time.Duration) *Element
- func (el *Element) Type(keys ...input.Key) error
- func (el *Element) Visible() (bool, error)
- func (el *Element) Wait(opts *EvalOptions) error
- func (el *Element) WaitEnabled() error
- func (el *Element) WaitInteractable() (pt *proto.Point, err error)
- func (el *Element) WaitInvisible() error
- func (el *Element) WaitLoad() error
- func (el *Element) WaitStable(d time.Duration) error
- func (el *Element) WaitStableRAF() error
- func (el *Element) WaitVisible() error
- func (el *Element) WaitWritable() error
- func (el *Element) WithCancel() (*Element, func())
- func (el *Element) WithPanic(fail func(interface{})) *Element
- type Elements
- type ErrCovered
- type ErrElementNotFound
- type ErrEval
- type ErrExpectElement
- type ErrExpectElements
- type ErrInvisibleShape
- type ErrNavigation
- type ErrNoPointerEvents
- type ErrNoShadowRoot
- type ErrNotInteractable
- type ErrObjectNotFound
- type ErrPageCloseCanceled
- type ErrPageNotFound
- type ErrTry
- type EvalOptions
- type Hijack
- type HijackRequest
- func (ctx *HijackRequest) Body() string
- func (ctx *HijackRequest) Header(key string) string
- func (ctx *HijackRequest) Headers() proto.NetworkHeaders
- func (ctx *HijackRequest) IsNavigation() bool
- func (ctx *HijackRequest) JSONBody() gson.JSON
- func (ctx *HijackRequest) Method() string
- func (ctx *HijackRequest) Req() *http.Request
- func (ctx *HijackRequest) SetBody(obj interface{}) *HijackRequest
- func (ctx *HijackRequest) SetContext(c context.Context) *HijackRequest
- func (ctx *HijackRequest) Type() proto.NetworkResourceType
- func (ctx *HijackRequest) URL() *url.URL
- type HijackResponse
- func (ctx *HijackResponse) Body() string
- func (ctx *HijackResponse) Fail(reason proto.NetworkErrorReason) *HijackResponse
- func (ctx *HijackResponse) Headers() http.Header
- func (ctx *HijackResponse) Payload() *proto.FetchFulfillRequest
- func (ctx *HijackResponse) SetBody(obj interface{}) *HijackResponse
- func (ctx *HijackResponse) SetHeader(pairs ...string) *HijackResponse
- type HijackRouter
- func (r *HijackRouter) Add(pattern string, resourceType proto.NetworkResourceType, handler func(*Hijack)) error
- func (r *HijackRouter) MustAdd(pattern string, handler func(*Hijack)) *HijackRouter
- func (r *HijackRouter) MustRemove(pattern string) *HijackRouter
- func (r *HijackRouter) MustStop()
- func (r *HijackRouter) Remove(pattern string) error
- func (r *HijackRouter) Run()
- func (r *HijackRouter) Stop() error
- type KeyAction
- type KeyActionType
- type KeyActions
- type Keyboard
- type Message
- type Mouse
- func (m *Mouse) Click(button proto.InputMouseButton, clickCount int) error
- func (m *Mouse) Down(button proto.InputMouseButton, clickCount int) error
- func (m *Mouse) MoveAlong(guide func() (proto.Point, bool)) error
- func (m *Mouse) MoveLinear(to proto.Point, steps int) error
- func (m *Mouse) MoveTo(p proto.Point) error
- func (m *Mouse) MustClick(button proto.InputMouseButton) *Mouse
- func (m *Mouse) MustDown(button proto.InputMouseButton) *Mouse
- func (m *Mouse) MustMoveTo(x, y float64) *Mouse
- func (m *Mouse) MustScroll(x, y float64) *Mouse
- func (m *Mouse) MustUp(button proto.InputMouseButton) *Mouse
- func (m *Mouse) Position() proto.Point
- func (m *Mouse) Scroll(offsetX, offsetY float64, steps int) error
- func (m *Mouse) Up(button proto.InputMouseButton, clickCount int) error
- type Page
- func (p *Page) Activate() (*Page, error)
- func (p *Page) AddScriptTag(url, content string) error
- func (p *Page) AddStyleTag(url, content string) error
- func (p *Page) Browser() *Browser
- func (p *Page) Call(ctx context.Context, sessionID, methodName string, params interface{}) (res []byte, err error)
- func (p *Page) CancelTimeout() *Page
- func (p *Page) CaptureDOMSnapshot() (domSnapshot *proto.DOMSnapshotCaptureSnapshotResult, err error)
- func (p *Page) Close() error
- func (p *Page) Context(ctx context.Context) *Page
- func (p *Page) Cookies(urls []string) ([]*proto.NetworkCookie, error)
- func (p *Page) DisableDomain(method proto.Request) (restore func())
- func (p *Page) EachEvent(callbacks ...interface{}) (wait func())
- func (p *Page) Element(selector string) (*Element, error)
- func (p *Page) ElementByJS(opts *EvalOptions) (*Element, error)
- func (p *Page) ElementFromNode(node *proto.DOMNode) (*Element, error)
- func (p *Page) ElementFromObject(obj *proto.RuntimeRemoteObject) (*Element, error)
- func (p *Page) ElementFromPoint(x, y int) (*Element, error)
- func (p *Page) ElementR(selector, jsRegex string) (*Element, error)
- func (p *Page) ElementX(xPath string) (*Element, error)
- func (p *Page) Elements(selector string) (Elements, error)
- func (p *Page) ElementsByJS(opts *EvalOptions) (Elements, error)
- func (p *Page) ElementsX(xpath string) (Elements, error)
- func (p *Page) Emulate(device devices.Device) error
- func (p *Page) EnableDomain(method proto.Request) (restore func())
- func (p *Page) Eval(js string, args ...interface{}) (*proto.RuntimeRemoteObject, error)
- func (p *Page) EvalOnNewDocument(js string) (remove func() error, err error)
- func (p *Page) Evaluate(opts *EvalOptions) (res *proto.RuntimeRemoteObject, err error)
- func (p *Page) Event() <-chan *Message
- func (p *Page) Expose(name string, fn func(gson.JSON) (interface{}, error)) (stop func() error, err error)
- func (p *Page) ExposeHelpers(list ...*js.Function)
- func (p *Page) GetContext() context.Context
- func (p *Page) GetResource(url string) ([]byte, error)
- func (p *Page) GetSessionID() proto.TargetSessionID
- func (p *Page) GetWindow() (*proto.BrowserBounds, error)
- func (p *Page) HTML() (string, error)
- func (p *Page) HandleDialog() (wait func() *proto.PageJavascriptDialogOpening, ...)
- func (p *Page) HandleFileDialog() (func([]string) error, error)
- func (p *Page) Has(selector string) (bool, *Element, error)
- func (p *Page) HasR(selector, jsRegex string) (bool, *Element, error)
- func (p *Page) HasX(selector string) (bool, *Element, error)
- func (p *Page) HijackRequests() *HijackRouter
- func (p *Page) Info() (*proto.TargetTargetInfo, error)
- func (p *Page) InsertText(text string) error
- func (p *Page) IsIframe() bool
- func (p *Page) KeyActions() *KeyActions
- func (p *Page) LoadState(method proto.Request) (has bool)
- func (p *Page) MustActivate() *Page
- func (p *Page) MustAddScriptTag(url string) *Page
- func (p *Page) MustAddStyleTag(url string) *Page
- func (p *Page) MustCaptureDOMSnapshot() (domSnapshot *proto.DOMSnapshotCaptureSnapshotResult)
- func (p *Page) MustClose()
- func (p *Page) MustCookies(urls ...string) []*proto.NetworkCookie
- func (p *Page) MustElement(selector string) *Element
- func (p *Page) MustElementByJS(js string, params ...interface{}) *Element
- func (p *Page) MustElementFromNode(node *proto.DOMNode) *Element
- func (p *Page) MustElementFromPoint(left, top int) *Element
- func (p *Page) MustElementR(selector, jsRegex string) *Element
- func (p *Page) MustElementX(xPath string) *Element
- func (p *Page) MustElements(selector string) Elements
- func (p *Page) MustElementsByJS(js string, params ...interface{}) Elements
- func (p *Page) MustElementsX(xpath string) Elements
- func (p *Page) MustEmulate(device devices.Device) *Page
- func (p *Page) MustEval(js string, params ...interface{}) gson.JSON
- func (p *Page) MustEvalOnNewDocument(js string)
- func (p *Page) MustEvaluate(opts *EvalOptions) *proto.RuntimeRemoteObject
- func (p *Page) MustExpose(name string, fn func(gson.JSON) (interface{}, error)) (stop func())
- func (p *Page) MustGetWindow() *proto.BrowserBounds
- func (p *Page) MustHTML() string
- func (p *Page) MustHandleDialog() (wait func() *proto.PageJavascriptDialogOpening, handle func(bool, string))
- func (p *Page) MustHandleFileDialog() func(...string)
- func (p *Page) MustHas(selector string) bool
- func (p *Page) MustHasR(selector, regex string) bool
- func (p *Page) MustHasX(selector string) bool
- func (p *Page) MustInfo() *proto.TargetTargetInfo
- func (p *Page) MustInsertText(text string) *Page
- func (p *Page) MustNavigate(url string) *Page
- func (p *Page) MustNavigateBack() *Page
- func (p *Page) MustNavigateForward() *Page
- func (p *Page) MustObjectToJSON(obj *proto.RuntimeRemoteObject) gson.JSON
- func (p *Page) MustObjectsToJSON(list []*proto.RuntimeRemoteObject) gson.JSON
- func (p *Page) MustPDF(toFile ...string) []byte
- func (p *Page) MustRelease(obj *proto.RuntimeRemoteObject) *Page
- func (p *Page) MustReload() *Page
- func (p *Page) MustScreenshot(toFile ...string) []byte
- func (p *Page) MustScreenshotFullPage(toFile ...string) []byte
- func (p *Page) MustSearch(query string) *Element
- func (p *Page) MustSetBlockedURLs(urls ...string) *Page
- func (p *Page) MustSetCookies(cookies ...*proto.NetworkCookieParam) *Page
- func (p *Page) MustSetDocumentContent(html string) *Page
- func (p *Page) MustSetExtraHeaders(dict ...string) (cleanup func())
- func (p *Page) MustSetUserAgent(req *proto.NetworkSetUserAgentOverride) *Page
- func (p *Page) MustSetViewport(width, height int, deviceScaleFactor float64, mobile bool) *Page
- func (p *Page) MustSetWindow(left, top, width, height int) *Page
- func (p *Page) MustStopLoading() *Page
- func (p *Page) MustTriggerFavicon() *Page
- func (p *Page) MustWait(js string, params ...interface{}) *Page
- func (p *Page) MustWaitElementsMoreThan(selector string, num int) *Page
- func (p *Page) MustWaitIdle() *Page
- func (p *Page) MustWaitLoad() *Page
- func (p *Page) MustWaitNavigation() func()
- func (p *Page) MustWaitOpen() (wait func() (newPage *Page))
- func (p *Page) MustWaitRequestIdle(excludes ...string) (wait func())
- func (p *Page) MustWaitStable() *Page
- func (p *Page) MustWindowFullscreen() *Page
- func (p *Page) MustWindowMaximize() *Page
- func (p *Page) MustWindowMinimize() *Page
- func (p *Page) MustWindowNormal() *Page
- func (p *Page) Navigate(url string) error
- func (p *Page) NavigateBack() error
- func (p *Page) NavigateForward() error
- func (p *Page) ObjectToJSON(obj *proto.RuntimeRemoteObject) (gson.JSON, error)
- func (p *Page) Overlay(left, top, width, height float64, msg string) (remove func())
- func (p *Page) PDF(req *proto.PagePrintToPDF) (*StreamReader, error)
- func (p *Page) Race() *RaceContext
- func (p *Page) Release(obj *proto.RuntimeRemoteObject) error
- func (p *Page) Reload() error
- func (p *Page) Screenshot(fullPage bool, req *proto.PageCaptureScreenshot) ([]byte, error)
- func (p *Page) Search(query string) (*SearchResult, error)
- func (p *Page) SetBlockedURLs(urls []string) error
- func (p *Page) SetCookies(cookies []*proto.NetworkCookieParam) error
- func (p *Page) SetDocumentContent(html string) error
- func (p *Page) SetExtraHeaders(dict []string) (func(), error)
- func (p *Page) SetUserAgent(req *proto.NetworkSetUserAgentOverride) error
- func (p *Page) SetViewport(params *proto.EmulationSetDeviceMetricsOverride) error
- func (p *Page) SetWindow(bounds *proto.BrowserBounds) error
- func (p *Page) Sleeper(sleeper func() utils.Sleeper) *Page
- func (p *Page) StopLoading() error
- func (p *Page) String() string
- func (p *Page) Timeout(d time.Duration) *Page
- func (p *Page) TriggerFavicon() error
- func (p *Page) Wait(opts *EvalOptions) error
- func (p *Page) WaitElementsMoreThan(selector string, num int) error
- func (p *Page) WaitEvent(e proto.Event) (wait func())
- func (p *Page) WaitIdle(timeout time.Duration) (err error)
- func (p *Page) WaitLoad() error
- func (p *Page) WaitNavigation(name proto.PageLifecycleEventName) func()
- func (p *Page) WaitOpen() func() (*Page, error)
- func (p *Page) WaitRepaint() error
- func (p *Page) WaitRequestIdle(d time.Duration, includes, excludes []string) func()
- func (p *Page) WaitStable(d time.Duration, similarity float32) error
- func (p *Page) WithCancel() (*Page, func())
- func (p *Page) WithPanic(fail func(interface{})) *Page
- type PagePool
- type Pages
- type RaceContext
- func (rc *RaceContext) Do() (*Element, error)
- func (rc *RaceContext) Element(selector string) *RaceContext
- func (rc *RaceContext) ElementByJS(opts *EvalOptions) *RaceContext
- func (rc *RaceContext) ElementFunc(fn func(*Page) (*Element, error)) *RaceContext
- func (rc *RaceContext) ElementR(selector, regex string) *RaceContext
- func (rc *RaceContext) ElementX(selector string) *RaceContext
- func (rc *RaceContext) Handle(callback func(*Element) error) *RaceContext
- func (rc *RaceContext) MustDo() *Element
- func (rc *RaceContext) MustElementByJS(js string, params []interface{}) *RaceContext
- func (rc *RaceContext) MustHandle(callback func(*Element)) *RaceContext
- func (rc *RaceContext) Search(query string) *RaceContext
- type SearchResult
- type SelectorType
- type StreamReader
- type Touch
- func (t *Touch) Cancel() error
- func (t *Touch) End() error
- func (t *Touch) Move(points ...*proto.InputTouchPoint) error
- func (t *Touch) MustCancel() *Touch
- func (t *Touch) MustEnd() *Touch
- func (t *Touch) MustMove(points ...*proto.InputTouchPoint) *Touch
- func (t *Touch) MustStart(points ...*proto.InputTouchPoint) *Touch
- func (t *Touch) MustTap(x, y float64) *Touch
- func (t *Touch) Start(points ...*proto.InputTouchPoint) error
- func (t *Touch) Tap(x, y float64) error
- type TraceType
Examples ¶
- Package
- Package (Context_and_EachEvent)
- Package (Context_and_timeout)
- Package (Customize_browser_launch)
- Package (Customize_retry_strategy)
- Package (Direct_cdp)
- Package (Disable_headless_to_debug)
- Package (Download_file)
- Package (Error_handling)
- Package (Eval_reuse_remote_object)
- Package (Handle_events)
- Package (Hijack_requests)
- Package (Load_extension)
- Package (Log_cdp_traffic)
- Package (Page_pdf)
- Package (Page_screenshot)
- Package (Race_selectors)
- Package (Search)
- Package (States)
- Package (Wait_for_animation)
- Package (Wait_for_request)
- Page (Pool)
Constants ¶
This section is empty.
Variables ¶
DefaultLogger for rod
var DefaultSleeper = func() utils.Sleeper { return utils.BackoffSleeper(100*time.Millisecond, time.Second, nil) }
DefaultSleeper generates the default sleeper for retry, it uses backoff to grow the interval. The growth looks like:
A(0) = 100ms, A(n) = A(n-1) * random[1.9, 2.1), A(n) < 1s
Why the default is not RequestAnimationFrame or DOM change events is because of if a retry never ends it can easily flood the program. But you can always easily config it into what you want.
Functions ¶
func NotFoundSleeper ¶ added in v0.88.9
NotFoundSleeper returns ErrElementNotFound on the first call
Types ¶
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/go-rod/rod/lib/defaults
func New ¶
func New() *Browser
New creates a controller. DefaultDevice to emulate is set to devices.LaptopWithMDPIScreen.Landscape(), it can make the actual view area smaller than the browser window on headful mode, you can use NoDefaultDevice to disable it.
func (*Browser) Call ¶
func (b *Browser) Call(ctx context.Context, sessionID, methodName string, params interface{}) (res []byte, err error)
Call raw cdp interface directly
func (*Browser) CancelTimeout ¶
CancelTimeout cancels the current timeout context and returns a clone with the parent context
func (*Browser) Connect ¶
Connect to the browser and start to control it. If fails to connect, try to launch a local browser, if local browser not found try to download one.
func (*Browser) ControlURL ¶
ControlURL set the url to remote control browser.
func (*Browser) DefaultDevice ¶ added in v0.71.0
DefaultDevice sets the default device for new page to emulate in the future. Default is devices.LaptopWithMDPIScreen . Set it to devices.Clear to disable it.
func (*Browser) DisableDomain ¶
func (b *Browser) DisableDomain(sessionID proto.TargetSessionID, req proto.Request) (restore func())
DisableDomain and returns a restore function to restore previous state
func (*Browser) EachEvent ¶
func (b *Browser) EachEvent(callbacks ...interface{}) (wait func())
EachEvent is similar to Page.EachEvent, but catches events of the entire browser.
func (*Browser) EnableDomain ¶
func (b *Browser) EnableDomain(sessionID proto.TargetSessionID, req proto.Request) (restore func())
EnableDomain and returns a restore function to restore previous state
func (*Browser) GetContext ¶
GetContext of current instance
func (*Browser) GetCookies ¶ added in v0.71.0
func (b *Browser) GetCookies() ([]*proto.NetworkCookie, error)
GetCookies from the browser
func (*Browser) HandleAuth ¶
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) HijackRequests ¶
func (b *Browser) HijackRequests() *HijackRouter
HijackRequests same as Page.HijackRequests, but can intercept requests of the entire browser.
func (*Browser) IgnoreCertErrors ¶ added in v0.61.3
IgnoreCertErrors switch. If enabled, all certificate errors will be ignored.
func (*Browser) Monitor ¶ added in v0.70.0
Monitor address to listen if not empty. Shortcut for Browser.ServeMonitor
func (*Browser) MustClose ¶ added in v0.50.0
func (b *Browser) MustClose()
MustClose is similar to Browser.Close
func (*Browser) MustConnect ¶ added in v0.50.0
MustConnect is similar to Browser.Connect
func (*Browser) MustGetCookies ¶ added in v0.71.0
func (b *Browser) MustGetCookies() []*proto.NetworkCookie
MustGetCookies is similar Browser.GetCookies
func (*Browser) MustHandleAuth ¶ added in v0.50.0
MustHandleAuth is similar to Browser.HandleAuth
func (*Browser) MustIgnoreCertErrors ¶ added in v0.61.3
MustIgnoreCertErrors is similar to Browser.IgnoreCertErrors
func (*Browser) MustIncognito ¶ added in v0.50.0
MustIncognito is similar to Browser.Incognito
func (*Browser) MustPage ¶ added in v0.50.0
MustPage is similar to Browser.Page. The url list will be joined by "/".
func (*Browser) MustPageFromTargetID ¶ added in v0.50.0
func (b *Browser) MustPageFromTargetID(targetID proto.TargetTargetID) *Page
MustPageFromTargetID is similar to Browser.PageFromTargetID
func (*Browser) MustSetCookies ¶ added in v0.71.0
func (b *Browser) MustSetCookies(cookies ...*proto.NetworkCookie) *Browser
MustSetCookies is similar Browser.SetCookies. If the len(cookies) is 0 it will clear all the cookies.
func (*Browser) MustVersion ¶ added in v0.107.0
func (b *Browser) MustVersion() *proto.BrowserGetVersionResult
MustVersion is similar to Browser.Version.
func (*Browser) MustWaitDownload ¶ added in v0.83.0
MustWaitDownload is similar to Browser.WaitDownload. It will read the file into bytes then remove the file.
func (*Browser) NoDefaultDevice ¶ added in v0.81.1
NoDefaultDevice is the same as DefaultDevice(devices.Clear)
func (*Browser) Page ¶
func (b *Browser) Page(opts proto.TargetCreateTarget) (p *Page, err error)
Page creates a new browser tab. If opts.URL is empty, the default target will be "about:blank".
func (*Browser) PageFromSession ¶ added in v0.74.0
func (b *Browser) PageFromSession(sessionID proto.TargetSessionID) *Page
PageFromSession is used for low-level debugging
func (*Browser) PageFromTarget ¶ added in v0.50.0
func (b *Browser) PageFromTarget(targetID proto.TargetTargetID) (*Page, error)
PageFromTarget gets or creates a Page instance.
func (*Browser) RemoveState ¶ added in v0.74.0
func (b *Browser) RemoveState(key interface{})
RemoveState a state
func (*Browser) ServeMonitor ¶
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) SetCookies ¶ added in v0.71.0
func (b *Browser) SetCookies(cookies []*proto.NetworkCookieParam) error
SetCookies to the browser. If the cookies is nil it will clear all the cookies.
func (*Browser) Sleeper ¶ added in v0.50.0
Sleeper returns a clone with the specified sleeper for chained sub-operations
func (*Browser) SlowMotion ¶ added in v0.77.0
SlowMotion set the delay for each control action, such as the simulation of the human inputs
func (*Browser) Timeout ¶
Timeout returns a clone with the specified total timeout of all chained sub-operations
func (*Browser) Version ¶ added in v0.107.0
func (b *Browser) Version() (*proto.BrowserGetVersionResult, error)
Version info of the browser
func (*Browser) WaitDownload ¶ added in v0.83.0
func (b *Browser) WaitDownload(dir string) func() (info *proto.PageDownloadWillBegin)
WaitDownload returns a helper to get the next download file. The file path will be:
filepath.Join(dir, info.GUID)
func (*Browser) WaitEvent ¶
WaitEvent waits for the next event for one time. It will also load the data into the event object.
func (*Browser) WithCancel ¶ added in v0.69.0
WithCancel returns a clone with a context cancel function
type BrowserPool ¶ added in v0.101.7
type BrowserPool chan *Browser
BrowserPool to thread-safely limit the number of browsers at the same time. It's a common practice to use a channel to limit concurrency, it's not special for rod. This helper is more like an example to use Go Channel. Reference: https://golang.org/doc/effective_go#channels
func NewBrowserPool ¶ added in v0.101.7
func NewBrowserPool(limit int) BrowserPool
NewBrowserPool instance
func (BrowserPool) Cleanup ¶ added in v0.101.7
func (bp BrowserPool) Cleanup(iteratee func(*Browser))
Cleanup helper
func (BrowserPool) Get ¶ added in v0.101.7
func (bp BrowserPool) Get(create func() *Browser) *Browser
Get a browser from the pool. Use the BrowserPool.Put to make it reusable later.
func (BrowserPool) Put ¶ added in v0.101.7
func (bp BrowserPool) Put(p *Browser)
Put a browser back to the pool
type CDPClient ¶ added in v0.70.0
type CDPClient interface { Event() <-chan *cdp.Event Call(ctx context.Context, sessionID, method string, params interface{}) ([]byte, error) }
CDPClient is usually used to make rod side-effect free. Such as proxy all IO of rod.
type Element ¶
type Element struct { Object *proto.RuntimeRemoteObject // contains filtered or unexported fields }
Element represents the DOM element
func (*Element) Attribute ¶
Attribute of the DOM object. Attribute vs Property: https://stackoverflow.com/questions/6003819/what-is-the-difference-between-properties-and-attributes-in-html
func (*Element) BackgroundImage ¶ added in v0.76.6
BackgroundImage returns the css background-image of the element
func (*Element) Call ¶ added in v0.70.0
func (el *Element) Call(ctx context.Context, sessionID, methodName string, params interface{}) (res []byte, err error)
Call implements the proto.Client
func (*Element) CancelTimeout ¶
CancelTimeout cancels the current timeout context and returns a clone with the parent context
func (*Element) CanvasToImage ¶ added in v0.45.1
CanvasToImage get image data of a canvas. The default format is image/png. The default quality is 0.92. doc: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL
func (*Element) Click ¶
func (el *Element) Click(button proto.InputMouseButton, clickCount int) error
Click will press then release the button just like a human. Before the action, it will try to scroll to the element, hover the mouse over it, wait until the it's interactable and enabled.
func (*Element) ContainsElement ¶ added in v0.48.0
ContainsElement check if the target is equal or inside the element.
func (*Element) Describe ¶
Describe the current element. The depth is the maximum depth at which children should be retrieved, defaults to 1, use -1 for the entire subtree or provide an integer larger than 0. The pierce decides whether or not iframes and shadow roots should be traversed when returning the subtree. The returned proto.DOMNode.NodeID will always be empty, because NodeID is not stable (when proto.DOMDocumentUpdated is fired all NodeID on the page will be reassigned to another value) we don't recommend using the NodeID, instead, use the BackendNodeID to identify the element.
func (*Element) ElementByJS ¶
func (el *Element) ElementByJS(opts *EvalOptions) (*Element, error)
ElementByJS returns the element from the return value of the js
func (*Element) ElementR ¶ added in v0.57.0
ElementR returns the first child element that matches the css selector and its text matches the jsRegex.
func (*Element) ElementsByJS ¶
func (el *Element) ElementsByJS(opts *EvalOptions) (Elements, error)
ElementsByJS returns the elements from the return value of the js
func (*Element) Eval ¶
func (el *Element) Eval(js string, params ...interface{}) (*proto.RuntimeRemoteObject, error)
Eval is a shortcut for Element.Evaluate with AwaitPromise, ByValue and AutoExp set to true.
func (*Element) Evaluate ¶ added in v0.67.0
func (el *Element) Evaluate(opts *EvalOptions) (*proto.RuntimeRemoteObject, error)
Evaluate is just a shortcut of Page.Evaluate with This set to current element.
func (*Element) Focus ¶
Focus sets focus on the specified element. Before the action, it will try to scroll to the element.
func (*Element) GetContext ¶
GetContext of current instance
func (*Element) GetSessionID ¶ added in v0.72.0
func (el *Element) GetSessionID() proto.TargetSessionID
GetSessionID interface
func (*Element) HasR ¶ added in v0.61.0
HasR returns true if a child element that matches the css selector and its text matches the jsRegex.
func (*Element) Hover ¶ added in v0.49.1
Hover the mouse over the center of the element. Before the action, it will try to scroll to the element and wait until it's interactable.
func (*Element) Input ¶
Input focuses on the element and input text to it. Before the action, it will scroll to the element, wait until it's visible, enabled and writable. To empty the input you can use something like el.SelectAllText().MustInput("")
func (*Element) InputTime ¶ added in v0.79.2
InputTime focuses on the element and input time to it. Before the action, it will scroll to the element, wait until it's visible, enabled and writable. It will wait until the element is visible, enabled and writable.
func (*Element) Interactable ¶ added in v0.66.0
Interactable checks if the element is interactable with cursor. The cursor can be mouse, finger, stylus, etc. If not interactable err will be ErrNotInteractable, such as when covered by a modal,
func (*Element) KeyActions ¶ added in v0.107.0
func (el *Element) KeyActions() (*KeyActions, error)
KeyActions is similar with Page.KeyActions. Before the action, it will try to scroll to the element and focus on it.
func (*Element) Matches ¶ added in v0.45.0
Matches checks if the element can be selected by the css selector
func (*Element) MoveMouseOut ¶ added in v0.97.13
MoveMouseOut of the current element
func (*Element) MustAttribute ¶ added in v0.50.0
MustAttribute is similar to Element.Attribute
func (*Element) MustBackgroundImage ¶ added in v0.76.6
MustBackgroundImage is similar to Element.BackgroundImage
func (*Element) MustCanvasToImage ¶ added in v0.50.0
MustCanvasToImage is similar to Element.CanvasToImage
func (*Element) MustContainsElement ¶ added in v0.50.0
MustContainsElement is similar to Element.ContainsElement
func (*Element) MustDescribe ¶ added in v0.50.0
MustDescribe is similar to Element.Describe
func (*Element) MustDisabled ¶ added in v0.112.5
MustDisabled is similar to Element.Disabled
func (*Element) MustDoubleClick ¶ added in v0.111.0
MustDoubleClick is similar to Element.Click
func (*Element) MustElement ¶ added in v0.50.0
MustElement is similar to Element.Element
func (*Element) MustElementByJS ¶ added in v0.50.0
MustElementByJS is similar to Element.ElementByJS
func (*Element) MustElementR ¶ added in v0.57.0
MustElementR is similar to Element.ElementR
func (*Element) MustElementX ¶ added in v0.50.0
MustElementX is similar to Element.ElementX
func (*Element) MustElements ¶ added in v0.50.0
MustElements is similar to Element.Elements
func (*Element) MustElementsByJS ¶ added in v0.50.0
MustElementsByJS is similar to Element.ElementsByJS
func (*Element) MustElementsX ¶ added in v0.50.0
MustElementsX is similar to Element.ElementsX
func (*Element) MustGetXPath ¶ added in v0.109.3
MustGetXPath is similar to Element.GetXPath
func (*Element) MustInputTime ¶ added in v0.79.2
MustInputTime is similar to Element.Input
func (*Element) MustInteractable ¶ added in v0.66.0
MustInteractable is similar to Element.Interactable
func (*Element) MustKeyActions ¶ added in v0.107.0
func (el *Element) MustKeyActions() *KeyActions
MustKeyActions is similar to Element.KeyActions
func (*Element) MustMatches ¶ added in v0.50.0
MustMatches is similar to Element.Matches
func (*Element) MustMoveMouseOut ¶ added in v0.97.13
MustMoveMouseOut is similar to Element.MoveMouseOut
func (*Element) MustParent ¶ added in v0.50.0
MustParent is similar to Element.Parent
func (*Element) MustParents ¶ added in v0.50.0
MustParents is similar to Element.Parents
func (*Element) MustPrevious ¶ added in v0.50.0
MustPrevious is similar to Element.Previous
func (*Element) MustProperty ¶ added in v0.50.0
MustProperty is similar to Element.Property
func (*Element) MustRelease ¶ added in v0.50.0
func (el *Element) MustRelease()
MustRelease is similar to Element.Release
func (*Element) MustRemove ¶ added in v0.66.0
func (el *Element) MustRemove()
MustRemove the element from the page
func (*Element) MustResource ¶ added in v0.50.0
MustResource is similar to Element.Resource
func (*Element) MustScreenshot ¶ added in v0.50.0
MustScreenshot is similar to Element.Screenshot
func (*Element) MustScrollIntoView ¶ added in v0.50.0
MustScrollIntoView is similar to Element.ScrollIntoView
func (*Element) MustSelect ¶ added in v0.50.0
MustSelect is similar to Element.Select
func (*Element) MustSelectAllText ¶ added in v0.50.0
MustSelectAllText is similar to Element.SelectAllText
func (*Element) MustSelectText ¶ added in v0.50.0
MustSelectText is similar to Element.SelectText
func (*Element) MustSetFiles ¶ added in v0.50.0
MustSetFiles is similar to Element.SetFiles
func (*Element) MustShadowRoot ¶ added in v0.50.0
MustShadowRoot is similar to Element.ShadowRoot
func (*Element) MustShape ¶ added in v0.66.0
func (el *Element) MustShape() *proto.DOMGetContentQuadsResult
MustShape is similar to Element.Shape
func (*Element) MustVisible ¶ added in v0.50.0
MustVisible is similar to Element.Visible
func (*Element) MustWaitEnabled ¶ added in v0.84.1
MustWaitEnabled is similar to Element.WaitEnabled
func (*Element) MustWaitInteractable ¶ added in v0.88.0
MustWaitInteractable is similar to Element.WaitInteractable
func (*Element) MustWaitInvisible ¶ added in v0.50.0
MustWaitInvisible is similar to Element.WaitInvisible
func (*Element) MustWaitLoad ¶ added in v0.50.0
MustWaitLoad is similar to Element.WaitLoad
func (*Element) MustWaitStable ¶ added in v0.50.0
MustWaitStable is similar to Element.WaitStable
func (*Element) MustWaitVisible ¶ added in v0.50.0
MustWaitVisible is similar to Element.WaitVisible
func (*Element) MustWaitWritable ¶ added in v0.84.1
MustWaitWritable is similar to Element.WaitWritable
func (*Element) Property ¶
Property of the DOM object. Property vs Attribute: https://stackoverflow.com/questions/6003819/what-is-the-difference-between-properties-and-attributes-in-html
func (*Element) Resource ¶
Resource returns the "src" content of current element. Such as the jpg of <img src="a.jpg">
func (*Element) Screenshot ¶
func (el *Element) Screenshot(format proto.PageCaptureScreenshotFormat, quality int) ([]byte, error)
Screenshot of the area of the element
func (*Element) ScrollIntoView ¶
ScrollIntoView scrolls the current element into the visible area of the browser window if it's not already within the visible area.
func (*Element) Select ¶
func (el *Element) Select(selectors []string, selected bool, t SelectorType) error
Select the children option elements that match the selectors. Before the action, it will scroll to the element, wait until it's visible. If no option matches the selectors, it will return ErrElementNotFound.
func (*Element) SelectAllText ¶
SelectAllText selects all text Before the action, it will try to scroll to the element and focus on it.
func (*Element) SelectText ¶
SelectText selects the text that matches the regular expression. Before the action, it will try to scroll to the element and focus on it.
func (*Element) ShadowRoot ¶
ShadowRoot returns the shadow root of this element
func (*Element) Shape ¶ added in v0.66.0
func (el *Element) Shape() (*proto.DOMGetContentQuadsResult, error)
Shape of the DOM element content. The shape is a group of 4-sides polygons. A 4-sides polygon is not necessary a rectangle. 4-sides polygons can be apart from each other. For example, we use 2 4-sides polygons to describe the shape below:
____________ ____________ / ___/ = /___________/ + _________ /________/ /________/
func (*Element) Sleeper ¶ added in v0.50.0
Sleeper returns a clone with the specified sleeper for chained sub-operations
func (*Element) Tap ¶ added in v0.61.4
Tap will scroll to the button and tap it just like a human. Before the action, it will try to scroll to the element and wait until it's interactable and enabled.
func (*Element) Timeout ¶
Timeout returns a clone with the specified total timeout of all chained sub-operations
func (*Element) Type ¶ added in v0.107.0
Type is similar with Keyboard.Type. Before the action, it will try to scroll to the element and focus on it.
func (*Element) Wait ¶
func (el *Element) Wait(opts *EvalOptions) error
Wait until the js returns true
func (*Element) WaitEnabled ¶ added in v0.84.1
WaitEnabled until the element is not disabled. Doc for readonly: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/readonly
func (*Element) WaitInteractable ¶ added in v0.88.0
WaitInteractable waits for the element to be interactable. It will try to scroll to the element on each try.
func (*Element) WaitInvisible ¶
WaitInvisible until the element invisible
func (*Element) WaitStable ¶
WaitStable waits until no shape or position change for d duration. Be careful, d is not the max wait timeout, it's the least stable time. If you want to set a timeout you can use the "Element.Timeout" function.
func (*Element) WaitStableRAF ¶ added in v0.84.1
WaitStableRAF waits until no shape or position change for 2 consecutive animation frames. If you want to wait animation that is triggered by JS not CSS, you'd better use Element.WaitStable. About animation frame: https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame
func (*Element) WaitVisible ¶
WaitVisible until the element is visible
func (*Element) WaitWritable ¶ added in v0.84.1
WaitWritable until the element is not readonly. Doc for disabled: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/disabled
func (*Element) WithCancel ¶ added in v0.69.0
WithCancel returns a clone with a context cancel function
type Elements ¶
type Elements []*Element
Elements provides some helpers to deal with element list
type ErrElementNotFound ¶
type ErrElementNotFound struct{}
ErrElementNotFound error
func (*ErrElementNotFound) Error ¶ added in v0.73.0
func (e *ErrElementNotFound) Error() string
type ErrExpectElement ¶
type ErrExpectElement struct {
*proto.RuntimeRemoteObject
}
ErrExpectElement error
func (*ErrExpectElement) Error ¶ added in v0.73.0
func (e *ErrExpectElement) Error() string
func (*ErrExpectElement) Is ¶ added in v0.73.0
func (e *ErrExpectElement) Is(err error) bool
Is interface
type ErrExpectElements ¶
type ErrExpectElements struct {
*proto.RuntimeRemoteObject
}
ErrExpectElements error
func (*ErrExpectElements) Error ¶ added in v0.73.0
func (e *ErrExpectElements) Error() string
func (*ErrExpectElements) Is ¶ added in v0.73.0
func (e *ErrExpectElements) Is(err error) bool
Is interface
type ErrInvisibleShape ¶ added in v0.73.0
type ErrInvisibleShape struct {
*Element
}
ErrInvisibleShape error.
func (*ErrInvisibleShape) Error ¶ added in v0.73.0
func (e *ErrInvisibleShape) Error() string
Error ...
func (*ErrInvisibleShape) Is ¶ added in v0.88.11
func (e *ErrInvisibleShape) Is(err error) bool
Is interface
func (*ErrInvisibleShape) Unwrap ¶ added in v0.73.0
func (e *ErrInvisibleShape) Unwrap() error
Unwrap ...
type ErrNavigation ¶
type ErrNavigation struct {
}ErrNavigation error
func (*ErrNavigation) Error ¶ added in v0.73.0
func (e *ErrNavigation) Error() string
type ErrNoPointerEvents ¶ added in v0.88.7
type ErrNoPointerEvents struct {
*Element
}
ErrNoPointerEvents error.
func (*ErrNoPointerEvents) Error ¶ added in v0.88.7
func (e *ErrNoPointerEvents) Error() string
Error ...
func (*ErrNoPointerEvents) Is ¶ added in v0.88.7
func (e *ErrNoPointerEvents) Is(err error) bool
Is interface
func (*ErrNoPointerEvents) Unwrap ¶ added in v0.88.7
func (e *ErrNoPointerEvents) Unwrap() error
Unwrap ...
type ErrNoShadowRoot ¶ added in v0.112.4
type ErrNoShadowRoot struct {
*Element
}
ErrNoShadowRoot error
func (*ErrNoShadowRoot) Error ¶ added in v0.112.4
func (e *ErrNoShadowRoot) Error() string
Error ...
func (*ErrNoShadowRoot) Is ¶ added in v0.112.4
func (e *ErrNoShadowRoot) Is(err error) bool
Is interface
type ErrNotInteractable ¶ added in v0.66.0
type ErrNotInteractable struct{}
ErrNotInteractable error. Check the doc of Element.Interactable for details.
func (*ErrNotInteractable) Error ¶ added in v0.73.0
func (e *ErrNotInteractable) Error() string
type ErrObjectNotFound ¶ added in v0.74.2
type ErrObjectNotFound struct {
*proto.RuntimeRemoteObject
}
ErrObjectNotFound error
func (*ErrObjectNotFound) Error ¶ added in v0.74.2
func (e *ErrObjectNotFound) Error() string
func (*ErrObjectNotFound) Is ¶ added in v0.74.2
func (e *ErrObjectNotFound) Is(err error) bool
Is interface
type ErrPageCloseCanceled ¶ added in v0.63.0
type ErrPageCloseCanceled struct{}
ErrPageCloseCanceled error
func (*ErrPageCloseCanceled) Error ¶ added in v0.73.0
func (e *ErrPageCloseCanceled) Error() string
type ErrPageNotFound ¶ added in v0.100.0
type ErrPageNotFound struct{}
ErrPageNotFound error
func (*ErrPageNotFound) Error ¶ added in v0.100.0
func (e *ErrPageNotFound) Error() string
type EvalOptions ¶ added in v0.50.0
type EvalOptions struct { // If enabled the eval result will be a plain JSON value. // If disabled the eval result will be a reference of a remote js object. ByValue bool AwaitPromise bool // ThisObj represents the "this" object in the JS ThisObj *proto.RuntimeRemoteObject // JS function definition to execute. JS string // JSArgs represents the arguments that will be passed to JS. // If an argument is *proto.RuntimeRemoteObject type, the corresponding remote object will be used. // Or it will be passed as a plain JSON value. // When an arg in the args is a *js.Function, the arg will be cached on the page's js context. // When the arg.Name exists in the page's cache, it reuse the cache without sending the definition to the browser again. // Useful when you need to eval a huge js expression many times. JSArgs []interface{} // Whether execution should be treated as initiated by user in the UI. UserGesture bool }
EvalOptions for Page.Evaluate
func Eval ¶ added in v0.67.0
func Eval(js string, args ...interface{}) *EvalOptions
Eval creates a EvalOptions with ByValue set to true.
func (*EvalOptions) ByObject ¶ added in v0.50.0
func (e *EvalOptions) ByObject() *EvalOptions
ByObject disables ByValue.
func (*EvalOptions) ByPromise ¶ added in v0.74.0
func (e *EvalOptions) ByPromise() *EvalOptions
ByPromise enables AwaitPromise.
func (*EvalOptions) ByUser ¶ added in v0.64.0
func (e *EvalOptions) ByUser() *EvalOptions
ByUser enables UserGesture.
func (*EvalOptions) String ¶ added in v0.88.0
func (e *EvalOptions) String() string
String interface
func (*EvalOptions) This ¶ added in v0.50.0
func (e *EvalOptions) This(obj *proto.RuntimeRemoteObject) *EvalOptions
This set the obj as ThisObj
type Hijack ¶
type Hijack struct { Request *HijackRequest Response *HijackResponse OnError func(error) // Skip to next handler Skip bool // CustomState is used to store things for this context CustomState interface{} // contains filtered or unexported fields }
Hijack context
func (*Hijack) ContinueRequest ¶ added in v0.42.0
func (h *Hijack) ContinueRequest(cq *proto.FetchContinueRequest)
ContinueRequest without hijacking. The RequestID will be set by the router, you don't have to set it.
func (*Hijack) LoadResponse ¶
LoadResponse will send request to the real destination and load the response as default response to override.
func (*Hijack) MustLoadResponse ¶ added in v0.50.0
func (h *Hijack) MustLoadResponse()
MustLoadResponse is similar to Hijack.LoadResponse
type HijackRequest ¶
type HijackRequest struct {
// contains filtered or unexported fields
}
HijackRequest context
func (*HijackRequest) Body ¶
func (ctx *HijackRequest) Body() string
Body of the request, devtools API doesn't support binary data yet, only string can be captured.
func (*HijackRequest) Headers ¶
func (ctx *HijackRequest) Headers() proto.NetworkHeaders
Headers of request
func (*HijackRequest) IsNavigation ¶ added in v0.97.1
func (ctx *HijackRequest) IsNavigation() bool
IsNavigation determines whether the request is a navigation request
func (*HijackRequest) JSONBody ¶
func (ctx *HijackRequest) JSONBody() gson.JSON
JSONBody of the request
func (*HijackRequest) Req ¶ added in v0.52.0
func (ctx *HijackRequest) Req() *http.Request
Req returns the underlying http.Request instance that will be used to send the request.
func (*HijackRequest) SetBody ¶
func (ctx *HijackRequest) SetBody(obj interface{}) *HijackRequest
SetBody of the request, if obj is []byte or string, raw body will be used, else it will be encoded as json.
func (*HijackRequest) SetContext ¶ added in v0.57.1
func (ctx *HijackRequest) SetContext(c context.Context) *HijackRequest
SetContext of the underlying http.Request instance
func (*HijackRequest) Type ¶ added in v0.49.1
func (ctx *HijackRequest) Type() proto.NetworkResourceType
Type of the resource
type HijackResponse ¶
type HijackResponse struct {
// contains filtered or unexported fields
}
HijackResponse context
func (*HijackResponse) Fail ¶ added in v0.48.1
func (ctx *HijackResponse) Fail(reason proto.NetworkErrorReason) *HijackResponse
Fail request
func (*HijackResponse) Headers ¶
func (ctx *HijackResponse) Headers() http.Header
Headers returns the clone of response headers. If you want to modify the response headers use HijackResponse.SetHeader .
func (*HijackResponse) Payload ¶ added in v0.52.0
func (ctx *HijackResponse) Payload() *proto.FetchFulfillRequest
Payload to respond the request from the browser.
func (*HijackResponse) SetBody ¶
func (ctx *HijackResponse) SetBody(obj interface{}) *HijackResponse
SetBody of the payload, if obj is []byte or string, raw body will be used, else it will be encoded as json.
func (*HijackResponse) SetHeader ¶
func (ctx *HijackResponse) SetHeader(pairs ...string) *HijackResponse
SetHeader of the payload via key-value pairs
type HijackRouter ¶
type HijackRouter struct {
// contains filtered or unexported fields
}
HijackRouter context
func (*HijackRouter) Add ¶
func (r *HijackRouter) Add(pattern string, resourceType proto.NetworkResourceType, handler func(*Hijack)) error
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) MustAdd ¶ added in v0.50.0
func (r *HijackRouter) MustAdd(pattern string, handler func(*Hijack)) *HijackRouter
MustAdd is similar to HijackRouter.Add
func (*HijackRouter) MustRemove ¶ added in v0.50.0
func (r *HijackRouter) MustRemove(pattern string) *HijackRouter
MustRemove is similar to HijackRouter.Remove
func (*HijackRouter) MustStop ¶ added in v0.50.0
func (r *HijackRouter) MustStop()
MustStop is similar to HijackRouter.Stop
func (*HijackRouter) Remove ¶
func (r *HijackRouter) Remove(pattern string) error
Remove handler via the pattern
func (*HijackRouter) Run ¶
func (r *HijackRouter) Run()
Run the router, after you call it, you shouldn't add new handler to it.
type KeyAction ¶ added in v0.107.0
type KeyAction struct { Type KeyActionType Key input.Key }
KeyAction to perform
type KeyActionType ¶ added in v0.107.0
type KeyActionType int
KeyActionType enum
const ( KeyActionPress KeyActionType = iota KeyActionRelease KeyActionTypeKey )
KeyActionTypes
type KeyActions ¶ added in v0.107.0
type KeyActions struct { Actions []KeyAction // contains filtered or unexported fields }
KeyActions to simulate
func (*KeyActions) MustDo ¶ added in v0.107.0
func (ka *KeyActions) MustDo()
MustDo is similar to KeyActions.Do
func (*KeyActions) Press ¶ added in v0.107.0
func (ka *KeyActions) Press(keys ...input.Key) *KeyActions
Press keys is guaranteed to have a release at the end of actions
func (*KeyActions) Release ¶ added in v0.107.0
func (ka *KeyActions) Release(keys ...input.Key) *KeyActions
Release keys
func (*KeyActions) Type ¶ added in v0.107.0
func (ka *KeyActions) Type(keys ...input.Key) *KeyActions
Type will release the key immediately after the pressing
type Keyboard ¶
Keyboard represents the keyboard on a page, it's always related the main frame
func (*Keyboard) Press ¶
Press the key down. To input characters that are not on the keyboard, such as Chinese or Japanese, you should use method like Page.InsertText .
type Message ¶ added in v0.74.0
type Message struct { SessionID proto.TargetSessionID Method string // contains filtered or unexported fields }
Message represents a cdp.Event
type Mouse ¶
Mouse represents the mouse on a page, it's always related the main frame
func (*Mouse) Click ¶
func (m *Mouse) Click(button proto.InputMouseButton, clickCount int) error
Click the button. It's the combination of Mouse.Down and Mouse.Up
func (*Mouse) Down ¶
func (m *Mouse) Down(button proto.InputMouseButton, clickCount int) error
Down holds the button down
func (*Mouse) MoveAlong ¶ added in v0.112.0
MoveAlong the guide function. Every time the guide function is called it should return the next mouse position, return true to stop. Read the source code of Mouse.MoveLinear as an example to use this method.
func (*Mouse) MoveLinear ¶ added in v0.112.0
MoveLinear to the absolute position with the given steps. Such as move from (0,0) to (6,6) with 3 steps, the mouse will first move to (2,2) then (4,4) then (6,6).
func (*Mouse) MustClick ¶ added in v0.50.0
func (m *Mouse) MustClick(button proto.InputMouseButton) *Mouse
MustClick is similar to Mouse.Click
func (*Mouse) MustDown ¶ added in v0.50.0
func (m *Mouse) MustDown(button proto.InputMouseButton) *Mouse
MustDown is similar to Mouse.Down
func (*Mouse) MustMoveTo ¶ added in v0.112.0
MustMoveTo is similar to Mouse.Move
func (*Mouse) MustScroll ¶ added in v0.50.0
MustScroll is similar to Mouse.Scroll
func (*Mouse) MustUp ¶ added in v0.50.0
func (m *Mouse) MustUp(button proto.InputMouseButton) *Mouse
MustUp is similar to Mouse.Up
type Page ¶
type Page struct { // TargetID is a unique ID for a remote page. // It's usually used in events sent from the browser to tell which page an event belongs to. TargetID proto.TargetTargetID // FrameID is a unique ID for a browsing context. // Usually, different FrameID means different javascript execution context. // Such as an iframe and the page it belongs to will have the same TargetID but different FrameIDs. FrameID proto.PageFrameID // SessionID is a unique ID for a page attachment to a controller. // It's usually used in transport layer to tell which page to send the control signal. // A page can attached to multiple controllers, the browser uses it distinguish controllers. SessionID proto.TargetSessionID // devices Mouse *Mouse Keyboard *Keyboard Touch *Touch // contains filtered or unexported fields }
Page represents the webpage. We try to hold as less states as possible. When a page is closed by Rod or not all the ongoing operations an events on it will abort.
Example (Pool) ¶
We can use PagePool to concurrently control and reuse pages.
package main import ( "fmt" "sync" "github.com/go-rod/rod" ) func main() { browser := rod.New().MustConnect() defer browser.MustClose() // We create a pool that will hold at most 3 pages which means the max concurrency is 3 pool := rod.NewPagePool(3) // Create a page if needed create := func() *rod.Page { // We use MustIncognito to isolate pages with each other return browser.MustIncognito().MustPage() } yourJob := func() { page := pool.Get(create) defer pool.Put(page) page.MustNavigate("http://mdn.dev").MustWaitLoad() fmt.Println(page.MustInfo().Title) } // Run jobs concurrently wg := sync.WaitGroup{} for range "...." { wg.Add(1) go func() { defer wg.Done() yourJob() }() } wg.Wait() // cleanup pool pool.Cleanup(func(p *rod.Page) { p.MustClose() }) }
Output: mdn.dev mdn.dev mdn.dev mdn.dev
func (*Page) AddScriptTag ¶
AddScriptTag to page. If url is empty, content will be used.
func (*Page) AddStyleTag ¶
AddStyleTag to page. If url is empty, content will be used.
func (*Page) Call ¶ added in v0.70.0
func (p *Page) Call(ctx context.Context, sessionID, methodName string, params interface{}) (res []byte, err error)
Call implements the proto.Client
func (*Page) CancelTimeout ¶
CancelTimeout cancels the current timeout context and returns a clone with the parent context
func (*Page) CaptureDOMSnapshot ¶ added in v0.113.0
func (p *Page) CaptureDOMSnapshot() (domSnapshot *proto.DOMSnapshotCaptureSnapshotResult, err error)
CaptureDOMSnapshot Returns a document snapshot, including the full DOM tree of the root node (including iframes, template contents, and imported documents) in a flattened array, as well as layout and white-listed computed style information for the nodes. Shadow DOM in the returned DOM tree is flattened. `Documents` The nodes in the DOM tree. The DOMNode at index 0 corresponds to the root document. `Strings` Shared string table that all string properties refer to with indexes. Normally use `Strings` is enough.
func (*Page) Cookies ¶
func (p *Page) Cookies(urls []string) ([]*proto.NetworkCookie, error)
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) DisableDomain ¶
DisableDomain and returns a restore function to restore previous state
func (*Page) EachEvent ¶
func (p *Page) EachEvent(callbacks ...interface{}) (wait func())
EachEvent of the specified event types, if any callback returns true the wait function will resolve, The type of each callback is (? means optional):
func(proto.Event, proto.TargetSessionID?) bool?
You can listen to multiple event types at the same time like:
browser.EachEvent(func(a *proto.A) {}, func(b *proto.B) {})
Such as subscribe the events to know when the navigation is complete or when the page is rendered. Here's an example to dismiss all dialogs/alerts on the page:
go page.EachEvent(func(e *proto.PageJavascriptDialogOpening) { _ = proto.PageHandleJavaScriptDialog{ Accept: false, PromptText: ""}.Call(page) })()
func (*Page) Element ¶
Element retries until an element in the page that matches the CSS selector, then returns the matched element.
func (*Page) ElementByJS ¶
func (p *Page) ElementByJS(opts *EvalOptions) (*Element, error)
ElementByJS returns the element from the return value of the js function. If sleeper is nil, no retry will be performed. By default, it will retry until the js function doesn't return null. To customize the retry logic, check the examples of Page.Sleeper.
func (*Page) ElementFromNode ¶ added in v0.47.0
ElementFromNode creates an Element from the node, NodeID or BackendNodeID must be specified.
func (*Page) ElementFromObject ¶ added in v0.47.0
func (p *Page) ElementFromObject(obj *proto.RuntimeRemoteObject) (*Element, error)
ElementFromObject creates an Element from the remote object id.
func (*Page) ElementFromPoint ¶ added in v0.48.0
ElementFromPoint creates an Element from the absolute point on the page. The point should include the window scroll offset.
func (*Page) ElementR ¶ added in v0.57.0
ElementR retries until an element in the page that matches the css selector and it's text matches the jsRegex, then returns the matched element.
func (*Page) ElementX ¶
ElementX retries until an element in the page that matches one of the XPath selectors, then returns the matched element.
func (*Page) ElementsByJS ¶
func (p *Page) ElementsByJS(opts *EvalOptions) (Elements, error)
ElementsByJS returns the elements from the return value of the js
func (*Page) Emulate ¶ added in v0.42.1
Emulate the device, such as iPhone9. If device is devices.Clear, it will clear the override.
func (*Page) EnableDomain ¶
EnableDomain and returns a restore function to restore previous state
func (*Page) Eval ¶
func (p *Page) Eval(js string, args ...interface{}) (*proto.RuntimeRemoteObject, error)
Eval is a shortcut for Page.Evaluate with AwaitPromise, ByValue set to true.
func (*Page) EvalOnNewDocument ¶ added in v0.44.0
EvalOnNewDocument Evaluates given script in every frame upon creation (before loading frame's scripts).
func (*Page) Evaluate ¶ added in v0.67.0
func (p *Page) Evaluate(opts *EvalOptions) (res *proto.RuntimeRemoteObject, err error)
Evaluate js on the page.
func (*Page) Expose ¶ added in v0.49.1
func (p *Page) Expose(name string, fn func(gson.JSON) (interface{}, error)) (stop func() error, err error)
Expose fn to the page's window object with the name. The exposure survives reloads. Call stop to unbind the fn.
func (*Page) ExposeHelpers ¶ added in v0.85.1
ExposeHelpers helper functions to page's js context so that we can use the Devtools' console to debug them.
func (*Page) GetContext ¶
GetContext of current instance
func (*Page) GetResource ¶ added in v0.76.6
GetResource content by the url. Such as image, css, html, etc. Use the proto.PageGetResourceTree to list all the resources.
func (*Page) GetSessionID ¶ added in v0.72.0
func (p *Page) GetSessionID() proto.TargetSessionID
GetSessionID interface
func (*Page) GetWindow ¶
func (p *Page) GetWindow() (*proto.BrowserBounds, error)
GetWindow position and size info
func (*Page) HandleDialog ¶
func (p *Page) HandleDialog() ( wait func() *proto.PageJavascriptDialogOpening, handle func(*proto.PageHandleJavaScriptDialog) error, )
HandleDialog accepts or dismisses next JavaScript initiated dialog (alert, confirm, prompt, or onbeforeunload). Because modal dialog will block js, usually you have to trigger the dialog in another goroutine. For example:
wait, handle := page.MustHandleDialog() go page.MustElement("button").MustClick() wait() handle(true, "")
func (*Page) HandleFileDialog ¶ added in v0.109.0
HandleFileDialog return a functions that waits for the next file chooser dialog pops up and returns the element for the event.
func (*Page) HasR ¶ added in v0.61.0
HasR an element that matches the css selector and its display text matches the jsRegex.
func (*Page) HijackRequests ¶
func (p *Page) 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. The entire process of hijacking one request:
browser --req-> rod ---> server ---> rod --res-> browser
The --req-> and --res-> are the parts that can be modified.
func (*Page) Info ¶ added in v0.42.1
func (p *Page) Info() (*proto.TargetTargetInfo, error)
Info of the page, such as the URL or title of the page
func (*Page) InsertText ¶ added in v0.107.0
InsertText is like pasting text into the page
func (*Page) KeyActions ¶ added in v0.107.0
func (p *Page) KeyActions() *KeyActions
KeyActions simulates the type actions on a physical keyboard. Useful when input shortcuts like ctrl+enter .
func (*Page) MustActivate ¶ added in v0.86.3
MustActivate is similar to Page.Activate
func (*Page) MustAddScriptTag ¶ added in v0.50.0
MustAddScriptTag is similar to Page.AddScriptTag
func (*Page) MustAddStyleTag ¶ added in v0.50.0
MustAddStyleTag is similar to Page.AddStyleTag
func (*Page) MustCaptureDOMSnapshot ¶ added in v0.113.0
func (p *Page) MustCaptureDOMSnapshot() (domSnapshot *proto.DOMSnapshotCaptureSnapshotResult)
MustCaptureDOMSnapshot is similar to CaptureDOMSnapshot.
func (*Page) MustClose ¶ added in v0.50.0
func (p *Page) MustClose()
MustClose is similar to Page.Close
func (*Page) MustCookies ¶ added in v0.50.0
func (p *Page) MustCookies(urls ...string) []*proto.NetworkCookie
MustCookies is similar to Page.Cookies
func (*Page) MustElement ¶ added in v0.50.0
MustElement is similar to Page.Element
func (*Page) MustElementByJS ¶ added in v0.50.0
MustElementByJS is similar to Page.ElementByJS
func (*Page) MustElementFromNode ¶ added in v0.50.0
MustElementFromNode is similar to Page.ElementFromNode
func (*Page) MustElementFromPoint ¶ added in v0.50.0
MustElementFromPoint is similar to Page.ElementFromPoint
func (*Page) MustElementR ¶ added in v0.57.0
MustElementR is similar to Page.ElementR
func (*Page) MustElementX ¶ added in v0.50.0
MustElementX is similar to Page.ElementX
func (*Page) MustElements ¶ added in v0.50.0
MustElements is similar to Page.Elements
func (*Page) MustElementsByJS ¶ added in v0.50.0
MustElementsByJS is similar to Page.ElementsByJS
func (*Page) MustElementsX ¶ added in v0.50.0
MustElementsX is similar to Page.ElementsX
func (*Page) MustEmulate ¶ added in v0.50.0
MustEmulate is similar to Page.Emulate
func (*Page) MustEvalOnNewDocument ¶ added in v0.50.0
MustEvalOnNewDocument is similar to Page.EvalOnNewDocument
func (*Page) MustEvaluate ¶ added in v0.67.0
func (p *Page) MustEvaluate(opts *EvalOptions) *proto.RuntimeRemoteObject
MustEvaluate is similar to Page.Evaluate
func (*Page) MustExpose ¶ added in v0.50.0
MustExpose is similar to Page.Expose
func (*Page) MustGetWindow ¶ added in v0.50.0
func (p *Page) MustGetWindow() *proto.BrowserBounds
MustGetWindow is similar to Page.GetWindow
func (*Page) MustHandleDialog ¶ added in v0.50.0
func (p *Page) MustHandleDialog() (wait func() *proto.PageJavascriptDialogOpening, handle func(bool, string))
MustHandleDialog is similar to Page.HandleDialog
func (*Page) MustHandleFileDialog ¶ added in v0.109.0
MustHandleFileDialog is similar to Page.HandleFileDialog
func (*Page) MustInfo ¶ added in v0.50.0
func (p *Page) MustInfo() *proto.TargetTargetInfo
MustInfo is similar to Page.Info
func (*Page) MustInsertText ¶ added in v0.107.0
MustInsertText is similar to Page.InsertText
func (*Page) MustNavigate ¶ added in v0.50.0
MustNavigate is similar to Page.Navigate
func (*Page) MustNavigateBack ¶ added in v0.61.4
MustNavigateBack is similar to Page.NavigateBack
func (*Page) MustNavigateForward ¶ added in v0.61.4
MustNavigateForward is similar to Page.NavigateForward
func (*Page) MustObjectToJSON ¶ added in v0.50.0
func (p *Page) MustObjectToJSON(obj *proto.RuntimeRemoteObject) gson.JSON
MustObjectToJSON is similar to Page.ObjectToJSON
func (*Page) MustObjectsToJSON ¶ added in v0.50.0
func (p *Page) MustObjectsToJSON(list []*proto.RuntimeRemoteObject) gson.JSON
MustObjectsToJSON is similar to Page.ObjectsToJSON
func (*Page) MustPDF ¶ added in v0.50.0
MustPDF is similar to PDF. If the toFile is "", it Page.will save output to "tmp/pdf" folder, time as the file name.
func (*Page) MustRelease ¶ added in v0.50.0
func (p *Page) MustRelease(obj *proto.RuntimeRemoteObject) *Page
MustRelease is similar to Page.Release
func (*Page) MustReload ¶ added in v0.61.4
MustReload is similar to Page.Reload
func (*Page) MustScreenshot ¶ added in v0.50.0
MustScreenshot is similar to Screenshot. If the toFile is "", it Page.will save output to "tmp/screenshots" folder, time as the file name.
func (*Page) MustScreenshotFullPage ¶ added in v0.50.0
MustScreenshotFullPage is similar to ScreenshotFullPage. If the toFile is "", it Page.will save output to "tmp/screenshots" folder, time as the file name.
func (*Page) MustSearch ¶ added in v0.50.0
MustSearch is similar to Page.Search . It only returns the first element in the search result.
func (*Page) MustSetBlockedURLs ¶ added in v0.112.3
MustSetBlockedURLs is similar to Page.SetBlockedURLs
func (*Page) MustSetCookies ¶ added in v0.50.0
func (p *Page) MustSetCookies(cookies ...*proto.NetworkCookieParam) *Page
MustSetCookies is similar to Page.SetCookies. If the len(cookies) is 0 it will clear all the cookies.
func (*Page) MustSetDocumentContent ¶ added in v0.104.0
MustSetDocumentContent is similar to Page.SetDocumentContent
func (*Page) MustSetExtraHeaders ¶ added in v0.50.0
MustSetExtraHeaders is similar to Page.SetExtraHeaders
func (*Page) MustSetUserAgent ¶ added in v0.50.0
func (p *Page) MustSetUserAgent(req *proto.NetworkSetUserAgentOverride) *Page
MustSetUserAgent is similar to Page.SetUserAgent
func (*Page) MustSetViewport ¶ added in v0.64.0
MustSetViewport is similar to Page.SetViewport
func (*Page) MustSetWindow ¶ added in v0.64.0
MustSetWindow is similar to Page.SetWindow
func (*Page) MustStopLoading ¶ added in v0.50.0
MustStopLoading is similar to Page.StopLoading
func (*Page) MustTriggerFavicon ¶ added in v0.113.2
MustTriggerFavicon is similar to TriggerFavicon.
func (*Page) MustWaitElementsMoreThan ¶ added in v0.97.3
MustWaitElementsMoreThan is similar to Page.WaitElementsMoreThan
func (*Page) MustWaitIdle ¶ added in v0.50.0
MustWaitIdle is similar to Page.WaitIdle
func (*Page) MustWaitLoad ¶ added in v0.50.0
MustWaitLoad is similar to Page.WaitLoad
func (*Page) MustWaitNavigation ¶ added in v0.63.2
func (p *Page) MustWaitNavigation() func()
MustWaitNavigation is similar to Page.WaitNavigation
func (*Page) MustWaitOpen ¶ added in v0.50.0
MustWaitOpen is similar to Page.WaitOpen
func (*Page) MustWaitRequestIdle ¶ added in v0.50.0
MustWaitRequestIdle is similar to Page.WaitRequestIdle
func (*Page) MustWaitStable ¶ added in v0.113.0
MustWaitStable is similar to Page.WaitStable
func (*Page) MustWindowFullscreen ¶ added in v0.50.0
MustWindowFullscreen is similar to Page.WindowFullscreen
func (*Page) MustWindowMaximize ¶ added in v0.50.0
MustWindowMaximize is similar to Page.WindowMaximize
func (*Page) MustWindowMinimize ¶ added in v0.50.0
MustWindowMinimize is similar to Page.WindowMinimize
func (*Page) MustWindowNormal ¶ added in v0.50.0
MustWindowNormal is similar to Page.WindowNormal
func (*Page) Navigate ¶
Navigate to the url. If the url is empty, "about:blank" will be used. It will return immediately after the server responds the http header.
func (*Page) NavigateBack ¶ added in v0.61.4
NavigateBack history.
func (*Page) NavigateForward ¶ added in v0.61.4
NavigateForward history.
func (*Page) ObjectToJSON ¶
ObjectToJSON by object id
func (*Page) PDF ¶
func (p *Page) PDF(req *proto.PagePrintToPDF) (*StreamReader, error)
PDF prints page as PDF
func (*Page) Race ¶ added in v0.57.0
func (p *Page) Race() *RaceContext
Race creates a context to race selectors
func (*Page) Release ¶
func (p *Page) Release(obj *proto.RuntimeRemoteObject) error
Release the remote object. Usually, you don't need to call it. When a page is closed or reloaded, all remote objects will be released automatically. It's useful if the page never closes or reloads.
func (*Page) Screenshot ¶
Screenshot captures the screenshot of current page.
func (*Page) Search ¶ added in v0.47.0
func (p *Page) Search(query string) (*SearchResult, error)
Search for the given query in the DOM tree until the result count is not zero, before that it will keep retrying. The query can be plain text or css selector or xpath. It will search nested iframes and shadow doms too.
func (*Page) SetBlockedURLs ¶ added in v0.112.3
SetBlockedURLs For some requests that do not want to be triggered, such as some dangerous operations, delete, quit logout, etc. Wildcards ('*') are allowed, such as ["*/api/logout/*","delete"]. NOTE: if you set empty pattern "", it will block all requests.
func (*Page) SetCookies ¶
func (p *Page) SetCookies(cookies []*proto.NetworkCookieParam) error
SetCookies is similar to Browser.SetCookies .
func (*Page) SetDocumentContent ¶ added in v0.104.0
SetDocumentContent sets the page document html content
func (*Page) SetExtraHeaders ¶
SetExtraHeaders whether to always send extra HTTP headers with the requests from this page.
func (*Page) SetUserAgent ¶
func (p *Page) SetUserAgent(req *proto.NetworkSetUserAgentOverride) error
SetUserAgent (browser brand, accept-language, etc) of the page. If req is nil, a default user agent will be used, a typical mac chrome.
func (*Page) SetViewport ¶ added in v0.62.0
func (p *Page) SetViewport(params *proto.EmulationSetDeviceMetricsOverride) error
SetViewport overrides the values of device screen dimensions
func (*Page) SetWindow ¶ added in v0.62.0
func (p *Page) SetWindow(bounds *proto.BrowserBounds) error
SetWindow location and size
func (*Page) Sleeper ¶
Sleeper returns a clone with the specified sleeper for chained sub-operations
func (*Page) StopLoading ¶
StopLoading forces the page stop navigation and pending resource fetches.
func (*Page) Timeout ¶
Timeout returns a clone with the specified total timeout of all chained sub-operations
func (*Page) TriggerFavicon ¶ added in v0.113.2
TriggerFavicon supports when browser in headless mode to trigger favicon's request. Pay attention to this function only supported when browser in headless mode, if you call it in no-headless mode, it will raise an error with the message "browser is no-headless".
func (*Page) WaitElementsMoreThan ¶ added in v0.97.3
WaitElementsMoreThan Wait until there are more than <num> <selector> elements.
func (*Page) WaitEvent ¶
WaitEvent waits for the next event for one time. It will also load the data into the event object.
func (*Page) WaitLoad ¶
WaitLoad waits for the `window.onload` event, it returns immediately if the event is already fired.
func (*Page) WaitNavigation ¶ added in v0.63.2
func (p *Page) WaitNavigation(name proto.PageLifecycleEventName) func()
WaitNavigation wait for a page lifecycle event when navigating. Usually you will wait for proto.PageLifecycleEventNameNetworkAlmostIdle
func (*Page) WaitRepaint ¶ added in v0.84.1
WaitRepaint waits until the next repaint. Doc: https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame
func (*Page) WaitRequestIdle ¶
WaitRequestIdle returns a wait function that waits until no request for d duration. Be careful, d is not the max wait timeout, it's the least idle time. If you want to set a timeout you can use the "Page.Timeout" function. Use the includes and excludes regexp list to filter the requests by their url.
func (*Page) WaitStable ¶ added in v0.113.0
WaitStable like "Element.WaitStable". WaitStable polling the changes of the DOM tree in `d` duration,until the similarity equal or more than simThreshold. `simThreshold` is the similarity threshold,it's scope in [0,1]. Be careful,d is not the max wait timeout, it's the least stable time. If you want to set a timeout you can use the "Page.Timeout" function.
func (*Page) WithCancel ¶ added in v0.69.0
WithCancel returns a clone with a context cancel function
type PagePool ¶ added in v0.73.2
type PagePool chan *Page
PagePool to thread-safely limit the number of pages at the same time. It's a common practice to use a channel to limit concurrency, it's not special for rod. This helper is more like an example to use Go Channel. Reference: https://golang.org/doc/effective_go#channels
type Pages ¶
type Pages []*Page
Pages provides some helpers to deal with page list
func (Pages) First ¶ added in v0.53.0
First returns the first page, if the list is empty returns nil
func (Pages) MustFindByURL ¶ added in v0.50.0
MustFindByURL is similar to Page.FindByURL
type RaceContext ¶ added in v0.57.0
type RaceContext struct {
// contains filtered or unexported fields
}
RaceContext stores the branches to race
func (*RaceContext) Element ¶ added in v0.57.0
func (rc *RaceContext) Element(selector string) *RaceContext
Element the doc is similar to MustElement
func (*RaceContext) ElementByJS ¶ added in v0.57.0
func (rc *RaceContext) ElementByJS(opts *EvalOptions) *RaceContext
ElementByJS the doc is similar to MustElementByJS
func (*RaceContext) ElementFunc ¶ added in v0.107.1
func (rc *RaceContext) ElementFunc(fn func(*Page) (*Element, error)) *RaceContext
ElementFunc takes a custom function to determine race success
func (*RaceContext) ElementR ¶ added in v0.57.0
func (rc *RaceContext) ElementR(selector, regex string) *RaceContext
ElementR the doc is similar to ElementR
func (*RaceContext) ElementX ¶ added in v0.57.0
func (rc *RaceContext) ElementX(selector string) *RaceContext
ElementX the doc is similar to ElementX
func (*RaceContext) Handle ¶ added in v0.81.0
func (rc *RaceContext) Handle(callback func(*Element) error) *RaceContext
Handle adds a callback function to the most recent chained selector. The callback function is run, if the corresponding selector is present first, in the Race condition.
func (*RaceContext) MustDo ¶ added in v0.57.0
func (rc *RaceContext) MustDo() *Element
MustDo is similar to RaceContext.Do
func (*RaceContext) MustElementByJS ¶ added in v0.57.0
func (rc *RaceContext) MustElementByJS(js string, params []interface{}) *RaceContext
MustElementByJS is similar to RaceContext.ElementByJS
func (*RaceContext) MustHandle ¶ added in v0.81.0
func (rc *RaceContext) MustHandle(callback func(*Element)) *RaceContext
MustHandle is similar to RaceContext.Handle
func (*RaceContext) Search ¶ added in v0.112.0
func (rc *RaceContext) Search(query string) *RaceContext
Search the doc is similar to MustSearch
type SearchResult ¶ added in v0.97.0
type SearchResult struct { *proto.DOMPerformSearchResult // First element in the search result First *Element // contains filtered or unexported fields }
SearchResult handler
func (*SearchResult) All ¶ added in v0.97.0
func (s *SearchResult) All() (Elements, error)
All returns all elements
func (*SearchResult) Get ¶ added in v0.97.0
func (s *SearchResult) Get(i, l int) (Elements, error)
Get l elements at the index of i from the remote search result.
func (*SearchResult) Release ¶ added in v0.97.0
func (s *SearchResult) Release()
Release the remote search result
type SelectorType ¶ added in v0.68.0
type SelectorType string
SelectorType enum
const ( // SelectorTypeRegex type SelectorTypeRegex SelectorType = "regex" // SelectorTypeCSSSector type SelectorTypeCSSSector SelectorType = "css-selector" // SelectorTypeText type SelectorTypeText SelectorType = "text" )
type StreamReader ¶ added in v0.63.0
type StreamReader struct { Offset *int // contains filtered or unexported fields }
StreamReader for browser data stream
func NewStreamReader ¶ added in v0.63.0
func NewStreamReader(c proto.Client, h proto.IOStreamHandle) *StreamReader
NewStreamReader instance
func (*StreamReader) Close ¶ added in v0.102.0
func (sr *StreamReader) Close() error
Close the stream, discard any temporary backing storage.
type Touch ¶ added in v0.61.1
type Touch struct {
// contains filtered or unexported fields
}
Touch presents a touch device, such as a hand with fingers, each finger is a proto.InputTouchPoint. Touch events is stateless, we use the struct here only as a namespace to make the API style unified.
func (*Touch) Move ¶ added in v0.61.1
func (t *Touch) Move(points ...*proto.InputTouchPoint) error
Move touch points. Use the InputTouchPoint.ID (Touch.identifier) to track points. Doc: https://developer.mozilla.org/en-US/docs/Web/API/Touch_events
func (*Touch) MustCancel ¶ added in v0.61.1
MustCancel is similar to Touch.Cancel
func (*Touch) MustMove ¶ added in v0.61.1
func (t *Touch) MustMove(points ...*proto.InputTouchPoint) *Touch
MustMove is similar to Touch.Move
func (*Touch) MustStart ¶ added in v0.61.1
func (t *Touch) MustStart(points ...*proto.InputTouchPoint) *Touch
MustStart is similar to Touch.Start
type TraceType ¶ added in v0.59.0
type TraceType string
TraceType for logger
const ( // TraceTypeWaitRequestsIdle type TraceTypeWaitRequestsIdle TraceType = "wait requests idle" // TraceTypeWaitRequests type TraceTypeWaitRequests TraceType = "wait requests" // TraceTypeQuery type TraceTypeQuery TraceType = "query" // TraceTypeWait type TraceTypeWait TraceType = "wait" // TraceTypeInput type TraceTypeInput TraceType = "input" )
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
fixtures
|
|
gen-fonts
Package main ...
|
Package main ... |
lib
|
|
assets
Package assets is generated by "lib/assets/generate"
|
Package assets is generated by "lib/assets/generate" |
assets/generate
Package main ...
|
Package main ... |
cdp
Package cdp for application layer communication with browser.
|
Package cdp for application layer communication with browser. |
defaults
Package defaults of commonly used options parsed from environment.
|
Package defaults of commonly used options parsed from environment. |
devices
Package devices ...
|
Package devices ... |
devices/generate
Package main ...
|
Package main ... |
docker
The .github/workflows/docker.yml uses it as an github action and run it like this:
|
The .github/workflows/docker.yml uses it as an github action and run it like this: |
examples/compare-chromedp/click
Package main ...
|
Package main ... |
examples/compare-chromedp/cookie
Package main ...
|
Package main ... |
examples/compare-chromedp/download_file
Package main ...
|
Package main ... |
examples/compare-chromedp/download_image
Package main ...
|
Package main ... |
examples/compare-chromedp/emulate
Package main ...
|
Package main ... |
examples/compare-chromedp/eval
Package main ...
|
Package main ... |
examples/compare-chromedp/headers
Package main ...
|
Package main ... |
examples/compare-chromedp/keys
Package main ...
|
Package main ... |
examples/compare-chromedp/logic
Package main ...
|
Package main ... |
examples/compare-chromedp/pdf
Package main ...
|
Package main ... |
examples/compare-chromedp/proxy
Package main ...
|
Package main ... |
examples/compare-chromedp/remote
Package main ...
|
Package main ... |
examples/compare-chromedp/screenshot
Package main ...
|
Package main ... |
examples/compare-chromedp/submit
Package main ...
|
Package main ... |
examples/compare-chromedp/subtree
Package main ...
|
Package main ... |
examples/compare-chromedp/text
Package main ...
|
Package main ... |
examples/compare-chromedp/upload
Package main ...
|
Package main ... |
examples/compare-chromedp/visible
Package main ...
|
Package main ... |
examples/connect-browser
Package main ...
|
Package main ... |
examples/custom-launch
Package main ...
|
Package main ... |
examples/debug-deadlock
Package main ...
|
Package main ... |
examples/disable-window-alert
Package main ...
|
Package main ... |
examples/launch-managed
Package main ...
|
Package main ... |
examples/stripe
Package main ...
|
Package main ... |
examples/translator
Package main ...
|
Package main ... |
examples/use-rod-like-chrome-extension
Package main ...
|
Package main ... |
input
Package input ...
|
Package input ... |
js
Package js generated by "lib/js/generate"
|
Package js generated by "lib/js/generate" |
js/generate
Package main ...
|
Package main ... |
launcher
Package launcher for launching browser utils.
|
Package launcher for launching browser utils. |
launcher/fixtures/chrome-empty
Package main ...
|
Package main ... |
launcher/fixtures/chrome-exit-err
Package main ...
|
Package main ... |
launcher/fixtures/chrome-lib-missing
Package main ...
|
Package main ... |
launcher/flags
Package flags ...
|
Package flags ... |
launcher/revision
Package main ...
|
Package main ... |
launcher/rod-manager
A server to help launch browser remotely
|
A server to help launch browser remotely |
proto
Package proto is a lib to encode/decode the data of the cdp protocol.
|
Package proto is a lib to encode/decode the data of the cdp protocol. |
proto/generate
Package main ...
|
Package main ... |
utils
Package utils ...
|
Package utils ... |
utils/ci-test
Package main A helper to run go test on CI with the right environment variables.
|
Package main A helper to run go test on CI with the right environment variables. |
utils/get-browser
Package main ...
|
Package main ... |
utils/lint
Package main ...
|
Package main ... |
utils/rename
Package main ...
|
Package main ... |
utils/setup
Package main ...
|
Package main ... |
utils/shell
Package main It helps to launcher a transparent shell under the current shell with some extra environment variables that are required by rod testing.
|
Package main It helps to launcher a transparent shell under the current shell with some extra environment variables that are required by rod testing. |
utils/simple-check
Package main ...
|
Package main ... |
examples/custom-websocket
Module
|
|
utils/check-issue
Module
|