Best Rod code snippet using rod.CancelTimeout
main.go
Source:main.go
...39 wait := page.MustWaitNavigation()40 wait()41 page.MustWaitLoad()42 time.Sleep(time.Second * 5)43 page.Timeout(15 * time.Second).MustElement("#app > div.tiktok-12azhi0-DivHeaderContainer.e10win0d0 > div > div.tiktok-ba55d9-DivHeaderRightContainer.e13wiwn60 > button").CancelTimeout()44 page.MustElement("#app > div.tiktok-12azhi0-DivHeaderContainer.e10win0d0 > div > div.tiktok-ba55d9-DivHeaderRightContainer.e13wiwn60 > button").MustClick()45 page.MustWaitLoad()46 time.Sleep(time.Second * 5)47 page.Timeout(15 * time.Second).MustElement("#loginContainer > div > div > div:nth-child(5)").CancelTimeout()48 page.MustElement("#loginContainer > div > div > div:nth-child(5)").MustClick().MustWaitInvisible()49 newPage := page.MustWaitOpen()50 newTab := newPage()51 newTab.EvalOnNewDocument(stealth.JS)52 //page.MustWaitLoad()53 time.Sleep(time.Second * 1000000)54 //ele, error := page.Element("#loginContainer > div > div > div:nth-child(5)")55 //if error != nil {56 // fmt.Println("Vao day")57 // ele.MustClick().MustWaitInvisible()58 // page.MustWaitLoad()59 // time.Sleep(time.Second * 1000000)60 //}61 time.Sleep(time.Second * 1000000)62 //page.MustElement(FIRST_NAME_INPUT).MustInput("Bùi Doãn")63 //time.Sleep(time.Millisecond * 5)64 //page.MustElement(LAST_NAME_INPUT).MustInput("Hà ")65 //time.Sleep(time.Millisecond * 5)66 //page.MustElement(USER_NAME_INPUT).MustInput("buidoanha24535612")67 //time.Sleep(time.Millisecond * 5)68 //page.MustElement(PASSWD_INPUT).MustInput("Buidoanha@1")69 //time.Sleep(time.Millisecond * 5)70 //page.MustElement(CONFIRM_PASSWD_INPUT).MustInput("Buidoanha@1")71 //page.MustElement(BUTTON_NEXT_ACCOUNT_GOOGLE).MustClick().MustWaitInvisible()72 //time.Sleep(time.Millisecond * 10)73 //page.MustWaitLoad()74 //page.Timeout(15 * time.Second).MustElement("img[src^='https://ssl.gstatic.com/accounts/signup/glif/account.svg']").CancelTimeout()75 //page.MustElement(RECOVERY_EMAIL_INPUT).MustInput("miaooo.sl.ys.lyn.hyeon.lb@gmail.com")76 //time.Sleep(time.Millisecond * 5)77 //page.MustElement(DAY_INPUT).MustInput("5")78 //time.Sleep(time.Millisecond * 5)79 //page.MustElement(MONTH_INPUT).Select([]string{`[value="2"]`}, false, rod.SelectorTypeCSSSector)80 //time.Sleep(time.Millisecond * 5)81 //page.MustElement(YEAR_INPUT).MustInput("1999")82 //time.Sleep(time.Millisecond * 5)83 //page.MustElement(GENDER_INPUT).Select([]string{`[value="1"]`}, false, rod.SelectorTypeCSSSector)84 //time.Sleep(time.Millisecond * 5)85 //page.MustElement(BUTTON_NEXT_ACCOUNT_GOOGLE).MustClick().MustWaitInvisible()86 //time.Sleep(time.Millisecond * 10)87 //page.MustWaitLoad()88 //page.Timeout(15 * time.Second).MustElement("img[src^='https://ssl.gstatic.com/accounts/signup/glif/privacy.svg']").CancelTimeout()89 //page.MustElement(BUTTON_NEXT_ACCOUNT_GOOGLE).MustScrollIntoView().MustClick().MustWaitInvisible()90 //page.MustWaitLoad()91 el, error := page.Element(IS_LOGIN_GMAIL)92 if error != nil {93 // print the username after successful login94 content := el.MustAttribute("aria-label")95 contentValue := *content96 if strings.Contains(contentValue, "buidoanhai2451242") {97 fmt.Println("Register success")98 } else {99 fmt.Println("Register failed")100 }101 }102 //if page.MustHas("img[src^='https://ssl.gstatic.com/accounts/signup/glif/account.svg']") {...
context.go
Source:context.go
...23func (b *Browser) Timeout(d time.Duration) *Browser {24 ctx, cancel := context.WithTimeout(b.ctx, d)25 return b.Context(context.WithValue(ctx, timeoutContextKey{}, &timeoutContextVal{b.ctx, cancel}))26}27// CancelTimeout åæ¶å½åè¶
æ¶ä¸ä¸æï¼å¹¶è¿åå
·æç¶ä¸ä¸æçå
é28func (b *Browser) CancelTimeout() *Browser {29 val := b.ctx.Value(timeoutContextKey{}).(*timeoutContextVal)30 val.cancel()31 return b.Context(val.parent)32}33// WithCancel è¿å带æä¸ä¸æåæ¶å½æ°çå
é34func (b *Browser) WithCancel() (*Browser, func()) {35 ctx, cancel := context.WithCancel(b.ctx)36 return b.Context(ctx), cancel37}38// Sleeper 为é¾å¼åæä½è¿åå
·ææå®Sleeperçå
é39func (b *Browser) Sleeper(sleeper func() utils.Sleeper) *Browser {40 newObj := *b41 newObj.sleeper = sleeper42 return &newObj43}44// Context è¿åå
·ææå®ctxçå
éï¼ç¨äºé¾å¼åæä½45func (p *Page) Context(ctx context.Context) *Page {46 newObj := *p47 newObj.ctx = ctx48 return &newObj49}50// GetContext è·åå½åctxå®ä¾51func (p *Page) GetContext() context.Context {52 return p.ctx53}54// Timeout è¿åä¸ä¸ªå
éï¼å
¶ä¸å
å«ææé¾æ¥åæä½çæå®æ»è¶
æ¶55func (p *Page) Timeout(d time.Duration) *Page {56 ctx, cancel := context.WithTimeout(p.ctx, d)57 return p.Context(context.WithValue(ctx, timeoutContextKey{}, &timeoutContextVal{p.ctx, cancel}))58}59// CancelTimeout åæ¶å½åè¶
æ¶ä¸ä¸æï¼å¹¶è¿åå
·æç¶ä¸ä¸æçå
é60func (p *Page) CancelTimeout() *Page {61 val := p.ctx.Value(timeoutContextKey{}).(*timeoutContextVal)62 val.cancel()63 return p.Context(val.parent)64}65// WithCancel è¿å带æä¸ä¸æåæ¶å½æ°çå
é66func (p *Page) WithCancel() (*Page, func()) {67 ctx, cancel := context.WithCancel(p.ctx)68 return p.Context(ctx), cancel69}70// Sleeper 为é¾å¼åæä½è¿åå
·ææå®Sleeperçå
é71func (p *Page) Sleeper(sleeper func() utils.Sleeper) *Page {72 newObj := *p73 newObj.sleeper = sleeper74 return &newObj75}76// Context è¿åå
·ææå®ctxçå
éï¼ç¨äºé¾å¼åæä½77func (el *Element) Context(ctx context.Context) *Element {78 newObj := *el79 newObj.ctx = ctx80 return &newObj81}82// GetContext è·åå½åctxçå®ä¾83func (el *Element) GetContext() context.Context {84 return el.ctx85}86// Timeout è¿åä¸ä¸ªå
éï¼å
¶ä¸å
å«ææé¾æ¥åæä½çæå®æ»è¶
æ¶87func (el *Element) Timeout(d time.Duration) *Element {88 ctx, cancel := context.WithTimeout(el.ctx, d)89 return el.Context(context.WithValue(ctx, timeoutContextKey{}, &timeoutContextVal{el.ctx, cancel}))90}91// CancelTimeout åæ¶å½åè¶
æ¶ä¸ä¸æï¼å¹¶è¿åå
·æç¶ä¸ä¸æçå
é92func (el *Element) CancelTimeout() *Element {93 val := el.ctx.Value(timeoutContextKey{}).(*timeoutContextVal)94 val.cancel()95 return el.Context(val.parent)96}97// WithCancel è¿å带æä¸ä¸æåæ¶å½æ°çå
é98func (el *Element) WithCancel() (*Element, func()) {99 ctx, cancel := context.WithCancel(el.ctx)100 return el.Context(ctx), cancel101}102// Sleeper 为é¾å¼åæä½è¿åå
·ææå®Sleeperçå
é103func (el *Element) Sleeper(sleeper func() utils.Sleeper) *Element {104 newObj := *el105 newObj.sleeper = sleeper106 return &newObj...
extractor.go
Source:extractor.go
...10 "github.com/go-rod/rod"11 "github.com/go-rod/rod/lib/proto"12)13func extractTable(page *rod.Page, writer *csv.Writer) {14 elem := page.Timeout(5 * time.Second).MustElement(`#GirdTable2, #GirdTable`).CancelTimeout()15 s, _ := elem.Attribute("id")16 t, _ := elem.Text()17 lines := strings.Split(t, "\n")18 if *s == "GirdTable2" {19 lines = lines[7:]20 } else {21 lines = lines[9:]22 }23 for _, line := range lines {24 fields := strings.Split(line, "\t")25 for i := range fields {26 fields[i] = strings.Trim(fields[i], "Â ")27 }28 writer.Write(fields)29 }30 writer.Flush()31}32func extractTicker(ticker string, browser *rod.Browser) {33 fileName := fmt.Sprintf("dat/%v_data.csv", ticker)34 _, err := os.Stat(fileName)35 if err == nil {36 return37 }38 url := fmt.Sprintf("https://s.cafef.vn/Lich-su-giao-dich-%v-1.chn", ticker)39 page := browser.MustPage(url).Timeout(15 * time.Second).MustWaitLoad().CancelTimeout()40 defer page.Close()41 f, err := os.Create(fileName)42 if err != nil {43 log.Print(err)44 return45 }46 defer f.Close()47 writer := bufio.NewWriter(f)48 csvWriter := csv.NewWriter(writer)49 // header := [11]string{50 // "date",51 // "recalibrate",52 // "close",53 // "change",54 // "KLKL",55 // "GTKL",56 // "KLTT",57 // "GTTT",58 // "Open",59 // "Highest",60 // "Lowest",61 // }62 // csvWriter.Write(header[:])63 extractTable(page, csvWriter)64 for true {65 elem := page.Timeout(3 * time.Second).MustElement(`#ctl00_ContentPlaceHolder1_ctl03_panelAjax> div > div > div > table > tbody > tr > td:last-child `)66 icon, _ := elem.Text()67 // fmt.Println(icon)68 if icon != ">" {69 break70 }71 elem.MustClick()72 wait := page.Timeout(3 * time.Second).WaitEvent(proto.NetworkDataReceived{})73 wait()74 time.Sleep(200 * time.Millisecond)75 // fmt.Print(page.MustElement(`#ctl00_ContentPlaceHolder1_ctl03_divHO > div > div > table > tbody > tr > td:nth-last-child(2) > a`).Text())76 extractTable(page, csvWriter)77 }78 // page.MustWaitLoad().MustScreenshot(fmt.Sprintf("%v.png", ticker))79}80func extractTickerLatest(ticker string) {81 url := fmt.Sprintf("https://s.cafef.vn/Lich-su-giao-dich-%v-1.chn", ticker)82 page := rod.New().NoDefaultDevice().MustConnect().MustPage(url)83 err := rod.Try(func() {84 t, _ := page.Timeout(time.Second * 5).MustElement(`#ctl00_ContentPlaceHolder1_ctl03_rptData2_ctl01_itemTR`).CancelTimeout().Text()85 fmt.Println(t)86 })87 if err != nil {88 // fmt.Println(err)89 }90 // page.MustWaitLoad().MustScreenshot(fmt.Sprintf("%v.png", ticker))91}92func extractAll() {93 file := "company_stock.csv"94 f, _ := os.Open(file)95 defer f.Close()96 csv := csv.NewReader(f)97 csv.Read()98 companyList := make([]string, 0)...
CancelTimeout
Using AI Code Generation
1import (2func main() {3 title := page.Element("title").Text()4 fmt.Println(title)5 utils.Timeout(time.Second * 2).Do(func() {6 title := page.Element("title").Text()7 fmt.Println(title)8 })9 page.Timeout(time.Second * 2).Element("title").Text()10 page.Timeout(time.Second * 2).Eval(`() => {11 return new Promise(resolve => {12 setTimeout(() => {13 resolve(document.title)14 }, 1000)15 })16 }`)17 page.Timeout(time.Second * 2).Eval(`() => {18 return new Promise(resolve => {19 setTimeout(() => {20 resolve(document.title)21 }, 1000)22 })23 }`, nil)24 page.Timeout(time.Second * 2).Eval(`() => {25 return new Promise(resolve => {26 setTimeout(() => {27 resolve(document.title)28 }, 1000)29 })30 }`, nil, proto.RuntimeRemoteObject{})31 page.Timeout(time.Second * 2).Eval(`() => {32 return new Promise(resolve => {33 setTimeout(() => {34 resolve(document.title)35 }, 1000)36 })37 }`, nil, proto.RuntimeRemoteObject{}, proto.RuntimeRemoteObject{})38 page.Timeout(time.Second * 2).Eval(`() => {39 return new Promise(resolve => {40 setTimeout(() => {41 resolve(document.title)42 }, 1000)43 })44 }`, nil, proto.RuntimeRemoteObject{}, proto.RuntimeRemoteObject{}, proto.RuntimeRemoteObject{})45 page.Timeout(time.Second * 2).Eval(`() => {46 return new Promise(resolve => {
CancelTimeout
Using AI Code Generation
1import (2func main() {3 browser := rod.New().Connect()4 page.Element("input[name=q]").WaitVisible()5 val, _ := page.Element("input[name=q]").Property("value")6 fmt.Println(val)7 page.Element("input[name=q]").WaitVisible()8 val, _ = page.Element("input[name=q]").Property("value")9 fmt.Println(val)10 page.Element("input[name=q]").WaitVisible()11 val, _ = page.Element("input[name=q]").Property("value")12 fmt.Println(val)13 page.Element("input[name=q]").WaitVisible()14 val, _ = page.Element("input[name=q]").Property("value")15 fmt.Println(val)16 page.Element("input[name=q]").WaitVisible()17 val, _ = page.Element("input[name=q]").Property("value")18 fmt.Println(val)19 page.Element("input[name=q]").WaitVisible()20 val, _ = page.Element("input[name=q]").Property("value")21 fmt.Println(val)22 page.Element("input[name=q]").WaitVisible()23 val, _ = page.Element("input[name=q]").Property("value")24 fmt.Println(val)25 page.Element("input[name=q]").WaitVisible()26 val, _ = page.Element("input
CancelTimeout
Using AI Code Generation
1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4 defer browser.MustClose()5 ctx, cancel := page.Timeout(1 * time.Second)6 cancel()7 page.MustWaitRequestIdle()8 ctx, cancel = page.Timeout(1 * time.Second)9 _, err := page.WaitRequestIdle().Do(ctx)10 if err != nil {11 log.Println(err)12 }13}
CancelTimeout
Using AI Code Generation
1import (2func main() {3 browser := rod.New().MustConnect()4 page.MustWaitLoad()5 time.Sleep(3 * time.Second)6 page.MustCancelTimeout()7 time.Sleep(3 * time.Second)8 browser.MustClose()9 fmt.Println("Done")10}
CancelTimeout
Using AI Code Generation
1import "fmt"2import "time"3import "github.com/go-rod/rod"4func main() {5b := rod.New().MustConnect()6ctx, cancel := rod.NewTimeout(5 * time.Second)7defer cancel()8p.MustElement("input[name=q]", rod.ToBeVisible(), rod.FromContext(ctx))9p.MustElement("input[name=q]", rod.ToBeHidden(), rod.FromContext(ctx))10}11github.com/go-rod/rod.(*Browser).MustPage(0xc00008a000, 0x5d0f6a, 0x1c, 0x0, 0x0, 0x0)
CancelTimeout
Using AI Code Generation
1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4 defer browser.MustClose()5 ctx, cancel := page.Timeout(5 * time.Second)6 defer cancel()7 page.MustElement("title").MustWaitVisible()8 cancel()9 page.MustElement("title").MustWaitVisible()10 page.MustElement("title").MustWaitVisible()11 page.MustElement("title").MustWaitVisible()12 page.MustElement("title").MustWaitVisible()13 page.MustElement("title").MustWaitVisible()14 page.MustElement("title").MustWait
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!!