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

if err == nil 后的逻辑不会执行 #1032

Closed BeyondIM closed 6 months ago

BeyondIM commented 6 months ago

Rod Version: v0.114.8

func TestRod(t *testing.T) {
    _, err := page.Element("[id^='postmessage_'] .locked")
    if err == nil {
        page.MustElement("[id^='postmessage_'] .locked a").MustClick()
        postCommentBtn := page.MustElement("#fwin_reply button[type='submit']")
        postCommentBtn.MustWaitStable()
        page.MustElement("#fwin_reply #postmessage").MustInput("感谢分享")
        postCommentBtn.MustClick().MustWaitInvisible()
    }

    slog.Info("debug", "111", "111")
}

存在的问题是,如果页面存在 [id^='postmessage_'] .locked,那么代码执行没有问题,如果不存在,则 slog.Info("debug", "111", "111") 不会执行,很奇怪

ysmood commented 6 months ago

请仔细阅读 page.Element 的文档

https://github.com/go-rod/rod/blob/0340433eab4e6551f075eaf1875035469a777480/query.go#L140-L142