How to use Elements method of rod Package

Best Rod code snippet using rod.Elements

query_test.go

Source:query_test.go Github

copy

Full Screen

...10 "github.com/go-rod/rod/lib/proto"11 "github.com/go-rod/rod/lib/utils"12 "github.com/ysmood/gson"13)14func TestPageElements(t *testing.T) {15 g := setup(t)16 g.page.MustNavigate(g.srcFile("fixtures/input.html"))17 g.page.MustElement("input")18 list := g.page.MustElements("input")19 g.Eq("input", list.First().MustDescribe().LocalName)20 g.Eq("submit", list.Last().MustText())21}22func TestPagesQuery(t *testing.T) {23 g := setup(t)24 b := g.browser25 b.MustPage(g.srcFile("fixtures/click.html")).MustWaitLoad()26 pages := b.MustPages()27 g.True(pages.MustFind("button").MustHas("button"))28 g.Panic(func() { rod.Pages{}.MustFind("____") })29 g.True(pages.MustFindByURL("click.html").MustHas("button"))30 g.Panic(func() { rod.Pages{}.MustFindByURL("____") })31 _, err := pages.Find("____")32 g.Err(err)33 g.Eq(err.Error(), "cannot find page")34 g.Panic(func() {35 pages.MustFindByURL("____")36 })37 g.Panic(func() {38 g.mc.stubErr(1, proto.RuntimeCallFunctionOn{})39 pages.MustFind("button")40 })41 g.Panic(func() {42 g.mc.stubErr(1, proto.RuntimeCallFunctionOn{})43 pages.MustFindByURL("____")44 })45}46func TestPagesOthers(t *testing.T) {47 g := setup(t)48 list := rod.Pages{}49 g.Nil(list.First())50 g.Nil(list.Last())51 list = append(list, &rod.Page{})52 g.NotNil(list.First())53 g.NotNil(list.Last())54}55func TestPageHas(t *testing.T) {56 g := setup(t)57 g.page.MustNavigate(g.srcFile("fixtures/selector.html"))58 g.page.MustElement("body")59 g.True(g.page.MustHas("span"))60 g.False(g.page.MustHas("a"))61 g.True(g.page.MustHasX("//span"))62 g.False(g.page.MustHasX("//a"))63 g.True(g.page.MustHasR("button", "03"))64 g.False(g.page.MustHasR("button", "11"))65 g.mc.stubErr(1, proto.RuntimeCallFunctionOn{})66 g.Err(g.page.HasX("//a"))67 g.mc.stubErr(1, proto.RuntimeCallFunctionOn{})68 g.Err(g.page.HasR("button", "03"))69}70func TestElementHas(t *testing.T) {71 g := setup(t)72 g.page.MustNavigate(g.srcFile("fixtures/selector.html"))73 b := g.page.MustElement("body")74 g.True(b.MustHas("span"))75 g.False(b.MustHas("a"))76 g.True(b.MustHasX("//span"))77 g.False(b.MustHasX("//a"))78 g.True(b.MustHasR("button", "03"))79 g.False(b.MustHasR("button", "11"))80}81func TestSearch(t *testing.T) {82 g := setup(t)83 p := g.page.MustNavigate(g.srcFile("fixtures/click.html"))84 el := p.MustSearch("click me")85 g.Eq("click me", el.MustText())86 g.True(el.MustClick().MustMatches("[a=ok]"))87 _, err := p.Sleeper(rod.NotFoundSleeper).Search("not-exists")88 g.True(errors.Is(err, &rod.ErrElementNotFound{}))89 g.Eq(err.Error(), "cannot find element")90 // when search result is not ready91 {92 g.mc.stub(1, proto.DOMGetSearchResults{}, func(send StubSend) (gson.JSON, error) {93 return gson.New(nil), cdp.ErrCtxNotFound94 })95 p.MustSearch("click me")96 }97 // when node id is zero98 {99 g.mc.stub(1, proto.DOMGetSearchResults{}, func(send StubSend) (gson.JSON, error) {100 return gson.New(proto.DOMGetSearchResultsResult{101 NodeIds: []proto.DOMNodeID{0},102 }), nil103 })104 p.MustSearch("click me")105 }106 g.Panic(func() {107 g.mc.stubErr(1, proto.DOMPerformSearch{})108 p.MustSearch("click me")109 })110 g.Panic(func() {111 g.mc.stubErr(1, proto.DOMGetSearchResults{})112 p.MustSearch("click me")113 })114 g.Panic(func() {115 g.mc.stubErr(1, proto.RuntimeCallFunctionOn{})116 p.MustSearch("click me")117 })118}119func TestSearchElements(t *testing.T) {120 g := setup(t)121 p := g.page.MustNavigate(g.srcFile("fixtures/selector.html"))122 {123 res, err := p.Search("button")124 g.E(err)125 c, err := res.All()126 g.E(err)127 g.Len(c, 4)128 g.mc.stubErr(1, proto.DOMGetSearchResults{})129 g.Err(res.All())130 g.mc.stubErr(1, proto.DOMResolveNode{})131 g.Err(res.All())132 }133 { // disable retry134 sleeper := func() utils.Sleeper { return utils.CountSleeper(1) }135 _, err := p.Sleeper(sleeper).Search("not-exists")136 g.Err(err)137 }138}139func TestSearchIframes(t *testing.T) {140 g := setup(t)141 p := g.page.MustNavigate(g.srcFile("fixtures/click-iframes.html"))142 el := p.MustSearch("button[onclick]")143 g.Eq("click me", el.MustText())144 g.True(el.MustClick().MustMatches("[a=ok]"))145}146func TestSearchIframesAfterReload(t *testing.T) {147 g := setup(t)148 p := g.page.MustNavigate(g.srcFile("fixtures/click-iframes.html"))149 frame := p.MustElement("iframe").MustFrame().MustElement("iframe").MustFrame()150 frame.MustReload()151 el := p.MustSearch("button[onclick]")152 g.Eq("click me", el.MustText())153 g.True(el.MustClick().MustMatches("[a=ok]"))154}155func TestPageRace(t *testing.T) {156 g := setup(t)157 p := g.page.MustNavigate(g.srcFile("fixtures/selector.html"))158 p.Race().Element("button").MustHandle(func(e *rod.Element) { g.Eq("01", e.MustText()) }).MustDo()159 g.Eq("01", p.Race().Element("button").MustDo().MustText())160 p.Race().ElementX("//button").MustHandle(func(e *rod.Element) { g.Eq("01", e.MustText()) }).MustDo()161 g.Eq("01", p.Race().ElementX("//button").MustDo().MustText())162 p.Race().ElementR("button", "02").MustHandle(func(e *rod.Element) { g.Eq("02", e.MustText()) }).MustDo()163 g.Eq("02", p.Race().ElementR("button", "02").MustDo().MustText())164 p.Race().MustElementByJS("() => document.querySelector('button')", nil).165 MustHandle(func(e *rod.Element) { g.Eq("01", e.MustText()) }).MustDo()166 g.Eq("01", p.Race().MustElementByJS("() => document.querySelector('button')", nil).MustDo().MustText())167 raceFunc := func(p *rod.Page) (*rod.Element, error) {168 el := p.MustElement("button")169 g.Eq("01", el.MustText())170 return el, nil171 }172 p.Race().ElementFunc(raceFunc).MustHandle(func(e *rod.Element) { g.Eq("01", e.MustText()) }).MustDo()173 g.Eq("01", p.Race().ElementFunc(raceFunc).MustDo().MustText())174 el, err := p.Sleeper(func() utils.Sleeper { return utils.CountSleeper(2) }).Race().175 Element("not-exists").MustHandle(func(e *rod.Element) {}).176 ElementX("//not-exists").177 ElementR("not-exists", "test").MustHandle(func(e *rod.Element) {}).178 Do()179 g.Err(err)180 g.Nil(el)181 el, err = p.Race().MustElementByJS(`() => notExists()`, nil).Do()182 g.Err(err)183 g.Nil(el)184}185func TestPageRaceRetryInHandle(t *testing.T) {186 g := setup(t)187 p := g.page.MustNavigate(g.srcFile("fixtures/selector.html"))188 p.Race().Element("div").MustHandle(func(e *rod.Element) {189 go func() {190 utils.Sleep(0.5)191 e.MustElement("button").MustEval(`() => this.innerText = '04'`)192 }()193 e.MustElement("button").MustWait("() => this.innerText === '04'")194 }).MustDo()195}196func TestPageElementX(t *testing.T) {197 g := setup(t)198 g.page.MustNavigate(g.srcFile("fixtures/selector.html"))199 g.page.MustElement("body")200 txt := g.page.MustElementX("//div").MustElementX("./button").MustText()201 g.Eq(txt, "02")202}203func TestPageElementsX(t *testing.T) {204 g := setup(t)205 g.page.MustNavigate(g.srcFile("fixtures/selector.html"))206 g.page.MustElement("body")207 list := g.page.MustElementsX("//button")208 g.Len(list, 4)209}210func TestElementR(t *testing.T) {211 g := setup(t)212 p := g.page.MustNavigate(g.srcFile("fixtures/selector.html"))213 el := p.MustElementR("button", `\d1`)214 g.Eq("01", el.MustText())215 el = p.MustElement("div").MustElementR("button", `03`)216 g.Eq("03", el.MustText())217 p = g.page.MustNavigate(g.srcFile("fixtures/input.html"))218 el = p.MustElementR("input", `submit`)219 g.Eq("submit", el.MustText())220 el = p.MustElementR("input", `placeholder`)221 g.Eq("blur", *el.MustAttribute("id"))222 el = p.MustElementR("option", `/cc/i`)223 g.Eq("CC", el.MustText())224}225func TestElementFromElement(t *testing.T) {226 g := setup(t)227 p := g.page.MustNavigate(g.srcFile("fixtures/selector.html"))228 el := p.MustElement("div").MustElement("button")229 g.Eq("02", el.MustText())230}231func TestElementsFromElement(t *testing.T) {232 g := setup(t)233 p := g.page.MustNavigate(g.srcFile("fixtures/input.html"))234 el := p.MustElement("form")235 list := p.MustElement("form").MustElements("option")236 g.Len(list, 4)237 g.Eq("B", list[1].MustText())238 g.mc.stubErr(1, proto.RuntimeCallFunctionOn{})239 g.Err(el.Elements("input"))240}241func TestElementParent(t *testing.T) {242 g := setup(t)243 p := g.page.MustNavigate(g.srcFile("fixtures/input.html"))244 el := p.MustElement("input").MustParent()245 g.Eq("FORM", el.MustEval(`() => this.tagName`).String())246}247func TestElementParents(t *testing.T) {248 g := setup(t)249 p := g.page.MustNavigate(g.srcFile("fixtures/input.html"))250 g.Len(p.MustElement("option").MustParents("*"), 4)251 g.Len(p.MustElement("option").MustParents("form"), 1)252}253func TestElementSiblings(t *testing.T) {254 g := setup(t)255 p := g.page.MustNavigate(g.srcFile("fixtures/selector.html"))256 el := p.MustElement("div")257 a := el.MustPrevious()258 b := el.MustNext()259 g.Eq(a.MustText(), "01")260 g.Eq(b.MustText(), "04")261}262func TestElementFromElementX(t *testing.T) {263 g := setup(t)264 p := g.page.MustNavigate(g.srcFile("fixtures/selector.html"))265 el := p.MustElement("div").MustElementX("./button")266 g.Eq("02", el.MustText())267}268func TestElementsFromElementsX(t *testing.T) {269 g := setup(t)270 p := g.page.MustNavigate(g.srcFile("fixtures/selector.html"))271 list := p.MustElement("div").MustElementsX("./button")272 g.Len(list, 2)273}274func TestElementTracing(t *testing.T) {275 g := setup(t)276 g.browser.Trace(true)277 g.browser.Logger(utils.LoggerQuiet)278 defer func() {279 g.browser.Trace(defaults.Trace)280 g.browser.Logger(rod.DefaultLogger)281 }()282 p := g.page.MustNavigate(g.srcFile("fixtures/click.html"))283 g.Eq(`rod.element("code") html`, p.MustElement("html").MustElement("code").MustText())284}285func TestPageElementByJS(t *testing.T) {286 g := setup(t)287 p := g.page.MustNavigate(g.srcFile("fixtures/click.html"))288 g.Eq(p.MustElementByJS(`() => document.querySelector('button')`).MustText(), "click me")289 _, err := p.ElementByJS(rod.Eval(`() => 1`))290 g.Is(err, &rod.ErrExpectElement{})291 g.Eq(err.Error(), "expect js to return an element, but got: {\"type\":\"number\",\"value\":1,\"description\":\"1\"}")292}293func TestPageElementsByJS(t *testing.T) {294 g := setup(t)295 p := g.page.MustNavigate(g.srcFile("fixtures/selector.html")).MustWaitLoad()296 g.Len(p.MustElementsByJS("() => document.querySelectorAll('button')"), 4)297 _, err := p.ElementsByJS(rod.Eval(`() => [1]`))298 g.Is(err, &rod.ErrExpectElements{})299 g.Eq(err.Error(), "expect js to return an array of elements, but got: {\"type\":\"number\",\"value\":1,\"description\":\"1\"}")300 _, err = p.ElementsByJS(rod.Eval(`() => 1`))301 g.Eq(err.Error(), "expect js to return an array of elements, but got: {\"type\":\"number\",\"value\":1,\"description\":\"1\"}")302 _, err = p.ElementsByJS(rod.Eval(`() => foo()`))303 g.Err(err)304 g.mc.stubErr(1, proto.RuntimeGetProperties{})305 _, err = p.ElementsByJS(rod.Eval(`() => [document.body]`))306 g.Err(err)307 g.mc.stubErr(4, proto.RuntimeCallFunctionOn{})308 g.Err(p.Elements("button"))309}310func TestPageElementTimeout(t *testing.T) {311 g := setup(t)312 page := g.page.MustNavigate(g.blank())313 start := time.Now()314 _, err := page.Timeout(300 * time.Millisecond).Element("not-exists")315 g.Is(err, context.DeadlineExceeded)316 g.Gte(time.Since(start), 300*time.Millisecond)317}318func TestPageElementMaxRetry(t *testing.T) {319 g := setup(t)320 page := g.page.MustNavigate(g.blank())321 s := func() utils.Sleeper { return utils.CountSleeper(5) }322 _, err := page.Sleeper(s).Element("not-exists")323 g.Is(err, &utils.ErrMaxSleepCount{})324}325func TestElementsOthers(t *testing.T) {326 g := setup(t)327 list := rod.Elements{}328 g.Nil(list.First())329 g.Nil(list.Last())330}...

Full Screen

Full Screen

bendibao.go

Source:bendibao.go Github

copy

Full Screen

...24 b.openBrowser()25 }26 page := b.getPage()27 defer page.MustClose()28 elements := page.MustElement(`.height`).MustElements(`.info-list`)29 if elements.Empty() {30 return nil31 }32 return b.getRisk(elements)33}34func (b *BenDiBao) MiddleRisk() []Risk {35 if b.browser == nil {36 b.openBrowser()37 }38 page := b.getPage()39 defer page.MustClose()40 elements := page.MustElement(`.middle`).MustElements(`.info-list`)41 if elements.Empty() {42 return nil43 }44 return b.getRisk(elements)45}46func (b *BenDiBao) openBrowser() {47 if b.browser == nil {48 la := launcher.New().Headless(b.Headless).MustLaunch()49 b.browser = rod.New().ControlURL(la).MustConnect()50 }51}52func (b *BenDiBao) Close() (err error) {53 if b.browser != nil {54 err = b.browser.Close()55 }56 b.browser = nil57 return58}59func (b *BenDiBao) getRisk(elements rod.Elements) []Risk {60 risk := []Risk{}61 for _, v := range elements {62 city := v.MustElements(`div.shi>span`)63 if city.Empty() {64 continue65 }66 province := city.First()67 shi := city.Last()68 infoElements := v.MustElements(`ul.info-detail>li`)69 if infoElements.Empty() {70 continue71 }72 communitys := []string{}73 for _, v := range infoElements {74 info := v.MustElement(`span`).MustText()75 communitys = append(communitys, info)76 }77 r := Risk{78 Province: province.MustText(),79 City: shi.MustText(),80 Communitys: communitys,81 }82 risk = append(risk, r)83 }84 return risk85}86func (b *BenDiBao) getPage() *rod.Page {87 return b.browser....

Full Screen

Full Screen

Elements

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().Connect()4 elements, err := page.Elements("input")5 if err != nil {6 panic(err)7 }8 for _, element := range elements {9 text, _ := element.Eval(`this.value`).String()10 fmt.Println(text)11 }12 elements, err = page.Elements("input[title=\"Search\"]")13 if err != nil {14 panic(err)15 }16 for _, element := range elements {17 text, _ := element.Eval(`this.value`).String()18 fmt.Println(text)19 }20 elements, err = page.Elements("input[title=\"Search\"]")21 if err != nil {22 panic(err)23 }24 for _, element := range elements {25 text, _ := element.Eval(`this.value`).String()26 fmt.Println(text)27 }28 elements, err = page.Elements("input[title=\"Search\"]")29 if err != nil {30 panic(err)31 }32 for _, element := range elements {33 text, _ := element.Eval(`this.value`).String()34 fmt.Println(text)35 }36 elements, err = page.Elements("input[title=\"Search\"]")37 if err != nil {38 panic(err)39 }40 for _, element := range elements {41 text, _ := element.Eval(`this.value`).String()42 fmt.Println(text)43 }44 elements, err = page.Elements("input[title=\"Search\"]")45 if err != nil {46 panic(err)47 }48 for _, element := range elements {49 text, _ := element.Eval(`this.value`).String()50 fmt.Println(text)51 }

Full Screen

Full Screen

Elements

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 l := launcher.New().Headless(false)4 browser := rod.New().ControlURL(l).Connect()5 defer browser.Close()6 page := browser.Page("")7 defer page.Close()8 page.WaitLoad()9 elements := page.Elements("input")10 fmt.Println("Number of elements found: ", len(elements))11}

