🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ### 启动访问某个网站 ``` package main import ( "context" "log" "time" "github.com/chromedp/chromedp" ) func main() { // 禁用chrome headless opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Flag("headless", false), ) allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...) defer cancel() // create chrome instance ctx, cancel := chromedp.NewContext( allocCtx, chromedp.WithLogf(log.Printf), ) defer cancel() // create a timeout ctx, cancel = context.WithTimeout(ctx, 5*time.Second) defer cancel() // navigate to a page, wait for an element, click var example string sel := `//*[@id="username"]` err := chromedp.Run(ctx, chromedp.Navigate(`https://github.com/awake1t`), chromedp.WaitVisible("body"), //缓一缓 chromedp.Sleep(2*time.Second), chromedp.SendKeys(sel, "username", chromedp.BySearch), //匹配xpath ) if err != nil { log.Fatal(err) } log.Printf("Go's time.After example:\n%s", example) } ```