How to use MustElement method of rod Package

Best Rod code snippet using rod.MustElement

bili.go

Source:bili.go Github

copy

Full Screen

...25}26//等待用户登录27func (b *BiliHandle) WaitLogIn(){28 if b.IsLogin {29 b.B.MustPage("http://bilibili.com").MustElement(".bili-icon_dingdao_dengchu")30 }31}32//播放视频33func (b *BiliHandle) Play() {34 v := b.Page.MustElement(".video-card-reco a").MustAttribute("href")35 childPage := b.B.MustPage(fmt.Sprintf("http:%s" , *v))36 childPage.MustElement("#bilibiliPlayer").MustClick()37 childPage.MustElement(".bilibili-player-upinfo-span.restart")38 fmt.Print("vide play ending")39 childPage.Close()40 //b.MustPage()41}42//分享视频43func (b *BiliHandle) Share () {44}45//点赞视频46func (b *BiliHandle) Like() {47 48}49type Dynamic struct {50 Time string `json:"time"`51 Content string `json:"content"`52}53// 老大哥在看你54func (b *BiliHandle)BigBrotherIsWatchingYou(id string) {55 var dynamicArr []Dynamic56 var dynamicArrItme Dynamic57 scpage := b.B.MustPage(fmt.Sprintf("https://space.bilibili.com/%s/dynamic" ,id))58 for {59 scpage.Reload()60 dynamicArr = nil61 scpage.MustElement(".main-content")62 arr := scpage.MustElements(".main-content")63 for _ , v := range arr {64 dyTime := v.MustElement(".detail-link.tc-slate").MustText()65 content := v.MustElement(".card-content").MustText()66 dynamicItem := Dynamic{67 Time: dyTime,68 Content: content,69 }70 dynamicArr = append(dynamicArr , dynamicItem)71 }72 //程序第一次运行73 if(dynamicArrItme.Time == "" ){74 dynamicArrItme = dynamicArr[0]75 utils.WxSendMsg(string(`老大哥已经开始看着他啦 , 最后一条动态更新为` + dynamicArrItme.Time))76 fmt.Println("fir")77 }78 if dynamicArrItme.Time != dynamicArr[0].Time || dynamicArrItme.Content != dynamicArr[0].Content {79 fmt.Println("更新")80 utils.WxSendMsg(fmt.Sprintf("老大哥发现Ta的动态已经更新啦 !!! 时间为 : %s , 内容为 : %s " , dynamicArr[0].Time ,dynamicArr[0].Content))81 dynamicArrItme = dynamicArr[0]82 }83 rand.Seed(time.Now().UnixNano())84 time.Sleep(time.Second * 3 )85 }86}87//转发动态88func (b *BiliHandle)Forward() {89 90}91//获取我关注的列表92func (b *BiliHandle) GetFocus() {93 var rs []string94 spacePage := b.B.MustPage("http://space.bilibili.com")95 spacePage.MustElement(".n-statistics a").MustClick()96 spacePage.MustElement(".list-item")97 for{98 arr := spacePage.MustElements(".list-item")99 for _ , v := range arr {100 name , _ := v.MustElement(".fans-name").Text()101 rs = append(rs ,name)102 }103 if len(arr) == 20 {104 spacePage.MustElement(".be-pager-next").MustClick()105 time.Sleep(time.Second)106 }else {107 fmt.Println("ending...")108 spacePage.Close()109 break110 }111 }112 fmt.Println(rs)113 fmt.Printf( "一共关注人数为 %d" , len(rs))114}115//我的直播关注列表116func (b *BiliHandle) GetLiveFocus (){117 type LiveItme struct {118 Name string `json:"name"`119 Status string `json:"status"`120 }121 var rsString string122 cpage := b.B.MustPage("https://link.bilibili.com/p/center/index#/user-center/follow/1")123 for{124 var liveArr []LiveItme125 cpage.Reload()126 cpage.MustElement(".favourite-card")127 arr := cpage.MustElements(".favourite-card")128 for _ , v := range arr {129 liveArr = append(liveArr , LiveItme{130 Name: v.MustElement(".anchor-name").MustText(),131 Status: v.MustElement(".anchor-status").MustText(),132 })133 }134 rsByte , err := json.Marshal(liveArr)135 if err != nil {136 fmt.Println(err)137 }138 if rsString == "" {139 rsString = string(rsByte)140 utils.WxSendMsg(string(`直播列表正在监听` + rsString))141 fmt.Println("fir")142 }143 if rsString != string(rsString) {144 rsString = string(rsByte)145 utils.WxSendMsg(string(`直播列表已经更新` + rsString))146 }147 rand.Seed(time.Now().UnixNano())148 r := rand.Intn(5) + 5149 fmt.Println(r)150 time.Sleep(time.Minute * time.Duration(r))151 }152}153//直播项目签到154func LiveSignIn (page *rod.Page) {155 page.MustElementR("a" ,"直播").MustClick()156 fmt.Println("bark")157}...

