headlessChrome

package module
v0.0.0-...-676f52a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 2, 2019 License: MIT Imports: 6 Imported by: 2

README ¶

DO NOT USE! This project does not work anymore due to changes in Chrome. Use the Chrome DevTools protocol in chromedp instead!

headlessChrome 🤖

Support only for Ubuntu on Docker for now. Mac appears to not be working. 😬

A go package for working with headless Chrome. Run interactive JavaScript commands on pages with go and Chrome without a GUI. Includes a few helpful functions out of the box to query and click selector paths by their classes, divs, or html content.

You could use this package to click buttons and scrape content on/from a website as if you were a browser, or to render pages that wouldn't be supported by other things like phantomjs or casperjs. Especially useful for sites that use EmberJS, where the content is rendered by javascript after the HTML payload is delivered.

Examples

An example project that does some simple things with a Makefile and Dockerfile is in the examples directory.

Install

go get github.com/integrii/headlessChrome

Documentation

http://godoc.org/github.com/integrii/headlessChrome

Docker Version

To run Chrome headless with docker, check out examples/docker/main.go as well as examples/docker/Makefile. When in that directory, you can do make test to build and run the container with the example app inside. You will see the source of httpbin.org displayed at the end of the build and run.

Custom Flags

By default, we startup with the bare minimum flags necessary to start headless chrome and open a javascript console. If you want more flags, like a resolution size, or a custom User-Agent, you can specify it by replacing the Args variable. Just be sure to append to it so you don't kill the default flags...

headlessChrome.Args = append(headlessChrome.Args,"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")
headlessChrome.Args = append(headlessChrome.Args,"--window-size=1024,768")
Changing the Path to Chrome

Change the path to Chrome by simply setting the headlessChrome.ChromePath variable.

headlessChrome.ChromePath = `/opt/google/chrome-unstable/chrome`
JavaScript Helper Examples

Find the full list in the docs.

// click some span element from the page by its text content
browser.ClickItemWithInnerHTML("span", "Google Search",0)

// select the content of something by its css classes
browser.GetContentOfItemWithClasses("button arrow bold",0)
time.Sleep(time.Second) // give it a second to query

// read the selected stuff from the console by picking
// the next item from the output channel
fmt.Println(<-browser.Output)
Contributing

Please send pull requests! It would be good to have support for more operating systems or more handy helpers to run more commonly used javascript code easily. Adding support for other operating systems should be as simple as checking the platform type and changing the ChromePath variable's default value.

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

View Source
var Args = []string{
	"--headless",
	"--disable-gpu",
	"--repl",
}

Args are the args that will be used to start chrome

View Source
var BrowserStartupTime = time.Second * 20

BrowserStartupTime is how long chrome has to startup the console before we consider it a failure

View Source
var ChromePath = ChromePathMacOS

ChromePath is the command to execute chrome

View Source
var ChromePathDocker = `/opt/google/chrome-unstable/chrome`

ChromePathDocker is where chrome normally lives in the project's docker container

View Source
var ChromePathMacOS = `/Applications/Google Chrome.app/Contents/MacOS/Google Chrome`

ChromePathMacOS is where chrome normally lives on MacOS

View Source
var Debug bool

Debug enables debug output for this package to console

Functions ¶

This section is empty.

Types ¶

type ChromeSession ¶

type ChromeSession struct {
	Session *interactive.Session
	Output  chan string
	Input   chan string
}

ChromeSession is an interactive console Session with a Chrome instance.

func NewBrowser ¶

func NewBrowser(url string) (*ChromeSession, error)

NewBrowser starts a new chrome headless Session.

func NewBrowserWithTimeout ¶

func NewBrowserWithTimeout(url string, timeout time.Duration) (*ChromeSession, error)

NewBrowserWithTimeout starts a new chrome headless session but limits how long it can run before its killed forcefully. A time limit of 0 means there is not a time limit

func (*ChromeSession) ClickItemWithClasses ¶

func (cs *ChromeSession) ClickItemWithClasses(classes string, itemIndex int)

ClickItemWithClasses clicks on the first item it finds with the provided classes. Multiple classes are separated by spaces

func (*ChromeSession) ClickItemWithID ¶

func (cs *ChromeSession) ClickItemWithID(id string)

ClickItemWithID clicks an item with the specified id

func (*ChromeSession) ClickItemWithInnerHTML ¶

func (cs *ChromeSession) ClickItemWithInnerHTML(elementType string, s string, itemIndex int)

ClickItemWithInnerHTML clicks an item that has the matching inner html

func (*ChromeSession) ClickSelector ¶

func (cs *ChromeSession) ClickSelector(s string)

ClickSelector calls a click() on the supplied selector

func (*ChromeSession) Exit ¶

func (cs *ChromeSession) Exit()

Exit exits the running command out by ossuing a 'quit' to the chrome console

func (*ChromeSession) ForceClose ¶

func (cs *ChromeSession) ForceClose()

ForceClose issues a force kill to the command

func (*ChromeSession) GetContentOfItemWithClasses ¶

func (cs *ChromeSession) GetContentOfItemWithClasses(classes string, itemIndex int)

GetContentOfItemWithClasses fetches the content of the element with the specified classes

func (*ChromeSession) GetContentOfItemWithSelector ¶

func (cs *ChromeSession) GetContentOfItemWithSelector(selector string)

GetContentOfItemWithSelector gets the content of an element with the specified selector

func (*ChromeSession) GetItemWithInnerHTML ¶

func (cs *ChromeSession) GetItemWithInnerHTML(elementType string, s string, itemIndex int)

GetItemWithInnerHTML fetches the item with the specified innerHTML content

func (*ChromeSession) GetValueOfItemWithClasses ¶

func (cs *ChromeSession) GetValueOfItemWithClasses(classes string, itemIndex int)

GetValueOfItemWithClasses returns the form value of the specified item

func (*ChromeSession) SetInputTextByClasses ¶

func (cs *ChromeSession) SetInputTextByClasses(classes string, itemIndex int, text string)

SetInputTextByClasses sets the input text for an input field

func (*ChromeSession) SetTextByClasses ¶

func (cs *ChromeSession) SetTextByClasses(classes string, itemIndex int, text string)

SetTextByClasses sets the text on the div with the specified id

func (*ChromeSession) SetTextByID ¶

func (cs *ChromeSession) SetTextByID(id string, text string)

SetTextByID sets the text on the div with the specified id

func (*ChromeSession) Write ¶

func (cs *ChromeSession) Write(s string)

Write writes to the Session

Directories ¶

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL