2017-12-27 02:30:28 +00:00
|
|
|
# About chromedp [![Build Status][1]][2] [![Coverage Status][3]][4]
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
Package chromedp is a faster, simpler way to drive browsers in Go using the
|
2017-12-27 02:30:28 +00:00
|
|
|
[Chrome Debugging Protocol][5] (for Chrome, Edge, Safari, etc) without external
|
|
|
|
dependencies (ie, Selenium, PhantomJS, etc).
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
**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**.
|
|
|
|
|
2017-12-27 02:30:28 +00:00
|
|
|
## Installing
|
2017-01-24 15:09:23 +00:00
|
|
|
|
|
|
|
Install in the usual way:
|
|
|
|
|
|
|
|
```sh
|
2017-12-27 02:30:28 +00:00
|
|
|
go get -u github.com/chromedp/chromedp
|
2017-01-24 15:09:23 +00:00
|
|
|
```
|
|
|
|
|
2017-12-27 02:30:28 +00:00
|
|
|
## Using
|
2017-01-24 15:09:23 +00:00
|
|
|
|
2017-01-24 15:38:56 +00:00
|
|
|
Below is a simple Google search performed using chromedp (taken from
|
2017-12-27 02:30:28 +00:00
|
|
|
[examples/simple][6]):
|
2017-01-24 15:38:56 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
```go
|
2017-12-27 02:30:28 +00:00
|
|
|
// Command simple is a chromedp example demonstrating how to do a simple google
|
|
|
|
// search.
|
2017-01-24 15:38:56 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2017-12-27 02:30:28 +00:00
|
|
|
"github.com/chromedp/cdproto/cdp"
|
|
|
|
"github.com/chromedp/chromedp"
|
2017-01-24 15:38:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// create context
|
|
|
|
ctxt, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// create chrome instance
|
2017-12-27 02:30:28 +00:00
|
|
|
c, err := chromedp.New(ctxt, chromedp.WithLog(log.Printf))
|
2017-01-24 15:38:56 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// run task list
|
|
|
|
var site, res string
|
2017-10-06 23:30:23 +00:00
|
|
|
err = c.Run(ctxt, googleSearch("site:brank.as", "Home", &site, &res))
|
2017-01-24 15:38:56 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-10-06 23:30:23 +00:00
|
|
|
log.Printf("saved screenshot from search result listing `%s` (%s)", res, site)
|
2017-01-24 15:38:56 +00:00
|
|
|
}
|
|
|
|
|
2017-12-27 02:30:28 +00:00
|
|
|
func googleSearch(q, text string, site, res *string) chromedp.Tasks {
|
2017-01-24 15:38:56 +00:00
|
|
|
var buf []byte
|
|
|
|
sel := fmt.Sprintf(`//a[text()[contains(., '%s')]]`, text)
|
2017-12-27 02:30:28 +00:00
|
|
|
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 {
|
2017-10-06 23:30:23 +00:00
|
|
|
return ioutil.WriteFile("screenshot.png", buf, 0644)
|
2017-01-24 15:38:56 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2017-12-27 02:30:28 +00:00
|
|
|
Please see the [examples][6] project for more examples. Please refer to the
|
|
|
|
[GoDoc API listing][7] for a summary of the API and Actions.
|
2017-01-24 15:09:23 +00:00
|
|
|
|
2017-12-27 02:30:28 +00:00
|
|
|
## Resources
|
|
|
|
|
2017-12-28 04:38:11 +00:00
|
|
|
* [chromedp: A New Way to Drive the Web][8] - GopherCon SG 2017 talk
|
2017-12-27 02:30:28 +00:00
|
|
|
* [Chrome DevTools Protocol][5] - Chrome Debugging Protocol Domain documentation
|
|
|
|
* [chromedp examples][6] - various `chromedp` examples
|
2017-12-28 04:38:11 +00:00
|
|
|
* [`github.com/chromedp/cdproto`][9] - GoDoc listing for the CDP domains used by `chromedp`
|
2018-02-16 00:23:02 +00:00
|
|
|
* [`github.com/chromedp/cdproto-gen`][10] - tool used to generate `cdproto`
|
2017-12-28 04:38:11 +00:00
|
|
|
* [`github.com/chromedp/chromedp-proxy`][11] - a simple CDP proxy for logging/debugging CDP clients and browser instances
|
2017-07-01 05:22:00 +00:00
|
|
|
|
2017-01-24 15:09:23 +00:00
|
|
|
## TODO
|
2017-12-27 02:30:28 +00:00
|
|
|
|
2017-01-24 15:09:23 +00:00
|
|
|
* Move timeouts to context (defaults)
|
|
|
|
* Implement more query selector options (allow over riding context timeouts)
|
|
|
|
* Contextual actions for "dry run" (or via an accumulator?)
|
|
|
|
* Network loader / manager
|
|
|
|
* Profiler
|
2017-12-27 02:30:28 +00:00
|
|
|
|
|
|
|
[1]: https://travis-ci.org/chromedp/chromedp.svg
|
|
|
|
[2]: https://travis-ci.org/chromedp/chromedp
|
|
|
|
[3]: https://coveralls.io/repos/chromedp/chromedp/badge.svg?branch=master&service=github
|
|
|
|
[4]: https://coveralls.io/github/chromedp/chromedp?branch=master
|
|
|
|
[5]: https://chromedevtools.github.io/devtools-protocol/
|
|
|
|
[6]: https://github.com/chromedp/examples
|
|
|
|
[7]: https://godoc.org/github.com/chromedp/chromedp
|
|
|
|
[8]: https://www.youtube.com/watch?v=_7pWCg94sKw
|
2017-12-28 04:38:11 +00:00
|
|
|
[9]: https://godoc.org/github.com/chromedp/cdproto
|
2018-02-16 00:23:02 +00:00
|
|
|
[10]: https://github.com/chromedp/cdproto-gen
|
2017-12-28 04:38:11 +00:00
|
|
|
[11]: https://github.com/chromedp/chromedp-proxy
|