多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[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) } ```