go-rod / rod

A Chrome DevTools Protocol driver for web automation and scraping.

Home Page:https://go-rod.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

BeyondIM opened this issue · comments

commented

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") 不会执行,很奇怪

请仔细阅读 page.Element 的文档

rod/query.go

Lines 140 to 142 in 0340433

// Element retries until an element in the page that matches the CSS selector, then returns
// the matched element.
func (p *Page) Element(selector string) (*Element, error) {