chromedp等待页面加载完成的代码例子
以下是使用chromedp等待页面加载完成的代码例子:
package main
import (
"context"
"fmt"
"time"
"github.com/chromedp/chromedp"
)
func main() {
// create a new context
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
// navigate to a website
err := chromedp.Run(ctx, chromedp.Navigate("https://www.example.com"))
if err != nil {
fmt.Println(err)
return
}
// wait for the page to load completely
err = chromedp.Run(ctx, chromedp.WaitVisible("body", chromedp.ByQuery))
if err != nil {
fmt.Println(err)
return
}
// wait for an additional 5 seconds to ensure all content is loaded
time.Sleep(5 * time.Second)
// do something with the loaded page
// ...
}
在上面的例子中,我们使用chromedp导航到一个网站,然后使用chromedp.WaitVisible等待页面加载完成。等待完成后,我们等待额外的5秒钟,以确保所有内容都已加载。然后可以在页面上执行其他操作。
原文地址: https://www.cveoy.top/t/topic/bIIL 著作权归作者所有。请勿转载和采集!