commit | 0406fa8a8ae689ea13f9161dc9c53efbb562361c | [log] [tgz] |
---|---|---|
author | Kenneth Shaw <kenneth.shaw@knq.io> | Tue Jul 10 12:51:58 2018 |
committer | Kenneth Shaw <kenneth.shaw@knq.io> | Tue Jul 10 12:51:58 2018 |
tree | 1cd4609df60b8d8a3a96ab5addc4ba837f68bb26 | |
parent | db9a8731419af3b4aefc0216969a9e0f5d2525db [diff] |
Fixing issue with kb
Package chromedp is a faster, simpler way to drive browsers in Go using the Chrome Debugging Protocol (for Chrome, Edge, Safari, etc) without external dependencies (ie, Selenium, PhantomJS, etc).
NOTE: chromedp's API is currently unstable, and may change at a moments notice. There are likely extremely bad bugs lurking in this code. CAVEAT USER.
Install in the usual way:
go get -u github.com/chromedp/chromedp
Below is a simple Google search performed using chromedp (taken from examples/simple):
This example shows logic for a simple search for a known website, clicking on the right link, and then taking a screenshot of a specific element on the loaded page and saving that to a local file on disk.
// Command simple is a chromedp example demonstrating how to do a simple google // search. package main import ( "context" "fmt" "io/ioutil" "log" "time" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/chromedp" ) func main() { var err error // create context ctxt, cancel := context.WithCancel(context.Background()) defer cancel() // create chrome instance c, err := chromedp.New(ctxt, chromedp.WithLog(log.Printf)) if err != nil { log.Fatal(err) } // run task list var site, res string err = c.Run(ctxt, googleSearch("site:brank.as", "Home", &site, &res)) if err != nil { log.Fatal(err) } // shutdown chrome err = c.Shutdown(ctxt) if err != nil { log.Fatal(err) } // wait for chrome to finish err = c.Wait() if err != nil { log.Fatal(err) } log.Printf("saved screenshot from search result listing `%s` (%s)", res, site) } func googleSearch(q, text string, site, res *string) chromedp.Tasks { var buf []byte sel := fmt.Sprintf(`//a[text()[contains(., '%s')]]`, text) return chromedp.Tasks{ chromedp.Navigate(`https://www.google.com`), chromedp.WaitVisible(`#hplogo`, chromedp.ByID), chromedp.SendKeys(`#lst-ib`, q+"\n", chromedp.ByID), chromedp.WaitVisible(`#res`, chromedp.ByID), chromedp.Text(sel, res), chromedp.Click(sel), chromedp.WaitNotVisible(`.preloader-content`, chromedp.ByQuery), chromedp.WaitVisible(`a[href*="twitter"]`, chromedp.ByQuery), chromedp.Location(site), chromedp.ScrollIntoView(`.banner-section.third-section`, chromedp.ByQuery), chromedp.Sleep(2 * time.Second), // wait for animation to finish chromedp.Screenshot(`.banner-section.third-section`, &buf, chromedp.ByQuery), chromedp.ActionFunc(func(context.Context, cdp.Executor) error { return ioutil.WriteFile("screenshot.png", buf, 0644) }), } }
Please see the examples project for more examples. Please refer to the GoDoc API listing for a summary of the API and Actions.
chromedp
examplesgithub.com/chromedp/cdproto
- GoDoc listing for the CDP domains used by chromedp
github.com/chromedp/cdproto-gen
- tool used to generate cdproto
github.com/chromedp/chromedp-proxy
- a simple CDP proxy for logging/debugging CDP clients and browser instances