Best Rod code snippet using rod.MustElementX
scrape.go
Source:scrape.go
...46 s.Page = browser.MustPage(url).MustWaitLoad()47 }48}49func (s *Scraper) Login(cred LoginInfo) error {50 usernameInput := s.Page.MustElementX(cred.UsernameInputPath)51 passwordInput := s.Page.MustElementX(cred.PasswordInputPath)52 buttonInput := s.Page.MustElementX(cred.ButtonPath)53 usernameInput.MustWaitInteractable().MustInput(cred.Username)54 passwordInput.MustWaitInteractable().MustInput(cred.Password)55 buttonInput.MustWaitInteractable().MustClick()56 s.Page.MustWaitNavigation()()57 cookies := s.Page.MustCookies()58 var filteredCookies []*proto.NetworkCookie59 for _, cookie := range cookies {60 if cookie.Name == "GETAFREE_AUTH_HASH_V2" {61 filteredCookies = append(filteredCookies, cookie)62 } else if cookie.Name == "GETAFREE_USER_ID" {63 filteredCookies = append(filteredCookies, cookie)64 }65 }66 if len(filteredCookies) < 2 {67 return errors.New("username or password is incorrect or check if your XPath is correct in the settings")68 }69 bytes, err := json.Marshal(proto.CookiesToParams(cookies))70 CheckError(err)71 file, err := os.Create("./login_cookie.json")72 CheckError(err)73 count, err := file.Write(bytes)74 CheckError(err)75 fmt.Println("wrote ", count)76 return nil77}78func (s *Scraper) GetProjects(pfunc func([]*pb.Projects)) func() error {79 // s.Page.MustWaitNavigation()()80 s.Page.WaitLoad()81 var projects []*pb.Projects82 stop, _ := s.Page.Expose("GetProjectList", func(v gson.JSON) (interface{}, error) {83 projectArr := v.Arr()84 for _, p := range projectArr {85 var project pb.Projects86 project.BiddingPrice = p.Get("biddingPrice").String()87 project.Description = p.Get("description").String()88 project.Currency = p.Get("currency").String()89 project.Link = p.Get("link").String()90 project.Selected = p.Get("selected").Bool()91 project.Skills = p.Get("skills").String()92 project.Title = p.Get("title").String()93 projects = append(projects, &project)94 }95 pfunc(projects)96 return nil, nil97 })98 // file, _ := os.Open("./evalProject.js")99 // bytes, _ := ioutil.ReadAll(file)100 _, err := s.Page.Evaluate(&rod.EvalOptions{101 JS: evalProjectFile,102 })103 CheckError(err)104 return stop105}106func (s *Scraper) SubscribeToProject(pfunc func(*pb.Projects)) {107 s.Page.MustWaitNavigation()()108 s.Page.MustWaitLoad()109 s.Page.Expose("GetProject", func(v gson.JSON) (interface{}, error) {110 var project pb.Projects111 project.BiddingPrice = v.Get("biddingPrice").String()112 project.Description = v.Get("description").String()113 project.Currency = v.Get("currency").String()114 project.Link = v.Get("link").String()115 project.Selected = v.Get("selected").Bool()116 project.Skills = v.Get("skills").String()117 project.Title = v.Get("title").String()118 pfunc(&project)119 return nil, nil120 })121 // file, _ := os.Open("./eval.js")122 // bytes, _ := ioutil.ReadAll(file)123 _, err := s.Page.Evaluate(&rod.EvalOptions{124 JS: evalFile,125 })126 CheckError(err)127}128func (s *Scraper) BidOnProject(url string, bidInfo BidInfo) bool {129 page := s.Browser.MustPage(url)130 page.WaitLoad()131 if bidInfo.BidAmount != "" {132 bidAmountInput := page.MustElementX(bidInfo.BidAmountPath)133 bidAmountInput.MustWaitInteractable().MustInput(bidInfo.BidAmount)134 }135 if bidInfo.ProjectDelivery != "" {136 projectDeliveryInput := page.MustElementX(bidInfo.ProjectDeliveryPath)137 projectDeliveryInput.MustWaitInteractable().MustInput(bidInfo.ProjectDelivery)138 }139 projectDescriptionInput := page.MustElementX(bidInfo.ProjectDescriptionPath)140 projectDescriptionInput.MustWaitInteractable().MustInput(bidInfo.ProjectDescription)141 bidButton := page.MustElementX(bidInfo.BidButtonPath)142 bidButton.MustWaitInteractable().MustClick()143 pageURL := page.MustInfo().URL144 fmt.Println(pageURL)145 defer page.MustClose()146 return strings.Contains(pageURL, "details")147}...
instagram.go
Source:instagram.go
...24 go user.watchBrowser(page, end_debug)25 }26 time.Sleep(time.Second * 3)27 // username input28 page.MustElementX("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input").MustInput(user.Username)29 // password input30 page.MustElementX("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[2]/div/label/input").MustInput(user.Password)31 // login button32 page.MustElementX("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[3]/button").MustClick()33 // This section makes the first login to the account and reports if the password is wrong34 time.Sleep(time.Second * 7)35 _, err := page.Timeout(time.Second * 5).ElementX("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[1]/div[1]/div/label/input")36 if err == nil {37 page.Close()38 log.Println("Username or password incorrect")39 return40 }41 // This section makes the first login to the account and reports if the password is wrong42 page.MustWaitLoad()43 log.Println("Wait 3 second for get cookies successful.")44 time.Sleep(time.Second * 3)45 f, err := os.Create("account_data/instagram/" + user.Username + "/cookie.dat")46 if err != nil {47 log.Fatal("We got an error at creating cookie data file\n", err)48 }49 // Write cookies into file50 for _, v := range page.MustCookies() {51 _, err2 := f.WriteString(v.Name + "#" + v.Value + "#" + v.Domain + "\n")52 if err2 != nil {53 log.Fatal("Cookies doesn't writed on file\n", err2)54 }55 }56 log.Println("Cookies saved.")57 f.Close()58 if watch_instagram {59 end_debug <- true // for stop saving screenshots60 }61 page.Close()62 log.Println("Login successful and cookies registered")63 user.cookieInstagramRun()64}65func (user *AccountInfo) cookieInstagramRun() {66 log.Println("Found cookie for ", user.Username)67 // first section68 page := rod.New().SlowMotion(time.Second * 2).MustConnect().MustPage("about:blank")69 page.MustEmulate(rod_device)70 file, err := os.Open("account_data/instagram/" + user.Username + "/cookie.dat")71 if err != nil {72 log.Fatalf(user.Username, user.Pname, " cookie read error: %s", err)73 }74 scanner := bufio.NewScanner(file)75 scanner.Split(bufio.ScanLines)76 for scanner.Scan() {77 saved_dat := strings.Split(scanner.Text(), "#")78 page.MustSetCookies([]*proto.NetworkCookieParam{{79 Name: saved_dat[0],80 Value: saved_dat[1],81 Domain: saved_dat[2],82 }}...)83 }84 file.Close()85 log.Println("Cookies loaded")86 page.MustNavigate("https://instagram.com").MustWaitLoad()87 // For debugging88 end_debug := make(chan bool)89 if watch_instagram {90 go user.watchBrowser(page, end_debug)91 }92 time.Sleep(time.Second * 5)93 // Pop-up control94 go func(page *rod.Page) {95 page.MustElementX("/html/body/div[5]/div/div/div/div[3]/button[2]").MustClick()96 }(page)97 // Add post button98 page.MustElementX("/html/body/div[1]/section/nav/div[2]/div/div/div[3]/div/div[3]/div/button").MustClick()99 // Set files100 page.MustElementX("/html/body/div[8]/div[2]/div/div/div/div[2]/div[1]/form/input").MustSetFiles(user.FilePath)101 // Next button102 page.MustElementX("/html/body/div[6]/div[2]/div/div/div/div[1]/div/div/div[2]/div/button").MustClick()103 // Next button104 page.MustElementX("/html/body/div[6]/div[2]/div/div/div/div[1]/div/div/div[2]/div/button").MustClick()105 // Write a caption106 page.MustElementX("/html/body/div[6]/div[2]/div/div/div/div[2]/div[2]/div/div/div/div[2]/div[1]/textarea").MustInput(user.Caption)107 // Share button108 page.MustElementX("/html/body/div[6]/div[2]/div/div/div/div[1]/div/div/div[2]/div/button").MustClick()109 log.Println("All process are done, waiting 15 seconds")110 time.Sleep(time.Second * 15)111 if watch_instagram {112 end_debug <- true // for stop saving screenshots113 }114 log.Println("Everything ok, shutting down browser.")115 page.Close()116 log.Println("Login with Cookie and share successfully")117}118func (user *AccountInfo) shareInstagram(debug bool) {119 _, err := os.ReadFile("account_data/instagram/" + user.Username + "/cookie.dat")120 watch_instagram = debug121 if err != nil {122 user.firstInstagramRun()...
bot.go
Source:bot.go
...25 })26 page := browser.MustPage("https://shopee.co.id/BOLD%C3%AB-Set-Wajan-Super-Pan-Beige-i.72109688.1207801918")27 page.MustWaitLoad()28 fmt.Println(time.Now().Format("15:04:05") + " : " + "page loaded..")29 // variant := page.MustElementX("//button[contains(text(),'earphone putih')]")30 // variant.MustClick()31 wait := page.MustWaitRequestIdle()32 buy := page.MustElement("#main > div > div._193wCc > div.page-product.page-product--mall > div > div.product-briefing.flex.card.zINA0e > div.flex.flex-auto._3-GQHh > div > div:nth-child(5) > div > div > button.btn.btn-solid-primary.btn--l._3Kiuzg")33 buy.MustClick()34 wait()35 fmt.Println(time.Now().Format("15:04:05") + " : " + "barang di beli..")36 wait = page.MustWaitRequestIdle()37 checkout := page.MustElementX("//*[@id=\"main\"]/div/div[2]/div[2]/div/div[3]/div[2]/div[7]/button[4]/span")38 checkout.MustClick()39 wait()40 page.MustWaitIdle()41 page.MustWaitLoad()42 fmt.Println(time.Now().Format("15:04:05") + " : " + "barang di checkout..")43 wait = page.MustWaitRequestIdle()44 pesanan := page.MustElementX("//*[@id=\"main\"]/div/div[3]/div[2]/div[4]/div[2]/div[9]/button")45 pesanan.MustClick()46 wait()47 page.MustWaitLoad()48 fmt.Println(time.Now().Format("15:04:05") + " : " + "barang di buatkan pesanan..")49 wait = page.MustWaitRequestIdle()50 bayar := page.MustElementX("//*[@id=\"pay-button\"]")51 bayar.MustClick()52 wait()53 page.MustWaitLoad()54 fmt.Println(time.Now().Format("15:04:05") + " : " + "barang di bayar..")55 page.MustElementX("//*[@id=\"pin-popup\"]/div[1]/div[3]/div[1]")56 page.Keyboard.MustPress('')57 page.Keyboard.MustPress('')58 page.Keyboard.MustPress('')59 page.Keyboard.MustPress('')60 page.Keyboard.MustPress('')61 page.Keyboard.MustPress('')62 wait = page.MustWaitRequestIdle()63 confirmPin := page.MustElementX("//*[@id=\"pin-popup\"]/div[1]/div[4]/div[2]")64 confirmPin.MustClick()65 wait()66 fmt.Println("sippp")67}...
MustElementX
Using AI Code Generation
1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4}5import (6func main() {7 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()8}9import (10func main() {11 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()12}13import (14func main() {15 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()16}17import (
MustElementX
Using AI Code Generation
1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4 page.MustScreenshot("screenshot.png")5}6import (7func main() {8 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()9 page.MustScreenshot("screenshot.png")10}11import (12func main() {13 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()14 page.MustScreenshot("screenshot.png")15}16import (17func main() {18 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()19 page.MustScreenshot("screenshot.png")20}21import (
MustElementX
Using AI Code Generation
1import (2func main() {3 browser := rod.New().MustConnect()4}5import (6func main() {7 browser := rod.New().MustConnect()8}9import (10func main() {11 browser := rod.New().MustConnect()12}13import (14func main() {15 browser := rod.New().MustConnect()16}17import (18func main() {19 browser := rod.New().MustConnect()
MustElementX
Using AI Code Generation
1import (2func main() {3}4import (5func main() {6}7import (8func main() {9}10import (11func main() {12}13import (14func main() {15}16import (17func main() {18}19import (20func main() {21}22import (
MustElementX
Using AI Code Generation
1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4 input.MustInput("Rod")5 input.MustPress(rod.Enter)6 page.MustWaitLoad()7 result := page.MustElement(`#search .g`)8 title := result.MustElement(`h3`).MustText()9 fmt.Println(title)10 if err := browser.Close(); err != nil {11 log.Fatal(err)12 }13}
MustElementX
Using AI Code Generation
1import (2func main() {3 l := launcher.New().MustLaunch()4 defer l.Close()5 browser := rod.New().ControlURL(l).MustConnect()6 page := browser.MustPage("")7 page.MustElement("input[name=q]").MustInput("rod").MustSubmit()8 page.MustElement("h3").MustText()9}10import (11func main() {12 l := launcher.New().MustLaunch()13 defer l.Close()14 browser := rod.New().ControlURL(l).MustConnect()15 page := browser.MustPage("")16 page.MustElement("input[name=q]").MustInput("rod").MustSubmit()17 page.MustElement("h3").MustText()18}19import (
MustElementX
Using AI Code Generation
1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4 defer browser.MustClose()5 page := browser.MustPage("")6 page.MustElement("input[name=q]").MustInput("rod").MustPress(input.Enter)7 page.MustWaitLoad()8 el := page.MustElement("#search .g")9 log.Println(el.MustText(), el.MustProperty("href").String())10}
MustElementX
Using AI Code Generation
1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4 defer browser.MustClose()5 page.MustElementX("/html/body/div[2]/div[2]/form/div[2]/div[1]/div[1]/div/div[2]/input").MustInput("rod")6 page.MustElementX("/html/body/div[2]/div[2]/form/div[2]/div[1]/div[3]/center/input[1]").MustClick()7}8import (9func main() {10 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()11 defer browser.MustClose()12 page.MustElementX("/html/body/div[2]/div[2]/form/div[2]/div[1]/div[1]/div/div[2]/input").MustInput("rod")13 page.MustElementX("/html
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!!