Go to file
Kenneth Shaw 5bef7dce13 Changes to ElementVisible/ElementNotVisible
Changed implementation of ElementVisible/ElementNotVisible to use a
javascript evaluation instead of the previous, cumbersome
implementation. As this may not work correctly in all scenarios, the old
versions will remain as ElementVisibleOld and ElementNotVisibleOld until
the new implementations can be vetted for correctness/performance.
2017-02-08 15:40:22 +07:00
cdp Expanding Evaluate API 2017-02-08 14:27:39 +07:00
client Updating latest protocol.json and adding some minor comment updates 2017-02-07 20:07:16 +07:00
cmd Expanding Evaluate API 2017-02-08 14:27:39 +07:00
contrib Initial import 2017-01-24 22:09:23 +07:00
examples Expanding Evaluate API 2017-02-08 14:27:39 +07:00
kb Updating latest protocol.json and adding some minor comment updates 2017-02-07 20:07:16 +07:00
runner Cleaning up chromedp/runner options 2017-01-28 09:32:44 +07:00
testdata Progress at getting SendKeys to send arrow keys 2017-01-28 12:07:34 +07:00
.gitignore Addition of chromedp-proxy, fixing SendKeys, more 2017-02-07 11:34:35 +07:00
actions.go Fixing issues with code generation 2017-01-26 14:28:34 +07:00
chromedp.go Fixing issues with code generation 2017-01-26 14:28:34 +07:00
eval.go Fixing Evaluate and EvaluateAsDevTools actions 2017-02-08 15:18:55 +07:00
handler.go Fixing issues with code generation 2017-01-26 14:28:34 +07:00
input.go Addition of chromedp-proxy, fixing SendKeys, more 2017-02-07 11:34:35 +07:00
LICENSE Initial import 2017-01-24 22:09:23 +07:00
nav.go Changes to ElementVisible/ElementNotVisible 2017-02-08 15:40:22 +07:00
query.go Fixing Evaluate and EvaluateAsDevTools actions 2017-02-08 15:18:55 +07:00
README.md Updating README.md with latest simple example code 2017-02-08 08:38:55 +07:00
sel.go Changes to ElementVisible/ElementNotVisible 2017-02-08 15:40:22 +07:00
util.go Fixing issues with code generation 2017-01-26 14:28:34 +07:00

About chromedp

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.

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)
	if err != nil {
		log.Fatal(err)
	}

	// run task list
	var site, res string
	err = c.Run(ctxt, googleSearch("site:brank.as", "Easy Money Management", &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 of #testimonials 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.Sleep(2 * time.Second),
		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.Sleep(2 * time.Second),
		cdp.WaitVisible(`#footer`, cdp.ByQuery),
		cdp.WaitNotVisible(`div.v-middle > div.la-ball-clip-rotate`, cdp.ByQuery),
		cdp.Location(site),
		cdp.Screenshot(`#testimonials`, &buf, cdp.ByID),
		cdp.ActionFunc(func(context.Context, cdptypes.FrameHandler) error {
			return ioutil.WriteFile("testimonials.png", buf, 0644)
		}),
	}
}

Please see the examples directory for examples.

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
  • More examples
  • Profiler
  • Unit tests / coverage: travis-ci + coveralls integration