go-rod / rod

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

page.ElementR returns fake element #984

Closed chekun closed 6 months ago

chekun commented 10 months ago

Rod Version: v0.114.5

The code to demonstrate your question

  1. Clone Rod to your local and cd to the repository:

    git clone https://github.com/go-rod/rod
    cd rod
    git checkout v0.114.5
  2. Use your code to replace the content of function TestRod in file rod_test.go.

  3. Test your code with: go test -run TestRod, make sure it fails as expected.

  4. Replace ALL THE CONTENT under "The code to demonstrate your question" with your TestRod function, like below:

func TestRod(t *testing.T) {
    l := launcher.New()
    l = l.Headless(false).Bin("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome")
    controlURL, _ := l.Launch()
    browser := rod.New().Trace(true).ControlURL(controlURL).MustConnect()
    defer browser.Close()
    page := browser.MustPage("https://www.baidu.com").MustWaitLoad()
    blockedEl, err := page.Timeout(2*time.Second).ElementR("div", "You’re Temporarily Blocked")
    log.Println(blockedEl, err)
    log.Println("text is")
    log.Println(blockedEl.Text())
    log.Println("html is")
    log.Println(blockedEl.HTML())
}

What you got

2023/11/29 18:04:09 <div#006a894f3906632b> <nil>
2023/11/29 18:04:09 text is
2023/11/29 18:04:09 rod.elementR("div","/You’re Temporarily Blocked/")  <nil>
2023/11/29 18:04:09 html is
2023/11/29 18:04:09 <div id="006a894f3906632b" style="position: fixed; z-index: 2147483647; border: none; border-radius: 3px; box-shadow: rgb(95, 50, 50) 0px 0px 3px; pointer-events: none; box-sizing: border-box; left: 0px; top: 0px; height: 0px; width: 500px;"><div style="position: absolute; color: rgb(204, 38, 214); font-size: 12px; background: rgba(255, 255, 255, 0.92); box-shadow: rgb(51, 51, 51) 0px 0px 3px; padding: 2px 5px; border-radius: 3px; white-space: nowrap; top: 0px;"><code>rod.elementR("div","/You’re Temporarily Blocked/") </code></div></div> <nil>

What you expected to see

I expect to see blockedEl == nil

What have you tried to solve the question

I have no idea to solve this, I mean if the element is not there ,shouldn't it be nil ?

nzoschke commented 1 week ago

Seeing the same issue here