go-rod / rod

A Chrome DevTools Protocol driver for web automation and scraping.
https://go-rod.github.io
MIT License
5.01k stars 328 forks source link

Is there any way to wait for a certain element in the page: #1023

Closed cplasfwst closed 3 months ago

cplasfwst commented 3 months ago

Rod Version: vv0.114.7

Is there any way to wait for a certain element in the page: page2.MustElementX(//div[@data-e2e="comment-list"]).MustElementsX(./div) The following div is completely loaded before continuing to perform subsequent operations? Because sometimes the number of loads may be 0 or 1, the morethan method should not be used appropriately.

github-actions[bot] commented 3 months ago

Please add a valid Rod Version: v0.0.0 to your issue. Current version is v0.114.8

generated by check-issue

cplasfwst commented 3 months ago

例如我的代码 //寻找评论按钮 page2.MustElementX(//div[@data-e2e="feed-comment-icon"]).MustClick() //.MustWaitLoad() //page2.MustWaitStable() //wait() //time.Sleep(time.Second * 3) //-----------先获取评论框,等待评论框出现后再获取评论条 //page2.MustElementX(//div[@class="Gacm1K1M comment-header-inner-container"]) //fmt.Println("等待完成") //time.Sleep(time.Second * 5) //page2.MustElementX(//div[@data-e2e="comment-list"]`).MustWaitLoad() //寻找评论列表里面的所有评论

    p2 := page2.MustElementX(`//div[@data-e2e="comment-list"]`).MustElementsX(`./div`)
    fmt.Println("一共加载了评论:", len(p2))`

Only delayed waiting will cause all 21 divs to appear, but I am not sure how long time.sleep will last. If you use the method I commented out above, only one child element will appear. This is the cmd output: Total comments loaded: 1 If you use time.sleep (2 seconds), you can fully load 21

IsmAbd commented 3 months ago

I got lucky with err = page.WaitElementsMoreThan("selector", 0).

cplasfwst commented 3 months ago

I got lucky with err = page.WaitElementsMoreThan("selector", 0).

I seem to have solved the above problem. Can I operate 2 pages at the same time?

ysmood commented 3 months ago

Can I operate 2 pages at the same time?

Yes