Full Screen

Full Screen

main.go

Source:main.go Github

copy

Full Screen

...24 }25}26func (p *Page) Login() error {27 time.Sleep(200 * time.Millisecond)28 p.page.MustElement("input[id='Frm_Username']").MustInput(p.username)29 p.page.MustElement("input[id='Frm_Password']").MustInput(p.password)30 p.page.MustElement("input[id='LoginId']").MustClick()31 if exist, errmsg, err := p.page.Has("font[id='errmsg']"); exist || err != nil {32 return errors.New(errmsg.String())33 }34 return nil35}36func (p *Page) WANInfoPage() *rod.Page {37 network_info := p.page.MustElement("table.menu_table tr.h2_content")38 network_info.MustClick()39 return network_info.Page()40}41func (p *Page) GetIPAddress(ipAddress chan string) (string, error) {42 IPAddress := p.page.MustElement("input[id='TextPPPIPAddress0']").MustAttribute("value")43 ipAddress <- *IPAddress44 return *IPAddress, nil45}46func (p *Page) GetConnStatus(connStatus chan string) (string, error) {47 ConnStatus := p.page.MustElement("input[id='TextPPPConStatus0']").MustAttribute("value")48 connStatus <- *ConnStatus49 return *ConnStatus, nil50}51func (p *Page) Logout(signal chan bool) {52 if err := p.page.MustElement("div[class='title_log'] > a").Click(proto.InputMouseButtonLeft); err != nil {53 log.Println(err)54 }55 signal <- true56}57func main() {58 err := godotenv.Load()59 if err != nil {60 log.Panicln("There is no .env found in the directory, please add!")61 }62 username := os.Getenv("USERNAME")63 password := os.Getenv("PASSWORD")64 admin_url := os.Getenv("ADMIN_URL")65 launcher := launcher.New().Headless(true)66 defer launcher.Cleanup()67 url := launcher.MustLaunch()68 browser := rod.New().ControlURL(url).Trace(true).MustConnect()69 defer browser.MustClose()70 page := NewPage(browser.MustPage(admin_url), admin_url, username, password)71 err = page.Login()72 if err != nil {73 log.Fatalln(err)74 }75 bodyPage := NewPage(page.page.MustElement("iframe[src='template.gch']").MustFrame(), admin_url, username, password)76 connInfoPage := NewPage(bodyPage.WANInfoPage(), admin_url, username, password)77 IPAddress := make(chan string, 1)78 ConnStatus := make(chan string, 1)79 go func() {80 _, err = connInfoPage.GetIPAddress(IPAddress)81 if err != nil {82 log.Fatalln(err)83 }84 _, err = connInfoPage.GetConnStatus(ConnStatus)85 if err != nil {86 log.Fatalln(err)87 }88 }()89 log.Println("IP Address: ", <-IPAddress)...

Full Screen

Full Screen

MustElement

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4 page.MustElement("input[name=q]").MustInput("rod").MustPress("Enter")5 page.MustElementR("h3", "rod").MustClick()6 fmt.Println(page.MustInfo().URL)7}8import (9func main() {10 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()11 page.MustElementR("h3", "rod").MustClick()12 fmt.Println(page.MustInfo().URL)13}14import (15func main() {16 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()17 page.MustElement("input[name=q]").MustInput("rod").MustPress("Enter")18 page.MustElementR("h3", "rod").MustClick()19 fmt.Println(page.MustInfo().URL)20}21import (

Full Screen

Full Screen

MustElement

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().MustConnect()4 page.MustElement("#lst-ib").MustInput("rod")5 page.MustElement("#tsf > div.tsf-p > div.jsb > center > input[type=\"submit\"]:nth-child(1)").MustClick()6 stats := page.MustElement("#resultStats").MustText()7 fmt.Println(stats)8 title := page.MustElement("#rso > div:nth-child(1) > div > div > div > div > h3 > a").MustText()9 fmt.Println(title)10 url := page.MustElement("#rso > div:nth-child(1) > div > div > div > div > h3 > a").MustProperty("href")11 fmt.Println(url)12 page.MustElement("#rso > div:nth-child(1) > div > div > div > div > h3 > a").MustClick()13 title = page.MustElement("head > title").MustText()14 fmt.Println(title)15 url = page.MustElement("head > title").MustProperty("href")16 fmt.Println(url)17 body := page.MustElement("body").MustHTML()18 fmt.Println(body)19 text := page.MustElement("body").MustText()20 fmt.Println(text)21 input := page.MustElement("#lst-ib")22 input.MustInput("rod")23 value := input.MustProperty("value")24 fmt.Println(value)25 textarea := page.MustElement("#textarea")26 textarea.MustInput("rod")27 value = textarea.MustProperty("value")28 fmt.Println(value)29 checkbox := page.MustElement("#checkbox")30 checkbox.MustInput("rod")31 value = checkbox.MustProperty("value")32 fmt.Println(value)

Full Screen

Full Screen

MustElement

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 l := launcher.New().MustLaunch()4 defer l.Close()5 b := rod.New().ControlURL(l).MustConnect()6 defer b.Close()7 page := b.MustPage("")8 defer page.Close()9 search := page.MustElement("input[name=q]")10 search.MustInput("rod library")11 search.MustPress("Enter")12 fmt.Println(page.MustElement("h3").MustText())13}14import (15func main() {16 l := launcher.New().MustLaunch()17 defer l.Close()18 b := rod.New().ControlURL(l).MustConnect()19 defer b.Close()20 page := b.MustPage("")21 defer page.Close()22 search := page.MustElementR("input[name=q]")23 search.MustInput("rod library")24 search.MustPress("Enter")25 fmt.Println(page.MustElementR("h3").MustText())26}27import (28func main() {29 l := launcher.New().MustLaunch()30 defer l.Close()31 b := rod.New().ControlURL(l).MustConnect()32 defer b.Close()33 page := b.MustPage("")34 defer page.Close()35 search.MustInput("rod library")36 search.MustPress("Enter")37}38import (39func main() {

Full Screen

Full Screen

MustElement

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().MustConnect()4 page.MustElement(`input[name="q"]`).MustInput("rod").MustPress(input.Enter)5 page.MustWaitLoad()6 page.MustElement(`#search`).MustElement(`.g`).MustElement("a").MustHandle(func(e *rod.Element) {7 log.Println(e.MustTe

Full Screen

Full Screen

MustElement

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4 page := browser.MustPage("")5 searchBox := page.MustElement("#lst-ib")6 searchBox.MustInput("Hello World")7 searchButton := page.MustElement("input[name='btnK']")8 searchButton.MustClick()9}10import (11func main() {12 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()13 page := browser.MustPage("")14 searchBox := page.Element("#lst-ib")15 searchBox.MustInput("Hello World")16 searchButton := page.Element("input[name='btnK']")17 searchButton.MustClick()18}19import (20func main() {21 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()22 page := browser.MustPage("")23 searchBox := page.Elements("#lst-ib")24 searchBox[0].MustInput("Hello World")25 searchButton := page.Elements("input[name='btnK']")

Full Screen

Full Screen

MustElement

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().MustConnect()4 page.MustElement("input[name='q']").MustInput("rod").MustPress("Enter")5}6import (7func main() {8 browser := rod.New().Connect()9 page.Element("input[name='q']").Input("rod").Press("Enter")10}11import (12func main() {13 browser := rod.New().MustConnect()14}15import (16func main() {17 browser := rod.New().Connect()18}19import (20func main() {21 browser := rod.New().MustConnect()22 page.MustElementR("input[name='q']").MustInput("rod").MustPress("Enter")23}24import (25func main() {26 browser := rod.New().Connect()27 page.ElementR("input[name='q']").Input("rod").Press("Enter")28}29import (30func main()

Full Screen

Full Screen

MustElement

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().MustConnect()4 el := page.MustElement("#hplogo")5 src, _ := el.Attr("src")6 fmt.Println(src)7}8import (9func main() {10 browser := rod.New().MustConnect()11 el, _ := page.Element("#hplogo")12 src, _ := el.Attr("src")13 fmt.Println(src)14}15import (16func main() {17 browser := rod.New().MustConnect()18 el := page.MustElementR("#hplogo")19 src, _ := el.Attr("src")

Full Screen

Full Screen

MustElement

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4 page.MustElement("#lst-ib").MustInput("Rod")5 page.MustElement(".lsb").MustClick()6}

Full Screen

Full Screen

MustElement

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4 input := page.MustElement("#lst-ib")5 input.MustInput("rod")6 page.MustElement("#tsf > div:nth-child(2) > div > div.FPdoLc.VlcLAe > center > input[type=\"submit\"]:nth-child(1)").MustClick()7 results := page.MustElement(".srg")8 fmt.Println(results.MustText())9}10import (11func main() {12 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()13 input := page.MustElementR("lst-ib", "id")14 input.MustInput("rod")15 page.MustElementR("tsf > div:nth-child(2) > div > div.FPdoLc.VlcLAe > center > input[type=\"submit\"]:nth-child(1)", "css").MustClick()16 results := page.MustElement(".srg")17 fmt.Println(results.MustText())18}19import (20func main() {21 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()

Full Screen

Full Screen

MustElement

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().MustConnect()4 page.MustElement("#lst-ib").MustInput("rod").MustPress(rod.Enter)5 elem := page.MustElement("h3")6 fmt.Println(elem.MustText())7}

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