### Create and Navigate Page Source: https://github.com/go-rod/go-rod.github.io/blob/main/context-and-timeout.md Basic example of creating a new browser instance, connecting, and navigating to a page. ```go page := rod.New().MustConnect().MustPage() page.MustNavigate("http://github.com") ``` -------------------------------- ### Serve Go-rod Documentation Locally Source: https://github.com/go-rod/go-rod.github.io/blob/main/contribute-doc.md Install docsify-cli and then run these commands to clone the repository and serve the documentation locally. The page will auto-refresh on edits. ```bash git clone https://github.com/go-rod/go-rod.github.io.git docsify serve -o go-rod.github.io ``` -------------------------------- ### Launch Multiple Browsers Source: https://github.com/go-rod/go-rod.github.io/blob/main/browsers-pages.md Demonstrates how to launch two independent browser instances. Ensure Rod and its dependencies are installed. ```go browser1 := rod.New().MustConnect() browser2 := rod.New().MustConnect() fmt.Println(browser1, browser2) ``` -------------------------------- ### Install Chrome on Ubuntu/Debian Source: https://github.com/go-rod/go-rod.github.io/blob/main/compatibility.md Use wget and apt to install the Google Chrome stable version on Ubuntu or Debian systems. ```bash wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb apt install ./google-chrome-stable_current_amd64.deb ``` -------------------------------- ### Launch Browser with Proxy and Handle Auth Source: https://github.com/go-rod/go-rod.github.io/blob/main/network/README.md A complete example demonstrating how to launch a browser with a proxy server, handle authentication pop-ups, ignore certificate errors, and navigate to a page. ```go // Create a browser launcher l := launcher.New() // Pass '--proxy-server=127.0.0.1:8081' argument to the browser on launch l = l.Set(flags.ProxyServer, "127.0.0.1:8080") // Launch the browser and get debug URL controlURL, _ := l.Launch() // Connect to the newly launched browser browser := rod.New().ControlURL(controlURL).MustConnect() // Handle proxy authentication pop-up go browser.MustHandleAuth("user", "password")() // <-- Notice how HandleAuth returns // a function that must be // started as a goroutine! // Ignore certificate errors since we are using local insecure proxy browser.MustIgnoreCertErrors(true) // Navigate to the page that prints IP address page := browser.MustPage("http://api.ipify.org") // IP address should be the same, since we are using local // proxy, however the response signals that the proxy works println(page.MustElement("html").MustText()) ``` -------------------------------- ### Install Chromium on Alpine Source: https://github.com/go-rod/go-rod.github.io/blob/main/compatibility.md Use apk to install the Chromium browser on Alpine Linux systems. ```bash apk add chromium ``` -------------------------------- ### Install Chrome on CentOS Source: https://github.com/go-rod/go-rod.github.io/blob/main/compatibility.md Use wget and yum to install the Google Chrome stable version on CentOS systems. ```bash wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm yum localinstall -y google-chrome-stable_current_x86_64.rpm ``` -------------------------------- ### Get Resource by URL Source: https://github.com/go-rod/go-rod.github.io/blob/main/page-resources/README.md Use Page.GetResource to fetch a file from a given URL. The method returns the binary content of the resource. ```go bin, _ := page.GetResource("https://test.com/a.png") fmt.Println(bin) ``` -------------------------------- ### Launch Browser in User Mode Source: https://github.com/go-rod/go-rod.github.io/blob/main/custom-launch.md Use `launcher.NewUserMode().MustLaunch()` to get the WebSocket URL for an existing browser session. Then, connect Rod to this URL using `rod.New().ControlURL(wsURL).MustConnect().NoDefaultDevice()`. ```go wsURL := launcher.NewUserMode().MustLaunch() rod.New().ControlURL(wsURL).MustConnect().NoDefaultDevice() ``` -------------------------------- ### Avoid Sleep with Race Selectors Source: https://github.com/go-rod/go-rod.github.io/blob/main/selectors/README.md This example demonstrates the anti-pattern of using `time.Sleep` for waiting. It's recommended to use race selectors instead for more robust automation. ```go func main() { page := rod.New().MustConnect().MustPage("https://leetcode.com/accounts/login/") page.MustElement("#id_login").MustInput("username") page.MustElement("#id_password").MustInput("password").MustType(input.Enter) time.Sleep(10 * time.Second) // Please avoid the use of time.Sleep! if page.MustHas(".nav-user-icon-base") { // print the username after successful login fmt.Println(page.MustElement(".nav-user-icon-base").MustAttribute("title")) } else if page.MustHas("[data-cy=sign-in-error]") { // when wrong username or password fmt.Println(page.MustElement("[data-cy=sign-in-error]").MustText()) } } ``` -------------------------------- ### Emulate predefined device Source: https://github.com/go-rod/go-rod.github.io/blob/main/emulation.md Emulates a predefined device profile for a page, setting viewport, user-agent, and orientation simultaneously. Use this for quick setup with common device types. ```go page.MustEmulate(devices.IPhone6or7or8Plus) ``` -------------------------------- ### Launch Browser via Docker Source: https://github.com/go-rod/go-rod.github.io/blob/main/custom-launch.md Use the provided Docker image for a consistent Chromium environment across different platforms. This is particularly helpful on Linux distributions where manual installation can be complex. ```bash docker run -p 7317:7317 ghcr.io/go-rod/rod ``` -------------------------------- ### Get Image Content from Page Source: https://github.com/go-rod/go-rod.github.io/blob/main/get-started/README.md Use the `MustResource` method to get the binary data of an image from the page. The result is saved to a file. ```go package main import ( "github.com/go-rod/rod" "github.com/go-rod/rod/lib/utils" ) func main() { page := rod.New().MustConnect().MustPage("https://www.wikipedia.org/") page.MustElement("#searchInput").MustInput("earth") page.MustElement("#search-form > fieldset > button").MustClick() el := page.MustElement("#mw-content-text > div.mw-parser-output > table.infobox > tbody > tr:nth-child(1) > td > a > img") _ = utils.OutputFile("b.png", el.MustResource()) } ``` -------------------------------- ### Use Race Selectors for Concurrent Waiting Source: https://github.com/go-rod/go-rod.github.io/blob/main/selectors/README.md This example shows the recommended approach using `Race().Element().MustHandle()` to concurrently wait for either a successful login indicator or an error message, eliminating the need for `time.Sleep`. ```go func main() { page := rod.New().MustConnect().MustPage("https://leetcode.com/accounts/login/") page.MustElement("#id_login").MustInput("username") page.MustElement("#id_password").MustInput("password").MustType(input.Enter) // It will keep polling until one selector has found a match 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]").MustHandle(func(e *rod.Element) { // when wrong username or password panic(e.MustText()) }).MustDo() } ``` -------------------------------- ### Get Returned Value from Javascript Eval Source: https://github.com/go-rod/go-rod.github.io/blob/main/javascript-runtime.md Retrieve and process values returned from JavaScript code evaluated on the page. This example retrieves a string property from an object set via Eval. ```go val := page.MustEval(`() => a`).Get("name").Str() fmt.Println(val) // output: jack ``` -------------------------------- ### Get Event Details (Response Status) Source: https://github.com/go-rod/go-rod.github.io/blob/main/events/README.md Retrieves details of a navigation event, specifically the HTTP status code of the response. The event subscription is done before navigation. ```go func main() { page := rod.New().MustConnect().MustPage() e := proto.NetworkResponseReceived{} wait := page.WaitEvent(&e) page.MustNavigate("https://www.wikipedia.org/") wait() fmt.Println(e.Response.Status) } ``` -------------------------------- ### Manually Set Slow Motion Source: https://github.com/go-rod/go-rod.github.io/blob/main/get-started/README.md Manually set the slow motion duration using code, for example, `rod.New().SlowMotion(2 * time.Second)`. ```go rod.New().SlowMotion(2 * time.Second) ``` -------------------------------- ### Get Element Resource Source: https://github.com/go-rod/go-rod.github.io/blob/main/page-resources/README.md Use Element.Resource to retrieve the file content from the 'src' attribute of an element. This is useful for images or other media linked via src. ```go bin := page.MustElement("img").MustResource() fmt.Println(bin) ``` -------------------------------- ### Get Elements from Nested Iframes Source: https://github.com/go-rod/go-rod.github.io/blob/main/selectors/README.md Navigates through nested iframes to select an element within the deepest frame. Assumes the structure shown in the accompanying diagram. ```go frame01 := page.MustElement("iframe").MustFrame() frame02 := frame01.MustElement("iframe").MustFrame() frame02.MustElement("button") ``` -------------------------------- ### Get Text Content from Page Source: https://github.com/go-rod/go-rod.github.io/blob/main/get-started/README.md Use the `MustText` method to retrieve the text content of a specific element on the page. Ensure the correct selector is used. ```go package main import ( "fmt" "github.com/go-rod/rod" ) func main() { page := rod.New().MustConnect().MustPage("https://www.wikipedia.org/") page.MustElement("#searchInput").MustInput("earth") page.MustElement("#search-form > fieldset > button").MustClick() el := page.MustElement("#mw-content-text > div.mw-parser-output > p:nth-child(7)") fmt.Println(el.MustText()) } ``` -------------------------------- ### Launch Browsers with Custom Arguments Source: https://github.com/go-rod/go-rod.github.io/blob/main/browsers-pages.md Illustrates launching browsers with specific configurations, such as disabling headless mode or setting a user data directory. Ensure the paths are valid. ```go browser1 := rod.New().ControlURL( launcher.New().Headless(false).MustLaunch(), ).MustConnect() browser2 := rod.New().ControlURL( launcher.New().UserDataDir("path").MustLaunch(), ).MustConnect() fmt.Println(browser1, browser2) ``` -------------------------------- ### Launch Browser with Launcher Lib Source: https://github.com/go-rod/go-rod.github.io/blob/main/custom-launch.md Use the launcher lib to simplify browser launching. It can automatically find or download the browser executable and manage command-line arguments. ```go func main() { u := launcher.New().Bin("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome").MustLaunch() rod.New().ControlURL(u).MustConnect().MustPage("https://example.com") } ``` ```go func main() { path, _ := launcher.LookPath() u := launcher.New().Bin(path).MustLaunch() rod.New().ControlURL(u).MustConnect().MustPage("https://example.com") } ``` ```go func main() { rod.New().MustConnect().MustPage("https://example.com") } ``` -------------------------------- ### Launch Multiple Pages for a Browser Source: https://github.com/go-rod/go-rod.github.io/blob/main/browsers-pages.md Demonstrates how to open multiple web pages within a single browser instance. Each page can be navigated to a different URL. ```go browser := rod.New().MustConnect() page1 := browser.MustPage("http://a.com") page2 := browser.MustPage("http://b.com") fmt.Println(page1, page2) ``` -------------------------------- ### Run mitmproxy with Authentication Source: https://github.com/go-rod/go-rod.github.io/blob/main/network/README.md This command launches a mitmproxy instance with a specified port and authentication credentials, useful for testing proxy configurations. ```bash docker run --rm -it -p 8080:8080 mitmproxy/mitmproxy mitmdump -p 8080 --proxyauth user:password ``` -------------------------------- ### Show Browser UI with DevTools Source: https://github.com/go-rod/go-rod.github.io/blob/main/get-started/README.md Runs the Go program and displays the browser UI, enabling DevTools for inspecting elements. Use 'CTRL + C' to stop. ```bash go run . -rod=show,devtools ``` -------------------------------- ### Launch Incognito Browser Source: https://github.com/go-rod/go-rod.github.io/blob/main/browsers-pages.md Shows how to create an incognito browser instance from an existing browser. This is useful for isolated browsing sessions. ```go browser1 := rod.New().MustConnect() browser2 := browser1.MustIncognito() fmt.Println(browser1, browser2) ``` -------------------------------- ### Initialize Go Module Source: https://github.com/go-rod/go-rod.github.io/blob/main/get-started/README.md Sets the Go proxy and initializes a new Go module for a project. Used for managing dependencies. ```bash go env -w GOPROXY=https://goproxy.io,direct go mod init learn-rod go mod tidy ``` -------------------------------- ### Open Page and Take Screenshot Source: https://github.com/go-rod/go-rod.github.io/blob/main/get-started/README.md Launches a browser, opens a Wikipedia page, waits for it to stabilize, and takes a screenshot. Requires Golang and Rod library. ```go package main import "github.com/go-rod/rod" func main() { page := rod.New().MustConnect().MustPage("https://www.wikipedia.org/") page.MustWaitStable().MustScreenshot("a.png") } ``` -------------------------------- ### Connect to a Running Browser Source: https://github.com/go-rod/go-rod.github.io/blob/main/custom-launch.md Connect to a browser instance that is already running with remote debugging enabled. Ensure the browser is launched with the `--remote-debugging-port` flag. ```go package main import ( "github.com/go-rod/rod" ) func main() { u := "ws://127.0.0.1:9222/devtools/browser/4dcf09f2-ba2b-463a-8ff5-90d27c6cc913" rod.New().ControlURL(u).MustConnect().MustPage("https://example.com") } ``` -------------------------------- ### Run Go Program Source: https://github.com/go-rod/go-rod.github.io/blob/main/get-started/README.md Executes the Go program in the current directory. Assumes a main package and a main function. ```bash go run . ``` -------------------------------- ### Simulate Key Combinations and Shortcuts Source: https://github.com/go-rod/go-rod.github.io/blob/main/input.md Utilize KeyActions() with Press() and Type() to simulate key presses, including combinations like Shift+A or shortcuts like Ctrl+Enter. The helper automatically releases pressed keys. ```go page.KeyActions().Press(input.ShiftLeft).Type('A').MustDo() ``` ```go page.KeyActions().Press(input.ControlLeft).Type(input.Enter).MustDo() ``` -------------------------------- ### Enable Slow Motion and Visual Trace Source: https://github.com/go-rod/go-rod.github.io/blob/main/get-started/README.md Run the module with extra options to enable slow motion and visual trace for debugging. Each action will wait for 1 second before execution. ```bash go run . -rod="show,slow=1s,trace" ``` -------------------------------- ### Emulate Network Conditions Source: https://github.com/go-rod/go-rod.github.io/blob/main/network/README.md Enable network domain and then emulate specific network conditions such as latency, throughput, and connection type to test slow network effects. ```go page.EnableDomain(proto.NetworkEnable{}) _ = proto.NetworkEmulateNetworkConditions{ Offline: false, Latency: 300, DownloadThroughput: 100, UploadThroughput: 50, ConnectionType: proto.NetworkConnectionTypeCellular2g, }.Call(page) ``` -------------------------------- ### MustElement Implementation in Rod Source: https://github.com/go-rod/go-rod.github.io/blob/main/error-handling.md Shows the internal implementation of a 'Must' prefixed method, demonstrating how it wraps a non-prefixed method and panics on error. ```go package main import ( "fmt" "github.com/go-rod/rod" ) // Page ... type Page rod.Page // MustElement ... func (p *Page) MustElement(selector string) *rod.Element { el, err := (*rod.Page)(p).Element(selector) if err != nil { panic(err) } return el } ``` -------------------------------- ### Traverse Element Tree and Select Children Source: https://github.com/go-rod/go-rod.github.io/blob/main/selectors/README.md Demonstrates selecting a section, then its next sibling, and then all list items within that section. Retrieves project links and their associated text. ```go // On awesome-go page, finding the specified section sect, // and retrieving the associated projects from the page. func main() { page := rod.New().MustConnect().MustPage("https://github.com/avelino/awesome-go") section := page.MustElementR("p", "Selenium and browser control tools").MustNext() // get children elements of an element projects := section.MustElements("li") for _, project := range projects { link := project.MustElement("a") log.Printf( "project %s (%s): '%s'", link.MustText(), link.MustProperty("href"), project.MustText(), ) } } ``` -------------------------------- ### Standard Go Error Handling with Rod Source: https://github.com/go-rod/go-rod.github.io/blob/main/error-handling.md Demonstrates the conventional Go approach to error handling by checking errors returned from Rod's non-prefixed methods like Element and HTML. ```go page := rod.New().MustConnect().MustPage("https://example.com") el, err := page.Element("a") if err != nil { panic(err) } html, err := el.HTML() if err != nil { panic(err) } fmt.Println(html) ``` -------------------------------- ### Enable Debugging and Fullscreen Source: https://github.com/go-rod/go-rod.github.io/blob/main/get-started/README.md Configures Rod to disable default devices, open the browser in fullscreen, and keep it open for debugging. Includes a long sleep to prevent immediate closure. ```go package main import ( t"time" "github.com/go-rod/rod" ) func main() { page := rod.New().NoDefaultDevice().MustConnect().MustPage("https://www.wikipedia.org/") page.MustWindowFullscreen() page.MustWaitStable().MustScreenshot("a.png") time.Sleep(time.Hour) } ``` -------------------------------- ### Set emulated media and color scheme Source: https://github.com/go-rod/go-rod.github.io/blob/main/emulation.md Sets the emulated media type (e.g., 'screen') and features, such as 'prefers-color-scheme' (e.g., 'dark'), for a specific page. This is useful for testing responsive designs and dark mode implementations. ```go page := browser.MustPage() _ = proto.EmulationSetEmulatedMedia{ Media: "screen", Features: []*proto.EmulationMediaFeature{ {Name: "prefers-color-scheme", Value: "dark"}, }, }.Call(page) ``` -------------------------------- ### Modify Browser Launch Arguments Source: https://github.com/go-rod/go-rod.github.io/blob/main/custom-launch.md Customize browser launch arguments using Set and Delete methods. Helper functions like UserDataDir and Headless simplify common configurations. ```go package main import ( "github.com/go-rod/rod" "github.com/go-rod/rod/lib/launcher" ) func main() { u := launcher.New(). Set("user-data-dir", "path"). Set("headless"). Delete("--headless"). MustLaunch() rod.New().ControlURL(u).MustConnect().MustPage("https://example.com") } ``` ```go func main() { u := launcher.New(). UserDataDir("path"). Headless(true). Headless(false). MustLaunch() rod.New().ControlURL(u).MustConnect().MustPage("https://example.com") } ``` -------------------------------- ### View Console Trace Log Source: https://github.com/go-rod/go-rod.github.io/blob/main/get-started/README.md Observe the trace log in the console to understand the automated operations performed by Rod. ```txt [rod] 2020/11/11 11:11:11 [eval] {"js":"rod.element","params":["#searchInput"]} [rod] 2020/11/11 11:11:11 [eval] {"js":"rod.visible","this":"input#searchInput"} [rod] 2020/11/11 11:11:11 [input] scroll into view [rod] 2020/11/11 11:11:11 [input] input earth [rod] 2020/11/11 11:11:11 [eval] {"js":"rod.element","params":["#search-form > fieldset > button"]} [rod] 2020/11/11 11:11:11 [eval] {"js":"rod.visible","this":"button.pure-button.pure-button-primary-progressive"} [rod] 2020/11/11 11:11:11 [input] scroll into view [rod] 2020/11/11 11:11:11 [input] left click ``` -------------------------------- ### Handle Proxy Authentication Pop-up Source: https://github.com/go-rod/go-rod.github.io/blob/main/network/README.md Use this snippet to handle proxy authentication pop-ups asynchronously. It returns a function that must be called as a goroutine. ```go go browser.MustHandleAuth("user", "password")() ``` -------------------------------- ### Select Element by XPath Source: https://github.com/go-rod/go-rod.github.io/blob/main/selectors/README.md Use ElementX for XPath queries when CSS selectors are not suitable or for familiarity with XPath. ```go page.MustElementX("//h2") ``` -------------------------------- ### Set Files for File Input Source: https://github.com/go-rod/go-rod.github.io/blob/main/input.md Use MustSetFiles() to upload one or more files to an HTML file input element. ```go page.MustElement(`[type=file]`).MustSetFiles("a.jpg", "b.pdf") ``` -------------------------------- ### Define custom device for emulation Source: https://github.com/go-rod/go-rod.github.io/blob/main/emulation.md Defines and emulates a custom device profile for a page, allowing fine-grained control over title, capabilities, user agent, accept language, and screen properties. Useful for simulating unique or specific device configurations. ```go page.MustEmulate(devices.Device{ Title: "iPhone 4", Capabilities: []string{"touch", "mobile"}, UserAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X)", AcceptLanguage: "en", Screen: devices.Screen{ DevicePixelRatio: 2, Horizontal: devices.ScreenSize{ Width: 480, Height: 320, }, Vertical: devices.ScreenSize{ Width: 320, Height: 480, }, }, }) ``` -------------------------------- ### Simulate Text Input Source: https://github.com/go-rod/go-rod.github.io/blob/main/input.md Use MustInput() to type text into an input field and MustText() to retrieve the current text. ```go el := page.MustElement(`[type="text"]`) el.MustInput("Jack") fmt.Println(el.MustText()) // use MustText to get the text ``` -------------------------------- ### Handle All Console API Events Source: https://github.com/go-rod/go-rod.github.io/blob/main/events/README.md Listens for all console API call events on a page and prints the arguments. This uses a goroutine to handle events concurrently. ```go go page.EachEvent(func(e *proto.RuntimeConsoleAPICalled) { fmt.Println(page.MustObjectsToJSON(e.Args)) })() ``` -------------------------------- ### Checking Specific Error Types with Go Source: https://github.com/go-rod/go-rod.github.io/blob/main/error-handling.md Illustrates how to check for specific error types, such as context deadline exceeded or Rod's EvalError, using Go's standard `errors.Is` and `errors.As` functions. ```go func main() { // Assuming 'page' is already defined and connected // For demonstration, we'll use a placeholder for page.Element call // _, err := page.Element("a") // Replace with actual call that might produce an error var err error // Placeholder for an error handleError(err) } func handleError(err error) { var evalErr *rod.EvalError 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) } } ``` -------------------------------- ### Detect Timeout Errors Source: https://github.com/go-rod/go-rod.github.io/blob/main/context-and-timeout.md Illustrates how to detect and handle context deadline exceeded errors for operations wrapped with a timeout using rod.Try. ```go page := rod.New().MustConnect().MustPage() err := rod.Try(func() { page.Timeout(2 * time.Second).MustNavigate("http://github.com") }) if errors.Is(err, context.DeadlineExceeded) { fmt.Println("timeout error") } else if err != nil { fmt.Println("other types of error") } ``` -------------------------------- ### Simulate Date Input Source: https://github.com/go-rod/go-rod.github.io/blob/main/input.md Use MustInputTime() to input date, datetime-local, month, or time values into corresponding HTML input types. ```go page.MustElement(`[type="date"]`).MustInputTime(time.Now()) ``` -------------------------------- ### Simplified Error Handling with rod.Try Source: https://github.com/go-rod/go-rod.github.io/blob/main/error-handling.md Utilizes `rod.Try` to catch errors from 'Must' prefixed methods, often resulting in more concise code but potentially catching unexpected errors. ```go page := rod.New().MustConnect().MustPage("https://example.com") err := rod.Try(func() { fmt.Println(page.MustElement("a").MustHTML()) }) panic(err) ``` -------------------------------- ### Expose Go Function for MD5 Hashing Source: https://github.com/go-rod/go-rod.github.io/blob/main/javascript-runtime.md Expose a Go function (e.g., for MD5 hashing) to the JavaScript environment using Page.Expose. The page can then call this function as if it were a native JavaScript method. ```go page.MustExpose("md5", func(g gson.JSON) (interface{}, error) { return md5.Sum([]byte(g.Str())), nil }) ``` ```javascript hash := page.MustEval(`() => window.md5("test")`).Str() fmt.Println(hash) ``` -------------------------------- ### Simulate Mouse Clicks on an Element Source: https://github.com/go-rod/go-rod.github.io/blob/main/input.md Use MustClick() for a left click or Click() with specified button and click count for other mouse buttons. ```go // left click page.MustElement("button").MustClick() // right click _ = page.MustElement("button").Click(proto.InputMouseButtonRight, 1) ``` -------------------------------- ### Browser Cleanup Source: https://github.com/go-rod/go-rod.github.io/blob/main/custom-launch.md Utilize the Cleanup helper function to remove the browser's user data directory after it has been fully closed. This is useful for managing disk space and ensuring a clean state for subsequent launches. ```go func main() { l := launcher.New(). Headless(false). Devtools(true) defer l.Cleanup() } ``` -------------------------------- ### Cancel Navigation with Context Source: https://github.com/go-rod/go-rod.github.io/blob/main/context-and-timeout.md Demonstrates how to cancel a navigation operation after a specified duration using context.WithCancel. Operations on the cloned page are affected, but the original page remains unaffected. ```go page := rod.New().MustConnect().MustPage() ctx, cancel := context.WithCancel(context.Background()) pageWithCancel := page.Context(ctx) go func() { time.Sleep(2 * time.Second) cancel() }() // The 2 lines below share the same context, they will be canceled after 2 seconds in total pageWithCancel.MustNavigate("http://github.com") pageWithCancel.MustElement("body") ``` -------------------------------- ### Timeout Navigation Source: https://github.com/go-rod/go-rod.github.io/blob/main/context-and-timeout.md A simplified way to set a timeout for a navigation operation using the page.Timeout helper. ```go page := rod.New().MustConnect().MustPage() page.Timeout(2 * time.Second).MustNavigate("http://github.com") ``` -------------------------------- ### Select Options in a Select Element Source: https://github.com/go-rod/go-rod.github.io/blob/main/input.md Use MustSelect() to choose options by their text content. For more advanced selection, use Select() with regex or CSS selectors and a boolean to indicate selection or deselection. ```go page.MustElement("select").MustSelect("B", "C") ``` ```go _ = page.MustElement("select").Select([]string{`^B$`}, true, rod.SelectorTypeRegex) // set false to deselect _ = page.MustElement("select").Select([]string{`[value="c"]`}, false, rod.SelectorTypeCSSSector) ``` -------------------------------- ### Click Search Button Source: https://github.com/go-rod/go-rod.github.io/blob/main/get-started/README.md Locates the search button using its CSS selector and performs a click action. Assumes the input has already been processed. ```go package main import ( "time" "github.com/go-rod/rod" ) func main() { browser := rod.New().MustConnect().NoDefaultDevice() page := browser.MustPage("https://www.wikipedia.org/").MustWindowFullscreen() page.MustElement("#searchInput").MustInput("earth") page.MustElement("#search-form > fieldset > button").MustClick() page.MustWaitStable().MustScreenshot("a.png") time.Sleep(time.Hour) } ``` -------------------------------- ### Handle Multiple Event Types Source: https://github.com/go-rod/go-rod.github.io/blob/main/events/README.md Subscribes to multiple event types simultaneously, such as console API calls and page load events. Each event type has its own handler function. ```go go page.EachEvent(func(e *proto.RuntimeConsoleAPICalled) { fmt.Println(page.MustObjectsToJSON(e.Args)) }, func(e *proto.PageLoadEventFired) { fmt.Println("loaded") })() ``` -------------------------------- ### Input Text into Element Source: https://github.com/go-rod/go-rod.github.io/blob/main/get-started/README.md Locates an input element using a CSS selector and inputs the text 'earth'. Waits for the element to appear before inputting. ```go package main import ( "time" "github.com/go-rod/rod" ) func main() { browser := rod.New().MustConnect().NoDefaultDevice() page := browser.MustPage("https://www.wikipedia.org/").MustWindowFullscreen() page.MustElement("#searchInput").MustInput("earth") page.MustWaitStable().MustScreenshot("a.png") time.Sleep(time.Hour) } ``` -------------------------------- ### Replace Specific JavaScript File Response Source: https://github.com/go-rod/go-rod.github.io/blob/main/network/README.md This snippet shows how to hijack requests for a specific file ('*/test.js') and replace its response body. It demonstrates hijacking at the browser level and then within a specific page scope. ```go browser := rod.New().MustConnect() router := browser.HijackRequests() router.MustAdd("*/test.js", func(ctx *rod.Hijack) { ctx.MustLoadResponse() ctx.Response.SetBody(`console.log("js file replaced")`) }) go router.Run() page := browser.MustPage("https://test.com/") // Hijack requests under the scope of a page page.HijackRequests() ``` -------------------------------- ### Select Element by Text Content Source: https://github.com/go-rod/go-rod.github.io/blob/main/selectors/README.md Use ElementR to match elements with specific text content. Supports case-insensitive matching with the 'i' flag. ```go page.MustElementR("input", "Search or jump") page.MustElementR("input", "/click/i") ``` -------------------------------- ### Aborting Heartbeat with Context Source: https://github.com/go-rod/go-rod.github.io/blob/main/understand-context.md Illustrates the idiomatic Go way to cancel a long-running 'heartbeat' function using the `context` package. A new context is created with a cancel function, and the heartbeat listens for the context's Done channel. ```go func main() { ctx, stop := context.WithCancel(context.Background()) go func() { fmt.Scanln() stop() }() heartbeat(ctx) } func heartbeat(ctx context.Context) { tick := time.Tick(time.Second) for { select { case <-tick: case <-ctx.Done(): return } fmt.Println("beat") } } ``` -------------------------------- ### Aborting Heartbeat with Channels Source: https://github.com/go-rod/go-rod.github.io/blob/main/understand-context.md Demonstrates how to stop a long-running 'heartbeat' function using a channel for cancellation signals. The main goroutine waits for user input to close the channel. ```go func main() { stop := make(chan struct{}) go func() { fmt.Scanln() close(stop) }() heartbeat(stop) } func heartbeat(stop chan struct{}) { tick := time.Tick(time.Second) for { select { case <-tick: case <-stop: return } fmt.Println("beat") } } ``` -------------------------------- ### Wait for Network Idle Event Source: https://github.com/go-rod/go-rod.github.io/blob/main/events/README.md Waits for the network of a page to become almost idle after navigation. The subscription must precede the navigation trigger. ```go func main() { page := rod.New().MustConnect().MustPage() wait := page.MustWaitNavigation() page.MustNavigate("https://www.wikipedia.org/") wait() } ``` -------------------------------- ### Evaluate Javascript with Go Variables Source: https://github.com/go-rod/go-rod.github.io/blob/main/javascript-runtime.md Pass Go variables as arguments to JavaScript functions evaluated on the page. The 'key' and 'data' variables are used to set a property on the window object. ```go key := "a" data := map[string]string{"name": "jack"} page.MustEval(`(k, val) => { window[k] = val }`, key, data) ``` -------------------------------- ### Block Images from Loading Source: https://github.com/go-rod/go-rod.github.io/blob/main/network/README.md Use Page.HijackRequests to intercept network requests. Configure a router to block resources of type NetworkResourceTypeImage by failing the request. Other resource types are allowed to continue. ```go func main() { page := rod.New().MustConnect().MustPage("") router := page.HijackRequests() router.MustAdd("*.png", func(ctx *rod.Hijack) { // There're a lot of types you can use in this enum, like NetworkResourceTypeScript for javascript files // In this case we're using NetworkResourceTypeImage to block images if ctx.Request.Type() == proto.NetworkResourceTypeImage { ctx.Response.Fail(proto.NetworkErrorReasonBlockedByClient) return } ctx.ContinueRequest(&proto.FetchContinueRequest{}) }) // since we are only hijacking a specific page, even using the "*" won't affect much of the performance go router.Run() page.MustNavigate("https://github.com/").MustWaitStable().MustScreenshot("") } ``` -------------------------------- ### Search for Elements Source: https://github.com/go-rod/go-rod.github.io/blob/main/selectors/README.md Uses the MustSearch helper, similar to Devtools' Search for nodes, to find elements, especially useful in deeply nested iframes or shadow-doms. ```go page.MustSearch("button") ``` -------------------------------- ### Expose Go Function for Button Click Event Source: https://github.com/go-rod/go-rod.github.io/blob/main/javascript-runtime.md Expose a Go function to handle events like button clicks. The JavaScript code can then assign this exposed function to an element's event listener, such as 'onclick'. ```go page.MustExpose("myClick", func(v gson.JSON) (interface{}, error) { fmt.Println("Clicked") return nil, nil }) ``` ```javascript page.MustElement("button").MustEval(`() => this.onclick = myClick`) ``` -------------------------------- ### Select Element by JavaScript Source: https://github.com/go-rod/go-rod.github.io/blob/main/selectors/README.md Use ElementByJS for complex queries or to integrate with high-level query engines like jQuery. This is the base for other selectors. ```go page.MustElementByJS(`() => jQuery('option:selected')[0]`) ``` -------------------------------- ### Cancel Timeout for Subsequent Operations Source: https://github.com/go-rod/go-rod.github.io/blob/main/context-and-timeout.md Demonstrates how to cancel a previously set timeout for an element operation, allowing subsequent operations on the same page instance to proceed without the timeout constraint. ```go page. Timeout(2 * time.Second).MustElement("a"). CancelTimeout(). MustElement("b") // This line won't be affected by the 2 seconds timeout. ``` -------------------------------- ### Set default timezone for all pages Source: https://github.com/go-rod/go-rod.github.io/blob/main/emulation.md Sets the default timezone for all pages launched by the browser instance using the `TZ` environment variable. This is useful for ensuring consistent timezone behavior across all browser interactions. ```go u := launcher.New().Env(append(os.Environ(), "TZ=America/New_York")...).MustLaunch() rod.New().ControlURL(u).MustConnect() ``` -------------------------------- ### Set timezone override for a specific page Source: https://github.com/go-rod/go-rod.github.io/blob/main/emulation.md Overrides the timezone for a specific page using `EmulationSetTimezoneOverride`. This allows for granular control over timezone settings on a per-page basis. ```go page := browser.MustPage() _ = proto.EmulationSetTimezoneOverride{TimezoneID: "America/New_York"}.Call(page) ``` -------------------------------- ### View Extracted Text Output Source: https://github.com/go-rod/go-rod.github.io/blob/main/get-started/README.md The console output will display the extracted text content from the specified element. ```txt Earth is the third planet from the Sun and the only astronomical object known to harbor life. ... ``` -------------------------------- ### Evaluate Javascript to Set Global Value Source: https://github.com/go-rod/go-rod.github.io/blob/main/javascript-runtime.md Use Page.Eval to set a global JavaScript variable on the page. This is useful for initializing values or state. ```go page.MustEval(`() => window.a = {name: 'jack'}`) ``` -------------------------------- ### Long-Running Heartbeat Function Source: https://github.com/go-rod/go-rod.github.io/blob/main/understand-context.md A basic Go function that prints 'beat' every second. This serves as a foundation for demonstrating cancellation. ```go package main import ( "fmt" time "time" ) func main() { heartbeat() } func heartbeat() { tick := time.Tick(time.Second) for { <-tick fmt.Println("beat") } } ``` -------------------------------- ### Independent Page Operation Source: https://github.com/go-rod/go-rod.github.io/blob/main/context-and-timeout.md Shows that operations on the original page are not affected by cancellations applied to a context-cloned page. ```go page.MustNavigate("http://github.com") // won't be canceled after 2 seconds ``` -------------------------------- ### Remove Text from an Input Field Source: https://github.com/go-rod/go-rod.github.io/blob/main/input.md Simulate selecting all text and then inputting an empty string to clear the field. Alternatively, use SelectText() to target specific portions. ```go page.MustElement(`[type="text"]`).MustSelectAllText().MustInput("") ``` -------------------------------- ### Check/Uncheck a Checkbox Source: https://github.com/go-rod/go-rod.github.io/blob/main/input.md Interact with checkboxes by first checking their 'checked' property and then calling MustClick() if they are not in the desired state. ```go el := page.MustElement(`[type="checkbox"]`) // check it if not checked if !el.MustProperty("checked").Bool() { el.MustClick() } ``` -------------------------------- ### Evaluate Javascript on an Element Source: https://github.com/go-rod/go-rod.github.io/blob/main/javascript-runtime.md Use Element.Eval to execute JavaScript code with the 'this' context set to the target element. This allows direct manipulation of element properties like innerText. ```go el := page.MustElement("button") el.MustEval(`() => this.innerText = "Apply"`) // Modify the content txt := el.MustEval(`() => this.innerText`).Str() fmt.Println(txt) // output: Apply ``` -------------------------------- ### Stop Event Subscription by Returning True Source: https://github.com/go-rod/go-rod.github.io/blob/main/events/README.md Stops an event subscription by returning 'true' from the event handler function. This is demonstrated with a page load event. ```go wait := page.EachEvent(func(e *proto.PageLoadEventFired) (stop bool) { return true }) page.MustNavigate("https://example.com") wait() ``` -------------------------------- ### Disable default device emulation Source: https://github.com/go-rod/go-rod.github.io/blob/main/emulation.md Disables the default device emulation feature for the browser by setting the default device to `devices.Clear`. This reverts browser settings to their default state. ```go browser.DefaultDevice(devices.Clear) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.