Full Screen

Full Screen

Elements

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().Connect()4 page.Element("#hplogo").MustScreenshot("logo.png")5 fmt.Println("Screenshot of Google logo saved to logo.png")6}

Full Screen

Full Screen

Elements

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().Connect()4 page.Element("#hplogo").MustWaitVisible()5 page.Element("#hplogo").MustWaitStable()6 page.Element("#hplogo").MustWaitInvisible()7 page.Element("#hplogo").MustWaitRemoved()8 fmt.Println("Elements method of rod class is used")9}

Full Screen

Full Screen

Elements

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 l := launcher.New().Bin("/usr/bin/chromium-browser").MustLaunch()4 defer l.Close()5 browser := rod.New().ControlURL(l).MustConnect()6 els := page.MustElements("a")7 fmt.Println(els[0].MustText())8}

Full Screen

Full Screen

Elements

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().Connect()4 defer browser.Close()5 page.Element(`[name="q"]`).Input("rod")6 page.Element(`[name="btnK"]`).Click()7 page.WaitLoad()8 fmt.Println(page.Element(`#search .g`).Text())9}

Full Screen

Full Screen

Elements

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().MustConnect()4 page.MustElement("input[name=q]").MustInput("rod").MustPress(rod.Enter)5 fmt.Println(page.MustElement("h3").MustText())6}

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