Go to file
2017-12-22 10:07:54 +07:00
cdp Updating to latest protocol.json 2017-12-22 10:07:54 +07:00
client Fixing issue with missing target_type values in client package 2017-12-22 10:01:21 +07:00
cmd Updating to latest protocol.json 2017-12-22 10:07:54 +07:00
contrib Fixing issues with unit tests 2017-12-18 07:11:42 +07:00
examples Updating eval example 2017-12-22 09:01:51 +07:00
kb Updating to latest protocol.json 2017-07-31 09:26:12 +07:00
runner Minor change to documentation on runner.DefaultChromePath consts 2017-12-22 08:43:51 +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 all: simplify some error variables 2017-12-18 10:51:37 +07:00
chromedp_test.go Fixing issue with unit tests so that they run properly on Darwin/Windows 2017-12-18 09:55:04 +07:00
chromedp.go all: simplify some error variables 2017-12-18 10:51:37 +07:00
doc.go Renaming FrameHandler, updating to latest protocol.json, and code fixes 2017-02-12 12:50:46 +07:00
errors.go Code cleanup 2017-02-12 14:08:40 +07:00
eval.go all: simplify some error variables 2017-12-18 10:51:37 +07:00
handler.go Add empty *page.EventLifecycleEvent handler 2017-12-18 11:03:22 +07:00
input_test.go Minor changes to unit tests in attempt to fix issues with travis 2017-06-18 09:32:12 +07:00
input.go Updating to latest protocol.json 2017-07-14 10:35:11 +07:00
LICENSE Initial import 2017-01-24 22:09:23 +07:00
nav_test.go Updating to latest protocol.json (clarifying timestamp behavior) 2017-07-09 08:40:29 +07:00
nav.go Minor documentation change to CaptureScreenshot 2017-12-19 12:14:35 +07:00
pool.go all: simplify some error variables 2017-12-18 10:51:37 +07:00
query_test.go Minor documentation cleanup from last commit 2017-12-18 10:48:40 +07:00
query.go all: simplify some error variables 2017-12-18 10:51:37 +07:00
README.md Updating examples/simple/main.go 2017-10-07 06:30:23 +07:00
sel_test.go Adding script to generate domains from protocol.json and updating to latest protocol.json 2017-05-06 08:20:24 +07:00
sel.go all: simplify some error variables 2017-12-18 10:51:37 +07:00
util.go Adding OuterHTML and InnerHTML actions 2017-07-01 11:53:22 +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.

Installation

Install in the usual way:

go get -u github.com/knq/chromedp

Usage

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.

// examples/simple/main.go
package main

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"time"

	cdp "github.com/knq/chromedp"
	cdptypes "github.com/knq/chromedp/cdp"
)

func main() {
	var err error

	// create context
	ctxt, cancel := context.WithCancel(context.Background())
	defer cancel()

	// create chrome instance
	c, err := cdp.New(ctxt, cdp.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) cdp.Tasks {
	var buf []byte
	sel := fmt.Sprintf(`//a[text()[contains(., '%s')]]`, text)
	return cdp.Tasks{
		cdp.Navigate(`https://www.google.com`),
		cdp.WaitVisible(`#hplogo`, cdp.ByID),
		cdp.SendKeys(`#lst-ib`, q+"\n", cdp.ByID),
		cdp.WaitVisible(`#res`, cdp.ByID),
		cdp.Text(sel, res),
		cdp.Click(sel),
		cdp.WaitVisible(`a[href="/brankas-for-business"]`, cdp.ByQuery),
		cdp.WaitNotVisible(`.preloader-content`, cdp.ByQuery),
		cdp.Location(site),
		cdp.ScrollIntoView(`.banner-section.third-section`, cdp.ByQuery),
		cdp.Sleep(2 * time.Second), // wait for animation to finish
		cdp.Screenshot(`.banner-section.third-section`, &buf, cdp.ByQuery),
		cdp.ActionFunc(func(context.Context, cdptypes.Handler) error {
			return ioutil.WriteFile("screenshot.png", buf, 0644)
		}),
	}
}

Please see the examples directory for some more examples, and please refer to the GoDoc API listing for a summary of the API and Actions.

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