Documentation ¶
Overview ¶
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").MustPress(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_timeout) ¶
Rod use https://golang.org/pkg/context to handle cancelations 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://example.com/").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") // If sleeper is nil page.ElementE will query without retrying. // If nothing found it will return an error. el, err := page.Sleeper(nil).Element("input") if errors.Is(err, &rod.ErrElementNotFound{}) { fmt.Println("element not found") } else if err != nil { panic(err) } fmt.Println(el.MustEval(`this.name`).String()) }
Output: 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://chromedevtools.github.io/devtools-protocol
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. // Doc: https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-setAdBlockingEnabled // 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 env "rod=show" // Devtools opens the tab in each new tab opened automatically l := launcher.New(). Headless(false). Devtools(true) defer l.Cleanup() // remove user-data-dir 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 env rod=monitor launcher.NewBrowser().Open(browser.ServeMonitor("")) defer browser.MustClose() page := browser.MustPage("https://www.wikipedia.org/") page.MustElement("#searchLanguage").MustSelect("[lang=zh]") page.MustElement("#searchInput").MustInput("热干面") page.Keyboard.MustPress(input.Enter) fmt.Println(page.MustElement("#firstHeading").MustText()) // Response gets the binary of the image as a []byte. img := page.MustElement(`[alt="Hot Dry Noodles.jpg"]`).MustResource() fmt.Println(len(img)) // print the size of the image utils.Pause() // pause goroutine }
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://example.com") // 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 ( "context" "fmt" "github.com/go-rod/rod" "github.com/go-rod/rod/lib/proto" ) func main() { browser := rod.New().MustConnect() defer browser.MustClose() ctx, cancel := context.WithCancel(context.Background()) defer cancel() page := browser.Context(ctx).MustPage("") done := make(chan int) // Listen for all events of console output. You can even listen to multiple types of events at that same time, // check the doc of EachEvent for details. go page.EachEvent(func(e *proto.RuntimeConsoleAPICalled) { fmt.Println(page.MustObjectsToJSON(e.Args)) close(done) })() wait := page.WaitEvent(&proto.PageLoadEventFired{}) page.MustNavigate("https://example.com") 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://example.com") wait() } // Or the for-loop style to handle events to do the same thing above. if false { page.MustNavigate("https://example.com") 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" "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.MustLoadResponse() // 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://www.wikipedia.org/").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(). Set("load-extension", extPath). // must use abs path for an extension Headless(false). // headless mode doesn't support extension yet MustLaunch() page := rod.New().ControlURL(u).MustConnect().MustPage("http://example.com") page.MustWait(`document.title === 'test-extension'`) fmt.Println("ok") // Skip
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" ) func main() { page := rod.New().MustConnect().MustPage("") wait := page.MustWaitNavigation() page.MustNavigate("https://github.com") wait() // until the navigation to settle down // simple version page.MustPDF("my.pdf") // customization version pdf, _ := page.PDF(&proto.PagePrintToPDF{ PaperWidth: 8.5, PaperHeight: 11, PageRanges: "1-3", IgnoreInvalidPageRanges: false, DisplayHeaderFooter: true, }) _ = 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" ) func main() { page := rod.New().MustConnect().MustPage("") wait := page.MustWaitNavigation() page.MustNavigate("https://github.com") wait() // until the navigation to settle down // simple version page.MustScreenshot("my.png") // customization version img, _ := page.Screenshot(true, &proto.PageCaptureScreenshot{ Format: proto.PageCaptureScreenshotFormatJpeg, Quality: 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).MustPress(input.Enter) // It will keep retrying until one selector has found a match page.Race().MustElement(".nav-user-icon-base", func(el *rod.Element) { // print the username after successful login fmt.Println(*el.MustAttribute("title")) }).MustElement("[data-cy=sign-in-error]", func(el *rod.Element) { // when wrong username or password panic(el.MustText()) }).MustDo() }
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://duckduckgo.com/") // Start to analyze request events wait := page.MustWaitRequestIdle() // This will trigger the search ajax request page.MustElement("#search_form_input_homepage").MustClick().MustInput("test") // 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(".search__autocomplete .acp")) > 0) }
Output: true
Index ¶
- Variables
- 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, landscape bool) *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) Headless() bool
- 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) 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) WaitEvent(e proto.Event) (wait func())
- func (b *Browser) WithCancel() (*Browser, func())
- 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) 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) Element(selectors ...string) (*Element, error)
- func (el *Element) ElementByJS(opts *EvalOptions) (*Element, error)
- func (el *Element) ElementR(pairs ...string) (*Element, error)
- func (el *Element) ElementX(xPaths ...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) 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) HTML() (string, error)
- func (el *Element) Has(selector string) (bool, *Element, error)
- func (el *Element) HasR(selector, regex 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) Interactable() (pt *proto.Point, err error)
- func (el *Element) Matches(selector string) (bool, 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) 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) MustEval(js string, params ...interface{}) gson.JSON
- func (el *Element) MustFocus() *Element
- func (el *Element) MustFrame() *Page
- 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) MustInteractable() bool
- func (el *Element) MustMatches(selector string) bool
- func (el *Element) MustNext() *Element
- func (el *Element) MustNodeID() proto.DOMNodeID
- func (el *Element) MustParent() *Element
- func (el *Element) MustParents(selector string) Elements
- func (el *Element) MustPress(key rune) *Element
- 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) MustVisible() bool
- func (el *Element) MustWait(js string, params ...interface{}) *Element
- func (el *Element) MustWaitInvisible() *Element
- func (el *Element) MustWaitLoad() *Element
- func (el *Element) MustWaitStable() *Element
- func (el *Element) MustWaitVisible() *Element
- func (el *Element) Next() (*Element, error)
- func (el *Element) NodeID() (proto.DOMNodeID, error)
- func (el *Element) Parent() (*Element, error)
- func (el *Element) Parents(selector string) (Elements, error)
- func (el *Element) Press(key rune) 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) Tap() error
- func (el *Element) Text() (string, error)
- func (el *Element) Timeout(d time.Duration) *Element
- func (el *Element) Trace(msg string) (removeOverlay func())
- func (el *Element) Visible() (bool, error)
- func (el *Element) Wait(opts *EvalOptions) error
- func (el *Element) WaitInvisible() error
- func (el *Element) WaitLoad() error
- func (el *Element) WaitStable(d time.Duration) error
- func (el *Element) WaitVisible() error
- func (el *Element) WithCancel() (*Element, func())
- type Elements
- type ErrCovered
- type ErrElementNotFound
- type ErrEval
- type ErrExpectElement
- type ErrExpectElements
- type ErrInvisibleShape
- type ErrNavigation
- type ErrNotInteractable
- type ErrObjectNotFound
- type ErrPageCloseCanceled
- 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) 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 Keyboard
- func (k *Keyboard) Down(key rune) error
- func (k *Keyboard) InsertText(text string) error
- func (k *Keyboard) MustDown(key rune) *Keyboard
- func (k *Keyboard) MustInsertText(text string) *Keyboard
- func (k *Keyboard) MustPress(key rune) *Keyboard
- func (k *Keyboard) MustUp(key rune) *Keyboard
- func (k *Keyboard) Press(key rune) error
- func (k *Keyboard) Up(key rune) error
- type Message
- type Mouse
- func (m *Mouse) Click(button proto.InputMouseButton) error
- func (m *Mouse) Down(button proto.InputMouseButton, clicks int) error
- func (m *Mouse) Move(x, y float64, steps int) error
- func (m *Mouse) MustClick(button proto.InputMouseButton) *Mouse
- func (m *Mouse) MustDown(button proto.InputMouseButton) *Mouse
- func (m *Mouse) MustMove(x, y float64) *Mouse
- func (m *Mouse) MustScroll(x, y float64) *Mouse
- func (m *Mouse) MustUp(button proto.InputMouseButton) *Mouse
- func (m *Mouse) Scroll(offsetX, offsetY float64, steps int) error
- func (m *Mouse) Up(button proto.InputMouseButton, clicks int) error
- type Page
- func (p *Page) AddScriptTag(url, content string) error
- func (p *Page) AddStyleTag(url, content string) error
- func (p *Page) Call(ctx context.Context, sessionID, methodName string, params interface{}) (res []byte, err error)
- func (p *Page) CancelTimeout() *Page
- 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(selectors ...string) (*Element, error)
- func (p *Page) ElementByJS(opts *EvalOptions) (*Element, error)
- func (p *Page) ElementFromNode(id proto.DOMNodeID) (*Element, error)
- func (p *Page) ElementFromObject(obj *proto.RuntimeRemoteObject) *Element
- func (p *Page) ElementFromPoint(x, y int) (*Element, error)
- func (p *Page) ElementR(pairs ...string) (*Element, error)
- func (p *Page) ElementX(xPaths ...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, landscape bool) error
- func (p *Page) EnableDomain(method proto.Request) (restore func())
- func (p *Page) Eval(js string, jsArgs ...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) GetContext() context.Context
- func (p *Page) GetDownloadFile(pattern string, resourceType proto.NetworkResourceType, client *http.Client) func() (http.Header, []byte, error)
- func (p *Page) GetResource(url string) ([]byte, error)
- func (p *Page) GetSessionID() proto.TargetSessionID
- func (p *Page) GetWindow() (*proto.BrowserBounds, error)
- func (p *Page) HandleDialog(accept bool, promptText string) func() error
- func (p *Page) Has(selectors ...string) (bool, *Element, error)
- func (p *Page) HasR(selector, regex string) (bool, *Element, error)
- func (p *Page) HasX(selectors ...string) (bool, *Element, error)
- func (p *Page) HijackRequests() *HijackRouter
- func (p *Page) Info() (*proto.TargetTargetInfo, error)
- func (p *Page) IsIframe() bool
- func (p *Page) LoadState(method proto.Request) (has bool)
- func (p *Page) MustAddScriptTag(url string) *Page
- func (p *Page) MustAddStyleTag(url string) *Page
- func (p *Page) MustClose()
- func (p *Page) MustCookies(urls ...string) []*proto.NetworkCookie
- func (p *Page) MustElement(selectors ...string) *Element
- func (p *Page) MustElementByJS(js string, params ...interface{}) *Element
- func (p *Page) MustElementFromNode(id proto.DOMNodeID) *Element
- func (p *Page) MustElementFromPoint(left, top int) *Element
- func (p *Page) MustElementR(pairs ...string) *Element
- func (p *Page) MustElementX(xPaths ...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) MustGetDownloadFile(pattern string) func() []byte
- func (p *Page) MustGetWindow() *proto.BrowserBounds
- func (p *Page) MustHandleDialog(accept bool, promptText string) (wait func())
- 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) 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(queries ...string) *Element
- func (p *Page) MustSetCookies(cookies ...*proto.NetworkCookieParam) *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) MustWait(js string, params ...interface{}) *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) MustWaitPauseOpen() (wait func() (p *Page, resume func()))
- func (p *Page) MustWaitRequestIdle(excludes ...string) (wait func())
- 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(from, to int, queries ...string) (Elements, error)
- func (p *Page) SetCookies(cookies []*proto.NetworkCookieParam) 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) Timeout(d time.Duration) *Page
- func (p *Page) Wait(this *proto.RuntimeRemoteObject, js string, params []interface{}) 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) WaitPauseOpen() (func() (*Page, func() error, error), error)
- func (p *Page) WaitRequestIdle(d time.Duration, includes, excludes []string) func()
- func (p *Page) WithCancel() (*Page, func())
- type PagePool
- type Pages
- type RaceContext
- func (rc *RaceContext) Do() error
- func (rc *RaceContext) Element(selector string, callback func(*Element) error) *RaceContext
- func (rc *RaceContext) ElementByJS(opts *EvalOptions, callback func(*Element) error) *RaceContext
- func (rc *RaceContext) ElementR(selector, regex string, callback func(*Element) error) *RaceContext
- func (rc *RaceContext) ElementX(selector string, callback func(*Element) error) *RaceContext
- func (rc *RaceContext) MustDo() *Page
- func (rc *RaceContext) MustElement(selector string, callback func(*Element)) *RaceContext
- func (rc *RaceContext) MustElementByJS(js string, params []interface{}, callback func(*Element) error) *RaceContext
- func (rc *RaceContext) MustElementR(selector, regex string, callback func(*Element)) *RaceContext
- func (rc *RaceContext) MustElementX(selector string, callback func(*Element)) *RaceContext
- 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 TraceMsg
- type TraceType
Examples ¶
- Package
- Package (Context_and_timeout)
- Package (Customize_browser_launch)
- Package (Customize_retry_strategy)
- Package (Direct_cdp)
- Package (Disable_headless_to_debug)
- Package (Error_handling)
- Package (Eval_reuse_remote_object)
- Package (Handle_events)
- Package (Hijack_requests)
- Package (Load_extension)
- 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
Functions ¶
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 (*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 ¶
DefaultDevice sets the default device for new page 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 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) {})
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 ¶
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 creates a new router instance for requests hijacking. When use Fetch domain outside the router should be stopped. Enabling hijacking disables page caching, but such as 304 Not Modified will still work as expected.
func (*Browser) IgnoreCertErrors ¶
IgnoreCertErrors switch. If enabled, all certificate errors will be ignored.
func (*Browser) MustConnect ¶
MustConnect is similar to Connect
func (*Browser) MustGetCookies ¶
func (b *Browser) MustGetCookies() []*proto.NetworkCookie
MustGetCookies is similar GetCookies
func (*Browser) MustHandleAuth ¶
MustHandleAuth is similar to HandleAuth
func (*Browser) MustIgnoreCertErrors ¶
MustIgnoreCertErrors is similar to IgnoreCertErrors
func (*Browser) MustIncognito ¶
MustIncognito is similar to Incognito
func (*Browser) MustPageFromTargetID ¶
func (b *Browser) MustPageFromTargetID(targetID proto.TargetTargetID) *Page
MustPageFromTargetID is similar to PageFromTargetID
func (*Browser) MustSetCookies ¶
func (b *Browser) MustSetCookies(cookies []*proto.NetworkCookie) *Browser
MustSetCookies is similar SetCookies
func (*Browser) Page ¶
func (b *Browser) Page(opts proto.TargetCreateTarget) (p *Page, err error)
Page creates a new browser tab. If url is empty, the default target will be "about:blank".
func (*Browser) PageFromSession ¶
func (b *Browser) PageFromSession(sessionID proto.TargetSessionID) *Page
PageFromSession is used for low-level debugging
func (*Browser) PageFromTarget ¶
func (b *Browser) PageFromTarget(targetID proto.TargetTargetID) (*Page, error)
PageFromTarget gets or creates a Page instance.
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 ¶
func (b *Browser) SetCookies(cookies []*proto.NetworkCookieParam) error
SetCookies to the browser
func (*Browser) SlowMotion ¶
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 timeout context.Context chained sub-operations
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 ¶
WithCancel returns a clone with a context cancel function
type CDPClient ¶
type CDPClient interface { Connect(ctx context.Context) error 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) BackgroundImage ¶
BackgroundImage returns the css background-image of the element
func (*Element) Call ¶
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 ¶
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) error
Click will press then release the button just like a human.
func (*Element) ContainsElement ¶
ContainsElement check if the target is equal or inside 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 ¶
ElementR returns the first element in the page that matches the CSS selector and its text matches the js regex.
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 js on the page. For more info check the Element.Evaluate
func (*Element) Evaluate ¶
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) GetContext ¶
GetContext of current instance
func (*Element) GetSessionID ¶
func (el *Element) GetSessionID() proto.TargetSessionID
GetSessionID interface
func (*Element) HasR ¶
HasR an element that matches the css selector and its text matches the js regex.
func (*Element) Input ¶
Input focus the element and input text to it. To empty the input you can use something like el.SelectAllText().MustInput("")
func (*Element) Interactable ¶
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) MustAttribute ¶
MustAttribute is similar to Attribute
func (*Element) MustBackgroundImage ¶
MustBackgroundImage is similar to BackgroundImage
func (*Element) MustCanvasToImage ¶
MustCanvasToImage is similar to CanvasToImage
func (*Element) MustContainsElement ¶
MustContainsElement is similar to ContainsElement
func (*Element) MustDescribe ¶
MustDescribe is similar to Describe
func (*Element) MustElement ¶
MustElement is similar to Element
func (*Element) MustElementByJS ¶
MustElementByJS is similar to ElementByJS
func (*Element) MustElementR ¶
MustElementR is similar to ElementR
func (*Element) MustElementX ¶
MustElementX is similar to ElementX
func (*Element) MustElements ¶
MustElements is similar to Elements
func (*Element) MustElementsByJS ¶
MustElementsByJS is similar to ElementsByJS
func (*Element) MustElementsX ¶
MustElementsX is similar to ElementsX
func (*Element) MustInteractable ¶
MustInteractable is similar to Interactable
func (*Element) MustMatches ¶
MustMatches is similar to Matches
func (*Element) MustNodeID ¶
MustNodeID is similar to NodeID
func (*Element) MustParent ¶
MustParent is similar to Parent
func (*Element) MustParents ¶
MustParents is similar to Parents
func (*Element) MustPrevious ¶
MustPrevious is similar to Previous
func (*Element) MustProperty ¶
MustProperty is similar to Property
func (*Element) MustResource ¶
MustResource is similar to Resource
func (*Element) MustScreenshot ¶
MustScreenshot is similar to Screenshot
func (*Element) MustScrollIntoView ¶
MustScrollIntoView is similar to ScrollIntoView
func (*Element) MustSelect ¶
MustSelect is similar to Select
func (*Element) MustSelectAllText ¶
MustSelectAllText is similar to SelectAllText
func (*Element) MustSelectText ¶
MustSelectText is similar to SelectText
func (*Element) MustSetFiles ¶
MustSetFiles is similar to SetFiles
func (*Element) MustShadowRoot ¶
MustShadowRoot is similar to ShadowRoot
func (*Element) MustShape ¶
func (el *Element) MustShape() *proto.DOMGetContentQuadsResult
MustShape is similar to Shape
func (*Element) MustVisible ¶
MustVisible is similar to Visible
func (*Element) MustWaitInvisible ¶
MustWaitInvisible is similar to WaitInvisible
func (*Element) MustWaitLoad ¶
MustWaitLoad is similar to WaitLoad
func (*Element) MustWaitStable ¶
MustWaitStable is similar to WaitStable
func (*Element) MustWaitVisible ¶
MustWaitVisible is similar to WaitVisible
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.
func (*Element) SelectAllText ¶
SelectAllText selects all text
func (*Element) SelectText ¶
SelectText selects the text that matches the regular expression
func (*Element) ShadowRoot ¶
ShadowRoot returns the shadow root of this element
func (*Element) Shape ¶
func (el *Element) Shape() (*proto.DOMGetContentQuadsResult, error)
Shape of the DOM element content. The shape is a group of 4-sides polygons (4-gons). A 4-gon is not necessary a rectangle. 4-gons can be apart from each other. For example, we use 2 4-gons to describe the shape below:
┌────────┐ ┌────────┐ │ ┌───┘ = └────────┘ + ┌────┐ └────┘ └────┘
func (*Element) Timeout ¶
Timeout returns a clone with the specified timeout context.Context chained sub-operations
func (*Element) Wait ¶
func (el *Element) Wait(opts *EvalOptions) error
Wait until the js returns true
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) WaitVisible ¶
WaitVisible until the element is visible
func (*Element) WithCancel ¶
WithCancel returns a clone with a context cancel function
type Elements ¶
type Elements []*Element
Elements provides some helpers to deal with element list
type ErrCovered ¶
type ErrCovered struct {
*Element
}
ErrCovered error.
func (*ErrCovered) Error ¶
func (e *ErrCovered) Error() string
func (*ErrCovered) Unwrap ¶
func (e *ErrCovered) Unwrap() error
type ErrElementNotFound ¶
type ErrElementNotFound struct { }
ErrElementNotFound error
func (*ErrElementNotFound) Error ¶
func (e *ErrElementNotFound) Error() string
type ErrExpectElement ¶
type ErrExpectElement struct {
*proto.RuntimeRemoteObject
}
ErrExpectElement error
func (*ErrExpectElement) Error ¶
func (e *ErrExpectElement) Error() string
type ErrExpectElements ¶
type ErrExpectElements struct {
*proto.RuntimeRemoteObject
}
ErrExpectElements error
func (*ErrExpectElements) Error ¶
func (e *ErrExpectElements) Error() string
type ErrInvisibleShape ¶
type ErrInvisibleShape struct { }
ErrInvisibleShape error.
func (*ErrInvisibleShape) Error ¶
func (e *ErrInvisibleShape) Error() string
func (*ErrInvisibleShape) Unwrap ¶
func (e *ErrInvisibleShape) Unwrap() error
type ErrNavigation ¶
type ErrNavigation struct {
}ErrNavigation error
func (*ErrNavigation) Error ¶
func (e *ErrNavigation) Error() string
type ErrNotInteractable ¶
type ErrNotInteractable struct{}
ErrNotInteractable error. Check the doc of Element.Interactable for details.
func (*ErrNotInteractable) Error ¶
func (e *ErrNotInteractable) Error() string
type ErrObjectNotFound ¶
type ErrObjectNotFound struct {
*proto.RuntimeRemoteObject
}
ErrObjectNotFound error
func (*ErrObjectNotFound) Error ¶
func (e *ErrObjectNotFound) Error() string
type ErrPageCloseCanceled ¶
type ErrPageCloseCanceled struct { }
ErrPageCloseCanceled error
func (*ErrPageCloseCanceled) Error ¶
func (e *ErrPageCloseCanceled) Error() string
type EvalOptions ¶
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 code to eval JS string // JSArgs represents the arguments in the JS if the JS is a function definition. // If an argument is *proto.RuntimeRemoteObject type, the corresponding remote object will be used. // Or it will be passed as a plain JSON value. JSArgs []interface{} // Whether execution should be treated as initiated by user in the UI. UserGesture bool JsHelper *js.Function }
EvalOptions for Page.Evaluate
func Eval ¶
func Eval(js string, args ...interface{}) *EvalOptions
Eval creates a EvalOptions with ByValue set to true.
func JsHelper ¶
func JsHelper(fn *js.Function, args ...interface{}) *EvalOptions
Convert name and jsArgs to Page.Eval, the name is method name in the "lib/js/helper.js".
func (*EvalOptions) ByObject ¶
func (e *EvalOptions) ByObject() *EvalOptions
ByObject disables ByValue.
func (*EvalOptions) ByPromise ¶
func (e *EvalOptions) ByPromise() *EvalOptions
ByPromise enables AwaitPromise.
func (*EvalOptions) ByUser ¶
func (e *EvalOptions) ByUser() *EvalOptions
ByUser enables UserGesture.
func (*EvalOptions) Strings ¶
func (e *EvalOptions) Strings(list ...string) *EvalOptions
Strings appends each string to JSArgs
func (*EvalOptions) This ¶
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 // contains filtered or unexported fields }
Hijack context
func (*Hijack) ContinueRequest ¶
func (h *Hijack) ContinueRequest(cq *proto.FetchContinueRequest)
ContinueRequest without hijacking
func (*Hijack) LoadResponse ¶
LoadResponse will send request to the real destination and load the response as default response to override.
func (*Hijack) MustLoadResponse ¶
func (h *Hijack) MustLoadResponse()
MustLoadResponse is similar to 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) JSONBody ¶
func (ctx *HijackRequest) JSONBody() gson.JSON
JSONBody of the request
func (*HijackRequest) Req ¶
func (ctx *HijackRequest) Req() *http.Request
Req returns the underlaying 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 ¶
func (ctx *HijackRequest) SetContext(c context.Context) *HijackRequest
SetContext of the underlaying http.Request instance
func (*HijackRequest) Type ¶
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 ¶
func (ctx *HijackResponse) Fail(reason proto.NetworkErrorReason) *HijackResponse
Fail request
func (*HijackResponse) Headers ¶
func (ctx *HijackResponse) Headers() http.Header
Headers of the payload
func (*HijackResponse) Payload ¶
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 ¶
func (r *HijackRouter) MustAdd(pattern string, handler func(*Hijack)) *HijackRouter
MustAdd is similar to Add
func (*HijackRouter) MustRemove ¶
func (r *HijackRouter) MustRemove(pattern string) *HijackRouter
MustRemove is similar to Remove
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 Keyboard ¶
Keyboard represents the keyboard on a page, it's always related the main frame
func (*Keyboard) InsertText ¶
InsertText is like pasting text into the page
func (*Keyboard) MustInsertText ¶
MustInsertText is similar to InsertText
type Message ¶
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) error
Click the button. It's the combination of Mouse.Down and Mouse.Up
func (*Mouse) Down ¶
func (m *Mouse) Down(button proto.InputMouseButton, clicks int) error
Down holds the button down
func (*Mouse) MustClick ¶
func (m *Mouse) MustClick(button proto.InputMouseButton) *Mouse
MustClick is similar to Click
func (*Mouse) MustDown ¶
func (m *Mouse) MustDown(button proto.InputMouseButton) *Mouse
MustDown is similar to Down
func (*Mouse) MustScroll ¶
MustScroll is similar to Scroll
func (*Mouse) MustUp ¶
func (m *Mouse) MustUp(button proto.InputMouseButton) *Mouse
MustUp is similar to Up
type Page ¶
type Page struct { TargetID proto.TargetTargetID SessionID proto.TargetSessionID FrameID proto.PageFrameID // 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
Example (Pool) ¶
It's a common practice to concurrently use a pool of resources in Go, it's not special for rod.
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 pool := rod.NewPagePool(3) // Create a page if needed. If you want pages to share cookies with each remove the MustIncognito() create := func() *rod.Page { return browser.MustIncognito().MustPage("") } yourJob := func() { page := pool.Get(create) defer pool.Put(page) page.MustNavigate("http://example.com").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: Example Domain Example Domain Example Domain Example Domain
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 ¶
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) 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 is similar to Browser.EachEvent, but only catches events for current page.
func (*Page) Element ¶
Element retries until an element in the page that matches one of the CSS selectors, 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 ¶
ElementFromNode creates an Element from the node id
func (*Page) ElementFromObject ¶
func (p *Page) ElementFromObject(obj *proto.RuntimeRemoteObject) *Element
ElementFromObject creates an Element from the remote object id.
func (*Page) ElementFromPoint ¶
ElementFromPoint creates an Element from the absolute point on the page. The point should include the window scroll offset.
func (*Page) ElementR ¶
ElementR retries until an element in the page that matches one of the pairs, then returns the matched element. Each pairs is a css selector and a regex. A sample call will look like page.MustElementR("div", "click me"). The regex is the js regex, not golang's.
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 ¶
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, jsArgs ...interface{}) (*proto.RuntimeRemoteObject, error)
Eval is just a shortcut for Page.Evaluate
func (*Page) EvalOnNewDocument ¶
EvalOnNewDocument Evaluates given script in every frame upon creation (before loading frame's scripts).
func (*Page) Evaluate ¶
func (p *Page) Evaluate(opts *EvalOptions) (res *proto.RuntimeRemoteObject, err error)
Evaluate js on the page.
func (*Page) Expose ¶
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. Must expose before navigation. The exposure survives reloads.
func (*Page) GetContext ¶
GetContext of current instance
func (*Page) GetDownloadFile ¶
func (p *Page) GetDownloadFile(pattern string, resourceType proto.NetworkResourceType, client *http.Client) func() (http.Header, []byte, error)
GetDownloadFile of the next download url that matches the pattern, returns the file content. The handler will be used once and removed.
func (*Page) GetResource ¶
GetResource content by the url. Such as image, css, html, etc. Use the proto.PageGetResourceTree to list all the resources.
func (*Page) GetSessionID ¶
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 ¶
HandleDialog accepts or dismisses next JavaScript initiated dialog (alert, confirm, prompt, or onbeforeunload). Because alert will block js, usually you have to run the wait function in another goroutine.
func (*Page) HasR ¶
HasR an element that matches the css selector and its display text matches the js regex.
func (*Page) HijackRequests ¶
func (p *Page) HijackRequests() *HijackRouter
HijackRequests same as Browser.HijackRequests, but scoped with the page
func (*Page) Info ¶
func (p *Page) Info() (*proto.TargetTargetInfo, error)
Info of the page, such as the URL or title of the page
func (*Page) MustAddScriptTag ¶
MustAddScriptTag is similar to AddScriptTag
func (*Page) MustAddStyleTag ¶
MustAddStyleTag is similar to AddStyleTag
func (*Page) MustCookies ¶
func (p *Page) MustCookies(urls ...string) []*proto.NetworkCookie
MustCookies is similar to Cookies
func (*Page) MustElement ¶
MustElement is similar to Element
func (*Page) MustElementByJS ¶
MustElementByJS is similar to ElementByJS
func (*Page) MustElementFromNode ¶
MustElementFromNode is similar to ElementFromNode
func (*Page) MustElementFromPoint ¶
MustElementFromPoint is similar to ElementFromPoint
func (*Page) MustElementR ¶
MustElementR is similar to ElementR
func (*Page) MustElementX ¶
MustElementX is similar to ElementX
func (*Page) MustElements ¶
MustElements is similar to Elements
func (*Page) MustElementsByJS ¶
MustElementsByJS is similar to ElementsByJS
func (*Page) MustElementsX ¶
MustElementsX is similar to ElementsX
func (*Page) MustEmulate ¶
MustEmulate is similar to Emulate
func (*Page) MustEvalOnNewDocument ¶
MustEvalOnNewDocument is similar to EvalOnNewDocument
func (*Page) MustEvaluate ¶
func (p *Page) MustEvaluate(opts *EvalOptions) *proto.RuntimeRemoteObject
MustEvaluate is similar to Evaluate
func (*Page) MustExpose ¶
MustExpose is similar to Expose
func (*Page) MustGetDownloadFile ¶
MustGetDownloadFile is similar to GetDownloadFile
func (*Page) MustGetWindow ¶
func (p *Page) MustGetWindow() *proto.BrowserBounds
MustGetWindow is similar to GetWindow
func (*Page) MustHandleDialog ¶
MustHandleDialog is similar to HandleDialog
func (*Page) MustInfo ¶
func (p *Page) MustInfo() *proto.TargetTargetInfo
MustInfo is similar to Info
func (*Page) MustNavigate ¶
MustNavigate is similar to Navigate
func (*Page) MustNavigateBack ¶
MustNavigateBack is similar to NavigateBack
func (*Page) MustNavigateForward ¶
MustNavigateForward is similar to NavigateForward
func (*Page) MustObjectToJSON ¶
func (p *Page) MustObjectToJSON(obj *proto.RuntimeRemoteObject) gson.JSON
MustObjectToJSON is similar to ObjectToJSON
func (*Page) MustObjectsToJSON ¶
func (p *Page) MustObjectsToJSON(list []*proto.RuntimeRemoteObject) gson.JSON
MustObjectsToJSON is similar to ObjectsToJSON
func (*Page) MustPDF ¶
MustPDF is similar to PDF. If the toFile is "", it will save output to "tmp/pdf" folder, time as the file name.
func (*Page) MustRelease ¶
func (p *Page) MustRelease(obj *proto.RuntimeRemoteObject) *Page
MustRelease is similar to Release
func (*Page) MustScreenshot ¶
MustScreenshot is similar to Screenshot. If the toFile is "", it will save output to "tmp/screenshots" folder, time as the file name.
func (*Page) MustScreenshotFullPage ¶
MustScreenshotFullPage is similar to ScreenshotFullPage. If the toFile is "", it will save output to "tmp/screenshots" folder, time as the file name.
func (*Page) MustSearch ¶
MustSearch is similar to Search
func (*Page) MustSetCookies ¶
func (p *Page) MustSetCookies(cookies ...*proto.NetworkCookieParam) *Page
MustSetCookies is similar to SetCookies
func (*Page) MustSetExtraHeaders ¶
MustSetExtraHeaders is similar to SetExtraHeaders
func (*Page) MustSetUserAgent ¶
func (p *Page) MustSetUserAgent(req *proto.NetworkSetUserAgentOverride) *Page
MustSetUserAgent is similar to SetUserAgent
func (*Page) MustSetViewport ¶
MustSetViewport is similar to SetViewport
func (*Page) MustSetWindow ¶
MustSetWindow is similar to SetWindow
func (*Page) MustStopLoading ¶
MustStopLoading is similar to StopLoading
func (*Page) MustWaitNavigation ¶
func (p *Page) MustWaitNavigation() func()
MustWaitNavigation is similar to WaitNavigation
func (*Page) MustWaitOpen ¶
MustWaitOpen is similar to WaitOpen
func (*Page) MustWaitPauseOpen ¶
MustWaitPauseOpen is similar to WaitPauseOpen
func (*Page) MustWaitRequestIdle ¶
MustWaitRequestIdle is similar to WaitRequestIdle
func (*Page) MustWindowFullscreen ¶
MustWindowFullscreen is similar to WindowFullscreen
func (*Page) MustWindowMaximize ¶
MustWindowMaximize is similar to WindowMaximize
func (*Page) MustWindowMinimize ¶
MustWindowMinimize is similar to WindowMinimize
func (*Page) MustWindowNormal ¶
MustWindowNormal is similar to 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) ObjectToJSON ¶
ObjectToJSON by object id
func (*Page) PDF ¶
func (p *Page) PDF(req *proto.PagePrintToPDF) (*StreamReader, error)
PDF prints page as PDF
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 options: https://chromedevtools.github.io/devtools-protocol/tot/Page#method-captureScreenshot
func (*Page) Search ¶
Search for each 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) SetCookies ¶
func (p *Page) SetCookies(cookies []*proto.NetworkCookieParam) error
SetCookies of the page.
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 ¶
func (p *Page) SetViewport(params *proto.EmulationSetDeviceMetricsOverride) error
SetViewport overrides the values of device screen dimensions
func (*Page) SetWindow ¶
func (p *Page) SetWindow(bounds *proto.BrowserBounds) error
SetWindow location and size
func (*Page) StopLoading ¶
StopLoading forces the page stop navigation and pending resource fetches.
func (*Page) Timeout ¶
Timeout returns a clone with the specified timeout context.Context chained sub-operations
func (*Page) Wait ¶
func (p *Page) Wait(this *proto.RuntimeRemoteObject, js string, params []interface{}) error
Wait js function until it returns true
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 ¶
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) WaitPauseOpen ¶
WaitPauseOpen waits for a page opened by the current page, before opening pause the js execution. Because the js will be paused, you should put the code that triggers it in a goroutine.
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) WithCancel ¶
WithCancel returns a clone with a context cancel function
type PagePool ¶
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.
type Pages ¶
type Pages []*Page
Pages provides some helpers to deal with page list
func (Pages) MustFindByURL ¶
MustFindByURL is similar to FindByURL
type RaceContext ¶
type RaceContext struct {
// contains filtered or unexported fields
}
RaceContext stores the branches to race
func (*RaceContext) Element ¶
func (rc *RaceContext) Element(selector string, callback func(*Element) error) *RaceContext
Element the doc is similar to MustElement but has a callback when a match is found
func (*RaceContext) ElementByJS ¶
func (rc *RaceContext) ElementByJS(opts *EvalOptions, callback func(*Element) error) *RaceContext
ElementByJS the doc is similar to MustElementByJS but has a callback when a match is found
func (*RaceContext) ElementR ¶
func (rc *RaceContext) ElementR(selector, regex string, callback func(*Element) error) *RaceContext
ElementR the doc is similar to ElementR but has a callback when a match is found
func (*RaceContext) ElementX ¶
func (rc *RaceContext) ElementX(selector string, callback func(*Element) error) *RaceContext
ElementX the doc is similar to ElementX but has a callback when a match is found
func (*RaceContext) MustElement ¶
func (rc *RaceContext) MustElement(selector string, callback func(*Element)) *RaceContext
MustElement is similar to Element
func (*RaceContext) MustElementByJS ¶
func (rc *RaceContext) MustElementByJS(js string, params []interface{}, callback func(*Element) error) *RaceContext
MustElementByJS is similar to ElementByJS
func (*RaceContext) MustElementR ¶
func (rc *RaceContext) MustElementR(selector, regex string, callback func(*Element)) *RaceContext
MustElementR is similar to ElementR
func (*RaceContext) MustElementX ¶
func (rc *RaceContext) MustElementX(selector string, callback func(*Element)) *RaceContext
MustElementX is similar to ElementX
type SelectorType ¶
type SelectorType string
SelectorType enum
const ( // SelectorTypeRegex type SelectorTypeRegex SelectorType = "regex" // SelectorTypeCSSSector type SelectorTypeCSSSector SelectorType = "css-selector" // SelectorTypeText type SelectorTypeText SelectorType = "text" )
type StreamReader ¶
type StreamReader struct { Offset int // contains filtered or unexported fields }
StreamReader for browser data stream
func NewStreamReader ¶
func NewStreamReader(c proto.Client, h proto.IOStreamHandle) *StreamReader
NewStreamReader instance
type Touch ¶
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 ¶
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) MustMove ¶
func (t *Touch) MustMove(points ...*proto.InputTouchPoint) *Touch
MustMove is similar to Move
func (*Touch) MustStart ¶
func (t *Touch) MustStart(points ...*proto.InputTouchPoint) *Touch
MustStart is similar to Start
type TraceMsg ¶
type TraceMsg struct { // Type of the message Type TraceType // Details is a json object Details interface{} }
TraceMsg for logger
type TraceType ¶
type TraceType string
TraceType for logger
const ( // TraceTypeWaitRequestsIdle type TraceTypeWaitRequestsIdle TraceType = "wait requests idle" // TraceTypeWaitRequests type TraceTypeWaitRequests TraceType = "wait requests" // TraceTypeEval type TraceTypeEval TraceType = "eval" // TraceTypeAction type TraceTypeAction TraceType = "act" // TraceTypeInput type TraceTypeInput TraceType = "input" )
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
fixtures
|
|
lib
|
|
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. |
launcher
Package launcher for launching browser utils.
|
Package launcher for launching browser utils. |