ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[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) } ```