Best Rod code snippet using rod.URL
rod-helper.go
Source:rod-helper.go
...11 "time"12)13/**14 * @Description: æ°å»ºä¸ä¸ªæ¯æ代çç browser 对象15 * @param httpProxyURL http://127.0.0.1:1080916 * @return *rod.Browser17 * @return error18 */19func NewBrowser(httpProxyURL string) (*rod.Browser, error) {20 var browser *rod.Browser21 err := rod.Try(func() {22 u := launcher.New().23 Proxy(httpProxyURL).24 MustLaunch()25 browser = rod.New().ControlURL(u).MustConnect()26 })27 if err != nil {28 return nil, err29 }30 return browser, nil31}32/**33 * @Description: 访é®ç®æ Urlï¼è¿å pageï¼åªæ¯è¿ä¸ª page ææï¼å¦æå次åºåå
¶ä»çäºä»¶æ æ34 * @param desURL ç®æ Url35 * @param httpProxyURL http://127.0.0.1:1080936 * @param timeOut è¶
æ¶æ¶é´37 * @param maxRetryTimes å½æ¯éè¶
æ¶ err çæ¶åï¼æå¤å¯ä»¥éè¯å 次38 * @return *rod.Page39 * @return error40 */41func NewBrowserFromDocker(httpProxyURL, remoteDockerURL string) (*rod.Browser, error) {42 var browser *rod.Browser43 err := rod.Try(func() {44 l := launcher.MustNewRemote(remoteDockerURL)45 u := l.Proxy(httpProxyURL).MustLaunch()46 l.Headless(false).XVFB()47 browser = rod.New().Client(l.Client()).ControlURL(u).MustConnect()48 })49 if err != nil {50 return nil, err51 }52 return browser, nil53}54func NewPage(browser *rod.Browser) (*rod.Page, error) {55 page, err := browser.Page(proto.TargetCreateTarget{URL: ""})56 if err != nil {57 return nil, err58 }59 return page, err60}61func NewPageNavigate(browser *rod.Browser, desURL string, timeOut time.Duration, maxRetryTimes int) (*rod.Page, error) {62 page, err := NewPage(browser)63 if err != nil {64 return nil, err65 }66 page = page.Timeout(timeOut)67 nowRetryTimes := 068 for nowRetryTimes <= maxRetryTimes {69 err = rod.Try(func() {70 wait := page.MustWaitNavigation()71 page.MustNavigate(desURL)72 wait()73 })74 if errors.Is(err, context.DeadlineExceeded) {75 // è¶
æ¶76 return nil, err77 } else if err == nil {78 // 没æé®é¢79 return page, nil80 }81 }82 return nil, err83}84func PageNavigate(page *rod.Page, desURL string, timeOut time.Duration, maxRetryTimes int) (*rod.Page, error) {85 var err error86 page = page.Timeout(timeOut)87 nowRetryTimes := 088 for nowRetryTimes <= maxRetryTimes {89 err = rod.Try(func() {90 wait := page.MustWaitNavigation()91 page.MustNavigate(desURL)92 wait()93 })94 if errors.Is(err, context.DeadlineExceeded) {95 // è¶
æ¶96 return nil, err97 } else if err == nil {98 // 没æé®é¢99 return page, nil100 }101 }102 return nil, err103}104/**105 * @Description: 访é®ç®æ Urlï¼è¿å pageï¼åªæ¯è¿ä¸ª page ææï¼å¦æå次åºåå
¶ä»çäºä»¶æ æ106 * @param desURL ç®æ Url107 * @param httpProxyURL http://127.0.0.1:10809108 * @param timeOut è¶
æ¶æ¶é´109 * @param maxRetryTimes å½æ¯éè¶
æ¶ err çæ¶åï¼æå¤å¯ä»¥éè¯å 次110 * @return *rod.Page111 * @return error112 */113func NewBrowserLoadPage(desURL string, httpProxyURL string, timeOut time.Duration, maxRetryTimes int) (*rod.Page, error) {114 browser, err := NewBrowser(httpProxyURL)115 if err != nil {116 return nil, err117 }118 page, err := browser.Page(proto.TargetCreateTarget{URL: ""})119 if err != nil {120 return nil, err121 }122 page = page.Timeout(timeOut)123 nowRetryTimes := 0124 for nowRetryTimes <= maxRetryTimes {125 err = rod.Try(func() {126 wait := page.MustWaitNavigation()127 page.MustNavigate(desURL)128 wait()129 })130 if errors.Is(err, context.DeadlineExceeded) {131 // è¶
æ¶132 return nil, err133 } else if err == nil {134 // 没æé®é¢135 return page, nil136 }137 }138 return nil, err139}140/**141 * @Description: 访é®ç®æ Urlï¼è¿å pageï¼åªæ¯è¿ä¸ª page ææï¼å¦æå次åºåå
¶ä»çäºä»¶æ æ142 * @param desURL ç®æ Url143 * @param httpProxyURL http://127.0.0.1:10809144 * @param timeOut è¶
æ¶æ¶é´145 * @param maxRetryTimes å½æ¯éè¶
æ¶ err çæ¶åï¼æå¤å¯ä»¥éè¯å 次146 * @return *rod.Page147 * @return error148 */149func NewBrowserLoadPageFromRemoteDocker(desURL string, httpProxyURL, remoteDockerURL string, timeOut time.Duration, maxRetryTimes int) (*rod.Page, error) {150 browser, err := NewBrowserFromDocker(httpProxyURL, remoteDockerURL)151 if err != nil {152 return nil, err153 }154 page, err := browser.Page(proto.TargetCreateTarget{URL: ""})155 if err != nil {156 return nil, err157 }158 page = page.Timeout(timeOut)159 nowRetryTimes := 0160 for nowRetryTimes <= maxRetryTimes {161 err = rod.Try(func() {162 wait := page.MustWaitNavigation()163 page.MustNavigate(desURL)164 wait()165 })166 if errors.Is(err, context.DeadlineExceeded) {167 // è¶
æ¶168 return nil, err169 } else if err == nil {170 // 没æé®é¢171 break172 }173 }174 return page, nil175}176/**177 * @Description: 访é®ç®æ Urlï¼è¿å pageï¼åªæ¯è¿ä¸ª page ææï¼å¦æå次åºåå
¶ä»çäºä»¶æ æ178 * @param desURL ç®æ Url179 * @param httpProxyURL http://127.0.0.1:10809180 * @param timeOut è¶
æ¶æ¶é´181 * @param maxRetryTimes å½æ¯éè¶
æ¶ err çæ¶åï¼æå¤å¯ä»¥éè¯å 次182 * @return *rod.Page183 * @return error184 */185func NewBrowserLoadPageByHijackRequests(desURL string, httpProxyURL string, timeOut time.Duration, maxRetryTimes int) (*rod.Page, error) {186 var page *rod.Page187 var err error188 // å建ä¸ä¸ª page189 browser := rod.New()190 err = browser.Connect()191 if err != nil {192 return nil, err193 }194 page, err = browser.Page(proto.TargetCreateTarget{URL: ""})195 if err != nil {196 return nil, err197 }198 page = page.Timeout(timeOut)199 // 设置代ç200 router := page.HijackRequests()201 defer router.Stop()202 err = rod.Try(func() {203 router.MustAdd("*", func(ctx *rod.Hijack) {204 px, _ := url.Parse(httpProxyURL)205 ctx.LoadResponse(&http.Client{206 Transport: &http.Transport{207 Proxy: http.ProxyURL(px),208 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},209 },210 Timeout: timeOut,211 }, true)212 })213 })214 if err != nil {215 return nil ,err216 }217 go router.Run()218 nowRetryTimes := 0219 for nowRetryTimes <= maxRetryTimes {220 err = rod.Try(func() {221 page.MustNavigate(desURL).MustWaitLoad()222 })223 if errors.Is(err, context.DeadlineExceeded) {224 // è¶
æ¶225 return nil, err226 } else if err == nil {227 // 没æé®é¢228 break229 }230 time.Sleep(time.Second)231 nowRetryTimes++232 }233 return page, nil234}...
rod_helper.go
Source:rod_helper.go
...14 nowUserData := filepath.Join(pkg.GetTmpFolderFPath(), pkg.RandStringBytesMaskImprSrcSB(20))15 purl := launcher.New().16 UserDataDir(nowUserData).17 MustLaunch()18 return rod.New().ControlURL(purl).MustConnect()19}20func NewPageNavigate(browser *rod.Browser, proxyUrl, desURL string, timeOut time.Duration) (*rod.Page, int, string, error) {21 page, err := newPage(browser)22 if err != nil {23 return nil, 0, "", err24 }25 return PageNavigate(page, proxyUrl, desURL, timeOut)26}27func PageNavigate(page *rod.Page, proxyUrl, desURL string, timeOut time.Duration) (*rod.Page, int, string, error) {28 router := page.HijackRequests()29 defer router.Stop()30 router.MustAdd("*", func(ctx *rod.Hijack) {31 px, _ := url.Parse(proxyUrl)32 err := ctx.LoadResponse(&http.Client{33 Transport: &http.Transport{34 Proxy: http.ProxyURL(px),35 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},36 },37 }, true)38 if err != nil {39 return40 }41 })42 go router.Run()43 err := page.SetUserAgent(&proto.NetworkSetUserAgentOverride{44 UserAgent: pkg.RandomUserAgent(true),45 })46 if err != nil {47 if page != nil {48 page.Close()49 }50 return nil, 0, "", err51 }52 var e proto.NetworkResponseReceived53 wait := page.WaitEvent(&e)54 page = page.Timeout(timeOut)55 err = rod.Try(func() {56 page.MustNavigate(desURL)57 wait()58 })59 if err != nil {60 if page != nil {61 page.Close()62 }63 return nil, 0, "", err64 }65 // åºå»åæ TimeOUt åæ¶äº66 page = page.CancelTimeout()67 Status := e.Response.Status68 ResponseURL := e.Response.URL69 return page, Status, ResponseURL, nil70}71func newPage(browser *rod.Browser) (*rod.Page, error) {72 page, err := browser.Page(proto.TargetCreateTarget{URL: ""})73 if err != nil {74 return nil, err75 }76 return page, err77}...
pdq.go
Source:pdq.go
...9var url string10//Creates a new browser, sets headless to be false, and clears the defualt device to laod the page correctly11func newBrowser() *rod.Browser {12 launcher := launcher.New().Headless(false).MustLaunch()13 browser := rod.New().ControlURL(launcher).MustConnect().DefaultDevice(devices.Clear)14 return browser15}16//Listens for a page load event and gets pageload metrics and memory usage17func onPageLoadEventFired(page *rod.Page, quit chan bool) func() {18 wait := page.EachEvent(19 func(p *proto.PageLoadEventFired) {20 select {21 case <-quit:22 close(quit)23 return24 default:25 getPageLoadTimingsOverlay(page)26 logMemoryUsage(page)27 quit <- true28 break29 }30 })31 return wait32}33//Listens for a target change event, gets FPS overlay, and waits for page load events34func onTargetInfoChanged(browser *rod.Browser) func() {35 wait := browser.EachEvent(36 func(t *proto.TargetTargetInfoChanged) {37 quit := make(chan bool)38 if url != t.TargetInfo.URL {39 page, err := browser.PageFromTarget(t.TargetInfo.TargetID)40 explain(err)41 getShowFPSCounterOverlay(page)42 w := onPageLoadEventFired(page, quit)43 go w()44 url = t.TargetInfo.URL45 }46 })47 return wait48}...
URL
Using AI Code Generation
1import (2func main() {3 browser := rod.New().MustConnect()4 page.MustElement("input[name=q]").MustInput("rod").MustPress(input.Enter)5 fmt.Println(page.MustElement("h3").MustText())6}
URL
Using AI Code Generation
1import (2func main() {3 browser := rod.New().MustConnect()4 fmt.Println(page.MustInfo().URL)5}6import (7func main() {8 browser := rod.New().MustConnect()9 fmt.Println(page.MustInfo().Title)10}11import (12func main() {13 browser := rod.New().MustConnect()14 fmt.Println(page.MustInfo().Width)15}16import (17func main() {18 browser := rod.New().MustConnect()19 fmt.Println(page.MustInfo().Height)20}21import (22func main() {23 browser := rod.New().MustConnect()24 fmt.Println(page.MustInfo().DeviceScaleFactor)25}26import (27func main() {28 browser := rod.New().MustConnect()29 fmt.Println(page.MustInfo().Mobile)30}31import (32func main() {33 browser := rod.New().MustConnect()34 fmt.Println(page.MustInfo().HasTouch)35}36import (
URL
Using AI Code Generation
1import (2func main() {3 browser := rod.New().MustConnect()4 page.MustElement("input[name=q]").MustInput("rod")5 page.MustElement("input[name=btnK]").MustClick()6 page.MustWaitLoad()7 fmt.Println(page.MustElement("h3").MustText())8}
URL
Using AI Code Generation
1import (2func main() {3 l := launcher.New().Bin("/usr/bin/google-chrome-stable").Headless(false).MustLaunch()4 defer l.Kill()5 fmt.Println("URL:", l.URL())6}
URL
Using AI Code Generation
1import (2func main() {3 browser := rod.New().Connect()4 fmt.Println(page.URL())5}6import (7func main() {8 browser := rod.New().Connect()9 page.WaitLoad()10 fmt.Println(page.URL())11}12import (13func main() {14 browser := rod.New().Connect()15 page.WaitLoad()16 fmt.Println(page.URL())17}18import (19func main() {20 browser := rod.New().Connect()21 page.WaitLoad()22 fmt.Println(page.URL())23}24import (25func main() {26 browser := rod.New().Connect()27 page.WaitLoad()28 fmt.Println(page.URL())29}30import (31func main() {32 browser := rod.New().Connect()33 page.WaitLoad()34 fmt.Println(page.URL())35}36import (37func main() {
URL
Using AI Code Generation
1import (2func main() {3 l := launcher.New().Bin("chromium").Headless(false).MustLaunch()4 defer l.Kill()5 b := rod.New().ControlURL(l).MustConnect()6 defer b.Close()7 page.MustElement("a").MustClick()8}9import (10func main() {11 page.MustElement("a").MustClick()12}13import (14func main() {15 page.MustElement("a").MustClick()16}17import (18func main() {19 page.MustElement("a").MustClick()20}21import (22func main() {23 page.MustElement("a").MustClick()24}25import (26func main() {27 page.MustElement("a").MustClick()28}29import (
URL
Using AI Code Generation
1import (2func main() {3 fmt.Println(page.MustURL())4}5import (6func main() {7 fmt.Println(page.MustTitle())8}9import (10func main() {11 fmt.Println(page.MustEval(`document.title`))12}13import (14func main() {15 fmt.Println(page.MustTitle())16 fmt.Println(page.MustTitle())17}18import (19func main() {20 fmt.Println(page.MustTitle())21 fmt.Println(page.MustTitle())22 page.MustReload()23 fmt.Println(page.MustTitle())24}25import (26func main() {27 fmt.Println(page.MustTitle
URL
Using AI Code Generation
1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 q := u.Query()7 q.Set("q", "rod")8 u.RawQuery = q.Encode()9 fmt.Println(u)10 browser := rod.New().MustConnect()11 defer browser.MustClose()12 page := browser.MustPage(u.String()).MustWaitLoad()13 page.MustElement("input[name=q]").MustInput("rod").MustPress(input.Enter)14 page.MustScreenshot("screenshot.png")15}16import (17func main() {18 if err != nil {19 log.Fatal(err)20 }21 q := u.Query()22 q.Set("q", "rod")23 u.RawQuery = q.Encode()24 fmt.Println(u)25 browser := rod.New().MustConnect()26 defer browser.MustClose()27 page := browser.MustPage(u.String()).MustWaitLoad()28 page.MustElement("input[name=q]").MustInput("rod").MustPress(input.Enter)29 page.MustScreenshot("screenshot.png")30}31import (32func main() {33 if err != nil {34 log.Fatal(err)35 }36 q := u.Query()37 q.Set("q", "rod")38 u.RawQuery = q.Encode()39 fmt.Println(u)40 browser := rod.New().MustConnect()41 defer browser.MustClose()42 page := browser.MustPage(u.String()).MustWaitLoad()43 page.MustElement("input[name=q]").MustInput("rod").MustPress(input.Enter)44 page.MustScreenshot("screenshot.png")45}46import (47func main() {48 if err != nil {49 log.Fatal(err)50 }
URL
Using AI Code Generation
1import (2func main() {3 if err != nil {4 fmt.Println("Error: ", err)5 os.Exit(1)6 }7 fmt.Println("Scheme: ", u.Scheme)8 fmt.Println("Host: ", u.Host)9 fmt.Println("Path: ", u.Path)10 fmt.Println("Query: ", u.RawQuery)11 fmt.Println("Fragment: ", u.Fragment)12 m, _ := url.ParseQuery(u.RawQuery)13 fmt.Println("Query Param 'q': ", m["q"][0])14}15import (16func main() {17 if err != nil {18 fmt.Println("Error: ", err)19 os.Exit(1)20 }21 client := &http.Client{}22 resp, err := client.Do(req)23 if err != nil {24 fmt.Println("Error: ", err)25 os.Exit(1)26 }27 defer resp.Body.Close()28 body, err := ioutil.ReadAll(resp.Body)29 if err != nil {30 fmt.Println("
URL
Using AI Code Generation
1import (2func main() {3 if err != nil {4 panic(err)5 }6 fmt.Printf("%#v7 fmt.Println(u.Scheme)8 fmt.Println(u.Host)9 fmt.Println(u.Path)10 fmt.Println(u.RawQuery)11 fmt.Println(u.Fragment)12}13import (14func main() {15 if err != nil {16 panic(err)17 }18 fmt.Printf("%#v19 fmt.Println(u.Scheme)20 fmt.Println(u.Host)21 fmt.Println(u.Path)22 fmt.Println(u.RawQuery)23 fmt.Println(u.Fragment)24}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!