Golang Chromedp自动刷新页面直至定位到特定的HTML元素
时间:2024-03-22
浏览:277
作者:佚名
为了在 Golang Chromedp 中实现自动刷新页面直到定位到特定 HTML 元素,关键是使用选项 `chromedp.atleast(0)`。该选项使查询即使未找到节点也能立即返回。这样,可以循环导航到目标 URL,查询所需元素是否存在,并在找到后单击它。
问题内容
chromedp.Navigate(tragetUrl),
chromedp.WaitVisible("#button"),
chromedp.Click("#button"),
目标:如果#button不存在,则重新加载当前页面,直到按钮出现并单击它
#button 会随机出现,具体取决于目标网站。
有什么好的建议可以实现上述目标吗?
正确答案
关键是使用选项 chromedp.atleast(0)。请参阅下面的演示:
func ReloadUntilButtonAppears(ctx context.Context, targetURL string) error {
var nodes []*cdp.Node
for {
if err := chromedp.Run(ctx,
chromedp.Navigate(targetURL),
// chromedp.AtLeast(0) makes the query return immediately
// even if there are not nodes found.
chromedp.Nodes("#button", &nodes, chromedp.ByQuery, chromedp.AtLeast(0)),
); err != nil {
return err
}
if len(nodes) > 0 {
return chromedp.Run(ctx, chromedp.MouseClickNode(nodes[0]))
}
}
}
今天关于《Golang Chromedp自动刷新页面直至定位到特定的HTML元素》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
