How to use Try method of rod Package

Best Rod code snippet using rod.Try

browser_test.go

Source:browser_test.go Github

copy

Full Screen

...182 g.Panic(func() {183 p.MustNavigate(s.URL())184 })185}186func TestTestTry(t *testing.T) {187 g := setup(t)188 g.Nil(rod.Try(func() {}))189 err := rod.Try(func() { panic(1) })190 var errVal *rod.ErrTry191 g.True(errors.As(err, &errVal))192 g.Is(err, &rod.ErrTry{})193 g.Eq(errVal.Unwrap().Error(), "1")194 g.Eq(1, errVal.Value)195 g.Has(errVal.Error(), "error value: 1\ngoroutine")196 errVal = rod.Try(func() { panic(errors.New("t")) }).(*rod.ErrTry)197 g.Eq(errVal.Unwrap().Error(), "t")198}199func TestBrowserOthers(t *testing.T) {200 g := setup(t)201 g.browser.Timeout(time.Second).CancelTimeout().MustGetCookies()202}203func TestBinarySize(t *testing.T) {204 g := setup(t)205 if runtime.GOOS == "windows" || utils.InContainer {206 g.SkipNow()207 }208 cmd := exec.Command("go", "build",209 "-trimpath",210 "-ldflags", "-w -s",...

Full Screen

Full Screen

rod-helper.go

Source:rod-helper.go Github

copy

Full Screen

...17 * @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}...

Full Screen

Full Screen

scraper_rod.go

Source:scraper_rod.go Github

copy

Full Screen

...31func (s *RodScraper) SetDepth(depth int) {32 s.depth = depth33}34func (s *RodScraper) Init() error {35 return rod.Try(func() {36 path, _ := launcher.LookPath()37 u := launcher.New().Bin(path).NoSandbox(true).MustLaunch()38 s.lock = &sync.RWMutex{}39 s.robotsMap = make(map[string]*robotstxt.RobotsData)40 s.protoUserAgent = &proto.NetworkSetUserAgentOverride{UserAgent: s.UserAgent}41 s.Browser = rod.42 New().43 ControlURL(u).44 MustConnect().45 MustIgnoreCertErrors(true)46 })47}48func (s *RodScraper) Scrape(paramURL string) (*ScrapedData, error) {49 scraped := &ScrapedData{}50 parsedURL, err := url.Parse(paramURL)51 if err != nil {52 return scraped, err53 }54 if s.depth > 0 {55 if err := s.checkRobots(parsedURL); err != nil {56 return scraped, err57 }58 }59 var e proto.NetworkResponseReceived60 s.Page = s.Browser.MustPage("")61 wait := s.Page.WaitEvent(&e)62 go s.Page.MustHandleDialog()63 errRod := rod.Try(func() {64 s.Page.65 Timeout(time.Duration(s.TimeoutSeconds) * time.Second).66 MustSetUserAgent(s.protoUserAgent).67 MustNavigate(paramURL)68 })69 if errRod != nil {70 fmt.Fprintln(color.Output, color.HiRedString("[ERRO]"), "["+time.Now().Format("2006-01-02 15:04:05")+"]", "[GoWappRod]:", "Error while visiting", paramURL, errRod.Error())71 return scraped, errRod72 }73 wait()74 if e.Response.SecurityDetails != nil && len(e.Response.SecurityDetails.Issuer) > 0 {75 scraped.CertIssuer = append(scraped.CertIssuer, e.Response.SecurityDetails.Issuer)76 }77 scraped.URLs = ScrapedURL{e.Response.URL, e.Response.Status}78 scraped.Headers = make(map[string][]string)79 for header, value := range e.Response.Headers {80 lowerCaseKey := strings.ToLower(header)81 scraped.Headers[lowerCaseKey] = append(scraped.Headers[lowerCaseKey], value.String())82 }83 scraped.DNS = scrapeDNS(paramURL)84 //TODO : headers and cookies could be parsed before load completed85 errRod = rod.Try(func() {86 s.Page.87 Timeout(time.Duration(s.LoadingTimeoutSeconds) * time.Second).88 MustWaitLoad()89 })90 if errRod != nil {91 fmt.Fprintln(color.Output, color.HiRedString("[ERRO]"), "["+time.Now().Format("2006-01-02 15:04:05")+"]", "[GoWappRod]:", "Error while loading", paramURL, errRod.Error())92 return scraped, errRod93 }94 scraped.HTML = s.Page.MustHTML()95 scripts, _ := s.Page.Elements("script")96 for _, script := range scripts {97 if src, _ := script.Property("src"); src.Val() != nil {98 scraped.Scripts = append(scraped.Scripts, src.String())99 }...

Full Screen

Full Screen

Try

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().MustConnect()4 defer browser.MustClose()5 page.MustElement("input[name=q]").MustInput("rod")6 _, err := page.Try(func() {7 page.MustElement("not-exists").MustClick()8 })9}10import (11func main() {12 browser := rod.New().MustConnect()13 defer browser.MustClose()14 page.MustElement("input[name=q]").MustInput("rod")15 page.WaitOpen(func() bool {16 return page.MustHas(".g")17 })18 fmt.Println("page loaded")19}20import (21func main() {22 browser := rod.New().MustConnect()23 defer browser.MustClose()24 page.MustElement("input[name=q]").MustInput("rod")25 page.WaitRequestIdle()26 fmt.Println("no more requests")27}28import (29func main() {30 browser := rod.New().MustConnect()31 defer browser.MustClose()32 page.MustElement("input[name=q]").MustInput("rod")33 page.WaitLoad()34 fmt.Println("page loaded")35}

Full Screen

Full Screen

Try

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4 title, err := page.Title().Try()5 if err != nil {6 fmt.Println("error")7 } else {8 fmt.Println(title)9 }10}11import (12func main() {13 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()14 title, err := page.Title().Timeout(10).Try()15 if err != nil {16 fmt.Println("error")17 } else {18 fmt.Println(title)19 }20}21import (22func main() {23 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()24 title, err := page.Title().Timeout(10).Try()25 if err != nil {26 fmt.Println("error")27 } else {

Full Screen

Full Screen

Try

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 browser := rod.New().MustConnect()5 defer browser.MustClose()6 page.MustElement("input[name=q]").MustInput("rod")7 page.MustElement("input[name=q]").MustPress("Enter")8 page.MustElement("div.g").MustElement("a").MustClick()9 fmt.Println(page.MustTitle())10}11import (12func main() {13 fmt.Println("Hello, playground")14 browser := rod.New().MustConnect()15 defer browser.MustClose()16 page.MustElement("input[name=q]").MustInput("rod")17 page.MustElement("input[name=q]").MustPress("Enter")18 page.MustElement("div.g").MustElement("a").MustClick()19 fmt.Println(page.MustTitle())20}21import (22func main() {23 fmt.Println("Hello, playground")24 browser := rod.New().MustConnect()25 defer browser.MustClose()26 page.MustElement("input[name=q]").MustInput("rod")27 page.MustElement("input[name=q]").MustPress("Enter")28 page.MustElement("div.g").MustElement("a").MustClick()29 fmt.Println(page.MustTitle())30}31import (32func main() {33 fmt.Println("Hello, playground")34 browser := rod.New().MustConnect()35 defer browser.MustClose()36 page.MustElement("input[name=q]").MustInput("rod")37 page.MustElement("input[name=q]").MustPress("Enter")38 page.MustElement("div.g").MustElement("a").MustClick()39 fmt.Println(page.MustTitle())40}41import (

Full Screen

Full Screen

Try

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().MustConnect()4 input := page.MustElement("input[name='q']")5 button := page.MustElement("input[name='btnK']")6 input.MustInput("rod")7 button.MustClick()8 page.MustWaitLoad()9 page.MustScreenshot("rod.png")10 fmt.Println("Success")11}

Full Screen

Full Screen

Try

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4 page.MustWaitLoad()5 title := page.MustElement("title").MustText()6 fmt.Println(title)7}8import (9func main() {10 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()11 page.MustWaitLoad()12 title := page.MustElement("title").MustText()13 fmt.Println(title)14}15import (16func main() {17 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()18 page.MustWaitLoad()19 title := page.MustElement("title").MustText()20 fmt.Println(title)21}22import (23func main() {

Full Screen

Full Screen

Try

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 input := page.MustElement("input")4 if input == nil {5 fmt.Println("input element not found")6 } else {7 fmt.Println(input)8 }9}10import (11func main() {12 input := page.MustElement("input")13 if input == nil {14 fmt.Println("input element not found")15 } else {16 fmt.Println(input)17 }18}19import (20func main() {21 input := page.ElementR("input")22 if input == nil {23 fmt.Println("input element not found")24 } else {25 fmt.Println(input)26 }27}28import (29func main() {30 input := page.Element("input")31 if input == nil {32 fmt.Println("input element not found")33 }

Full Screen

Full Screen

Try

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r = rods.Rod{Length: 10}4 fmt.Println(r.Try())5}6import (7func main() {8 r = rods.Rod{Length: 11}9 fmt.Println(r.Try())10}11import (12func main() {13 r = rods.Rod{Length: 12}14 fmt.Println(r.Try())15}16import (17func main() {18 r = rods.Rod{Length: 13}19 fmt.Println(r.Try())20}21import (22func main() {23 r = rods.Rod{Length: 14}24 fmt.Println(r.Try())25}26import (27func main() {28 r = rods.Rod{Length: 15}29 fmt.Println(r.Try())30}31import (32func main() {33 r = rods.Rod{Length: 16}34 fmt.Println(r.Try())35}36import (37func main() {38 r = rods.Rod{Length: 17}39 fmt.Println(r.Try())40}41import (42func main() {43 r = rods.Rod{Length: 18

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Rod automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful