Go to file
2017-12-28 11:38:11 +07:00
client Splitting repositories 2017-12-28 11:17:14 +07:00
contrib Splitting repositories 2017-12-28 11:17:14 +07:00
kb Splitting repositories 2017-12-28 11:17:14 +07:00
runner Splitting repositories 2017-12-28 11:17:14 +07:00
testdata Fixing issue when node is offscreen and more. 2017-02-27 19:17:36 +07:00
.gitignore Minor change to gitignore 2017-09-04 10:09:20 +07:00
.travis.yml CI: use latest versions of each Go major version 2017-12-18 10:51:37 +07:00
actions.go Splitting repositories 2017-12-28 11:17:14 +07:00
chromedp_test.go Splitting repositories 2017-12-28 11:17:14 +07:00
chromedp.go Splitting repositories 2017-12-28 11:17:14 +07:00
errors.go Splitting repositories 2017-12-28 11:17:14 +07:00
eval.go Splitting repositories 2017-12-28 11:17:14 +07:00
handler.go Splitting repositories 2017-12-28 11:17:14 +07:00
input_test.go Splitting repositories 2017-12-28 11:17:14 +07:00
input.go Splitting repositories 2017-12-28 11:17:14 +07:00
js.go Splitting repositories 2017-12-28 11:17:14 +07:00
LICENSE Initial import 2017-01-24 22:09:23 +07:00
nav_test.go Splitting repositories 2017-12-28 11:17:14 +07:00
nav.go Splitting repositories 2017-12-28 11:17:14 +07:00
pool.go Splitting repositories 2017-12-28 11:17:14 +07:00
query_test.go Splitting repositories 2017-12-28 11:17:14 +07:00
query.go Splitting repositories 2017-12-28 11:17:14 +07:00
README.md Fixing broken links in README.md 2017-12-28 11:38:11 +07:00
sel_test.go Splitting repositories 2017-12-28 11:17:14 +07:00
sel.go Splitting repositories 2017-12-28 11:17:14 +07:00
util.go Splitting repositories 2017-12-28 11:17:14 +07:00

About chromedp Build Status Coverage Status

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.

Installing

Install in the usual way:

go get -u github.com/chromedp/chromedp

Using

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.

Resources

TODO

  • 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