chromedp: github.com/knq/chromedp Index | Examples | Files | Directories

package chromedp

import "github.com/knq/chromedp"

Package chromedp is a high level Chrome DevTools Protocol client that simplifies driving browsers for scraping, unit testing, or profiling web pages using the CDP.

chromedp requires no third-party dependencies, implementing the async Chrome DevTools Protocol entirely in Go.

Index

Examples

Package Files

allocate.go allocate_linux.go browser.go browser_easyjson.go chromedp.go conn.go errors.go eval.go input.go js.go nav.go query.go sel.go target.go util.go

Variables

var DefaultExecAllocatorOptions = []ExecAllocatorOption{
    NoFirstRun,
    NoDefaultBrowserCheck,
    Headless,

    Flag("disable-background-networking", true),
    Flag("enable-features", "NetworkService,NetworkServiceInProcess"),
    Flag("disable-background-timer-throttling", true),
    Flag("disable-backgrounding-occluded-windows", true),
    Flag("disable-breakpad", true),
    Flag("disable-client-side-phishing-detection", true),
    Flag("disable-default-apps", true),
    Flag("disable-dev-shm-usage", true),
    Flag("disable-extensions", true),
    Flag("disable-features", "site-per-process,TranslateUI,BlinkGenPropertyTrees"),
    Flag("disable-hang-monitor", true),
    Flag("disable-ipc-flooding-protection", true),
    Flag("disable-popup-blocking", true),
    Flag("disable-prompt-on-repost", true),
    Flag("disable-renderer-backgrounding", true),
    Flag("disable-sync", true),
    Flag("force-color-profile", "srgb"),
    Flag("metrics-recording-only", true),
    Flag("safebrowsing-disable-auto-update", true),
    Flag("enable-automation", true),
    Flag("password-store", "basic"),
    Flag("use-mock-keychain", true),
}

DefaultExecAllocatorOptions are the ExecAllocator options used by NewContext if the given parent context doesn't have an allocator set up.

func ButtonLeft Uses

func ButtonLeft(p *input.DispatchMouseEventParams) *input.DispatchMouseEventParams

ButtonLeft is a mouse action option to set the button clicked as the left mouse button.

func ButtonMiddle Uses

func ButtonMiddle(p *input.DispatchMouseEventParams) *input.DispatchMouseEventParams

ButtonMiddle is a mouse action option to set the button clicked as the middle mouse button.

func ButtonNone Uses

func ButtonNone(p *input.DispatchMouseEventParams) *input.DispatchMouseEventParams

ButtonNone is a mouse action option to set the button clicked as none (used for mouse movements).

func ButtonRight Uses

func ButtonRight(p *input.DispatchMouseEventParams) *input.DispatchMouseEventParams

ButtonRight is a mouse action option to set the button clicked as the right mouse button.

func ByID Uses

func ByID(s *Selector)

ByID is a query option to select a single element by their CSS #id.

func ByNodeID Uses

func ByNodeID(s *Selector)

ByNodeID is a query option to select elements by their NodeIDs.

func ByQuery Uses

func ByQuery(s *Selector)

ByQuery is a query option to select a single element using DOM.querySelector.

func ByQueryAll Uses

func ByQueryAll(s *Selector)

ByQueryAll is a query option to select elements by DOM.querySelectorAll.

func BySearch Uses

func BySearch(s *Selector)

BySearch is a query option via DOM.performSearch (works with both CSS and XPath queries).

func Cancel Uses

func Cancel(ctx context.Context) error

Cancel cancels a chromedp context, waits for its resources to be cleaned up, and returns any error encountered during that process.

Usually a "defer cancel()" will be enough for most use cases. This API is useful if you want to catch underlying cancel errors, such as when a temporary directory cannot be deleted.

func DisableGPU Uses

func DisableGPU(a *ExecAllocator)

DisableGPU is the command line option to disable the GPU process.

func EvalAsValue Uses

func EvalAsValue(p *runtime.EvaluateParams) *runtime.EvaluateParams

EvalAsValue is a evaluate option that will cause the evaluated script to encode the result of the expression as a JSON-encoded value.

func EvalIgnoreExceptions Uses

func EvalIgnoreExceptions(p *runtime.EvaluateParams) *runtime.EvaluateParams

EvalIgnoreExceptions is a evaluate option that will cause script evaluation to ignore exceptions.

func EvalWithCommandLineAPI Uses

func EvalWithCommandLineAPI(p *runtime.EvaluateParams) *runtime.EvaluateParams

EvalWithCommandLineAPI is an evaluate option to make the DevTools Command Line API available to the evaluated script.

Note: this should not be used with untrusted Javascript.

func Headless Uses

func Headless(a *ExecAllocator)

Headless is the command line option to run in headless mode. On top of setting the headless flag, it also hides scrollbars and mutes audio.

func ListenBrowser Uses

func ListenBrowser(ctx context.Context, fn func(ev interface{}))

ListenBrowser adds a function which will be called whenever a browser event is received on the chromedp context. Cancelling ctx stops the listener from receiving any more events.

Note that the function is called synchronously when handling events. The function should avoid blocking at all costs. For example, any Actions must be run via a separate goroutine.

func ListenTarget Uses

func ListenTarget(ctx context.Context, fn func(ev interface{}))

ListenTarget adds a function which will be called whenever a target event is received on the chromedp context. Note that this only includes browser events; command responses and target events are not included. Cancelling ctx stops the listener from receiving any more events.

Note that the function is called synchronously when handling events. The function should avoid blocking at all costs. For example, any Actions must be run via a separate goroutine.

Code:

ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()

ts := httptest.NewServer(writeHTML(`
<body>
<script>
	console.log("hello js world")
	var p = document.createElement("div");
	p.setAttribute("id", "jsFinished");
	document.body.appendChild(p);
</script>
</body>
	`))
defer ts.Close()

chromedp.ListenTarget(ctx, func(ev interface{}) {
    switch ev := ev.(type) {
    case *cdpruntime.EventConsoleAPICalled:
        fmt.Printf("console.%s call:\n", ev.Type)
        for _, arg := range ev.Args {
            fmt.Printf("%s - %s\n", arg.Type, arg.Value)
        }
    }
})

if err := chromedp.Run(ctx,
    chromedp.Navigate(ts.URL),
    chromedp.WaitVisible("#jsFinished", chromedp.ByID),
); err != nil {
    panic(err)
}

Output:

console.log call:
string - "hello js world"

func NewContext Uses

func NewContext(parent context.Context, opts ...ContextOption) (context.Context, context.CancelFunc)

NewContext creates a chromedp context from the parent context. The parent context's Allocator is inherited, defaulting to an ExecAllocator with DefaultExecAllocatorOptions.

If the parent context contains an allocated Browser, the child context inherits it, and its first Run creates a new tab on that browser. Otherwise, its first Run will allocate a new browser.

Cancelling the returned context will close a tab or an entire browser, depending on the logic described above. To cancel a context while checking for errors, see Cancel.

Code:

// new browser, first tab
ctx1, cancel := chromedp.NewContext(context.Background())
defer cancel()

// ensure the first tab is created
if err := chromedp.Run(ctx1); err != nil {
    panic(err)
}

// same browser, second tab
ctx2, _ := chromedp.NewContext(ctx1)

// ensure the second tab is created
if err := chromedp.Run(ctx2); err != nil {
    panic(err)
}

c1 := chromedp.FromContext(ctx1)
c2 := chromedp.FromContext(ctx2)

fmt.Printf("Same browser: %t\n", c1.Browser == c2.Browser)
fmt.Printf("Same tab: %t\n", c1.Target == c2.Target)

Output:

Same browser: true
Same tab: false

func NewExecAllocator Uses

func NewExecAllocator(parent context.Context, opts ...ExecAllocatorOption) (context.Context, context.CancelFunc)

NewExecAllocator creates a new context set up with an ExecAllocator, suitable for use with NewContext.

func NewRemoteAllocator Uses

func NewRemoteAllocator(parent context.Context, url string) (context.Context, context.CancelFunc)

NewRemoteAllocator creates a new context set up with a RemoteAllocator, suitable for use with NewContext.

func NoDefaultBrowserCheck Uses

func NoDefaultBrowserCheck(a *ExecAllocator)

NoDefaultBrowserCheck is the Chrome comamnd line option to disable the default browser check.

func NoFirstRun Uses

func NoFirstRun(a *ExecAllocator)

NoFirstRun is the Chrome comamnd line option to disable the first run dialog.

func NoSandbox Uses

func NoSandbox(a *ExecAllocator)

NoSandbox is the Chrome comamnd line option to disable the sandbox.

func NodeEnabled Uses

func NodeEnabled(s *Selector)

NodeEnabled is a query option to wait until the element is enabled.

func NodeNotPresent Uses

func NodeNotPresent(s *Selector)

NodeNotPresent is a query option to wait until no elements are present matching the selector.

func NodeNotVisible Uses

func NodeNotVisible(s *Selector)

NodeNotVisible is a query option to wait until the element is not visible.

func NodeReady Uses

func NodeReady(s *Selector)

NodeReady is a query option to wait until the element is ready.

func NodeSelected Uses

func NodeSelected(s *Selector)

NodeSelected is a query option to wait until the element is selected.

func NodeVisible Uses

func NodeVisible(s *Selector)

NodeVisible is a query option to wait until the element is visible.

func Run Uses

func Run(ctx context.Context, actions ...Action) error

Run runs an action against context. The provided context must be a valid chromedp context, typically created via NewContext.

func Targets Uses

func Targets(ctx context.Context) ([]*target.Info, error)

Targets lists all the targets in the browser attached to the given context.

type Action Uses

type Action interface {
    // Do executes the action using the provided context and frame handler.
    Do(context.Context) error
}

Action is the common interface for an action that will be executed against a context and frame handler.

func AttributeValue Uses

func AttributeValue(sel interface{}, name string, value *string, ok *bool, opts ...QueryOption) Action

AttributeValue retrieves the element attribute value for the first node matching the selector.

func Attributes Uses

func Attributes(sel interface{}, attributes *map[string]string, opts ...QueryOption) Action

Attributes retrieves the element attributes for the first node matching the selector.

func AttributesAll Uses

func AttributesAll(sel interface{}, attributes *[]map[string]string, opts ...QueryOption) Action

AttributesAll retrieves the element attributes for all nodes matching the selector.

Note: this should be used with the ByQueryAll selector option.

func Blur Uses

func Blur(sel interface{}, opts ...QueryOption) Action

Blur unfocuses (blurs) the first node matching the selector.

func CaptureScreenshot Uses

func CaptureScreenshot(res *[]byte) Action

CaptureScreenshot captures takes a screenshot of the current viewport.

Note: this an alias for page.CaptureScreenshot.

func Clear Uses

func Clear(sel interface{}, opts ...QueryOption) Action

Clear clears the values of any input/textarea nodes matching the selector.

func Click Uses

func Click(sel interface{}, opts ...QueryOption) Action

Click sends a mouse click event to the first node matching the selector.

func ComputedStyle Uses

func ComputedStyle(sel interface{}, style *[]*css.ComputedProperty, opts ...QueryOption) Action

ComputedStyle retrieves the computed style of the first node matching the selector.

func Dimensions Uses

func Dimensions(sel interface{}, model **dom.BoxModel, opts ...QueryOption) Action

Dimensions retrieves the box model dimensions for the first node matching the selector.

func DoubleClick Uses

func DoubleClick(sel interface{}, opts ...QueryOption) Action

DoubleClick sends a mouse double click event to the first node matching the selector.

func Evaluate Uses

func Evaluate(expression string, res interface{}, opts ...EvaluateOption) Action

Evaluate is an action to evaluate the Javascript expression, unmarshaling the result of the script evaluation to res.

When res is a type other than *[]byte, or **chromedp/cdproto/runtime.RemoteObject, then the result of the script evaluation will be returned "by value" (ie, JSON-encoded), and subsequently an attempt will be made to json.Unmarshal the script result to res.

Otherwise, when res is a *[]byte, the raw JSON-encoded value of the script result will be placed in res. Similarly, if res is a *runtime.RemoteObject, then res will be set to the low-level protocol type, and no attempt will be made to convert the result.

Note: any exception encountered will be returned as an error.

func EvaluateAsDevTools Uses

func EvaluateAsDevTools(expression string, res interface{}, opts ...EvaluateOption) Action

EvaluateAsDevTools is an action that evaluates a Javascript expression as Chrome DevTools would, evaluating the expression in the "console" context, and making the Command Line API available to the script.

Note: this should not be used with untrusted Javascript.

func Focus Uses

func Focus(sel interface{}, opts ...QueryOption) Action

Focus focuses the first node matching the selector.

func InnerHTML Uses

func InnerHTML(sel interface{}, html *string, opts ...QueryOption) Action

InnerHTML retrieves the inner html of the first node matching the selector.

func JavascriptAttribute Uses

func JavascriptAttribute(sel interface{}, name string, res interface{}, opts ...QueryOption) Action

JavascriptAttribute retrieves the Javascript attribute for the first node matching the selector.

func KeyAction Uses

func KeyAction(keys string, opts ...KeyOption) Action

KeyAction will synthesize a keyDown, char, and keyUp event for each rune contained in keys along with any supplied key options.

Only well-known, "printable" characters will have char events synthesized.

Please see the chromedp/kb package for implementation details and the list of well-known keys.

func KeyActionNode Uses

func KeyActionNode(n *cdp.Node, keys string, opts ...KeyOption) Action

KeyActionNode dispatches a key event on a node.

func Location Uses

func Location(urlstr *string) Action

Location retrieves the document location.

func MatchedStyle Uses

func MatchedStyle(sel interface{}, style **css.GetMatchedStylesForNodeReturns, opts ...QueryOption) Action

MatchedStyle retrieves the matched style information for the first node matching the selector.

func MouseAction Uses

func MouseAction(typ input.MouseType, x, y int64, opts ...MouseOption) Action

MouseAction is a mouse action.

func MouseClickNode Uses

func MouseClickNode(n *cdp.Node, opts ...MouseOption) Action

MouseClickNode dispatches a mouse left button click event at the center of a specified node.

Note that the window will be scrolled if the node is not within the window's viewport.

func MouseClickXY Uses

func MouseClickXY(x, y int64, opts ...MouseOption) Action

MouseClickXY sends a left mouse button click (ie, mousePressed and mouseReleased event) at the X, Y location.

func Navigate(urlstr string) Action

Navigate navigates the current frame.

func NavigateBack() Action

NavigateBack navigates the current frame backwards in its history.

func NavigateForward() Action

NavigateForward navigates the current frame forwards in its history.

func NavigateToHistoryEntry(entryID int64) Action

NavigateToHistoryEntry is an action to navigate to the specified navigation entry.

func NavigationEntries(currentIndex *int64, entries *[]*page.NavigationEntry) Action

NavigationEntries is an action to retrieve the page's navigation history entries.

func NodeIDs Uses

func NodeIDs(sel interface{}, ids *[]cdp.NodeID, opts ...QueryOption) Action

NodeIDs retrieves the node IDs matching the selector.

func Nodes Uses

func Nodes(sel interface{}, nodes *[]*cdp.Node, opts ...QueryOption) Action

Nodes retrieves the document nodes matching the selector.

func OuterHTML Uses

func OuterHTML(sel interface{}, html *string, opts ...QueryOption) Action

OuterHTML retrieves the outer html of the first node matching the selector.

func Query Uses

func Query(sel interface{}, opts ...QueryOption) Action

Query is an action to query for document nodes match the specified sel and the supplied query options.

func QueryAfter Uses

func QueryAfter(sel interface{}, f func(context.Context, ...*cdp.Node) error, opts ...QueryOption) Action

QueryAfter is an action that will match the specified sel using the supplied query options, and after the visibility conditions of the query have been met, will execute f.

func Reload Uses

func Reload() Action

Reload reloads the current page.

func RemoveAttribute Uses

func RemoveAttribute(sel interface{}, name string, opts ...QueryOption) Action

RemoveAttribute removes the element attribute with name from the first node matching the selector.

func Reset Uses

func Reset(sel interface{}, opts ...QueryOption) Action

Reset is an action that resets the form of the first node matching the selector belongs to.

func Screenshot Uses

func Screenshot(sel interface{}, picbuf *[]byte, opts ...QueryOption) Action

Screenshot takes a screenshot of the first node matching the selector.

func ScrollIntoView Uses

func ScrollIntoView(sel interface{}, opts ...QueryOption) Action

ScrollIntoView scrolls the window to the first node matching the selector.

func SendKeys Uses

func SendKeys(sel interface{}, v string, opts ...QueryOption) Action

SendKeys synthesizes the key up, char, and down events as needed for the runes in v, sending them to the first node matching the selector.

Note: when selector matches a input[type="file"] node, then dom.SetFileInputFiles is used to set the upload path of the input node to v.

func SetAttributeValue Uses

func SetAttributeValue(sel interface{}, name, value string, opts ...QueryOption) Action

SetAttributeValue sets the element attribute with name to value for the first node matching the selector.

func SetAttributes Uses

func SetAttributes(sel interface{}, attributes map[string]string, opts ...QueryOption) Action

SetAttributes sets the element attributes for the first node matching the selector.

func SetJavascriptAttribute Uses

func SetJavascriptAttribute(sel interface{}, name, value string, opts ...QueryOption) Action

SetJavascriptAttribute sets the javascript attribute for the first node matching the selector.

func SetUploadFiles Uses

func SetUploadFiles(sel interface{}, files []string, opts ...QueryOption) Action

SetUploadFiles sets the files to upload (ie, for a input[type="file"] node) for the first node matching the selector.

func SetValue Uses

func SetValue(sel interface{}, value string, opts ...QueryOption) Action

SetValue sets the value of an element.

func Sleep Uses

func Sleep(d time.Duration) Action

Sleep is an empty action that calls time.Sleep with the specified duration.

Note: this is a temporary action definition for convenience, and will likely be marked for deprecation in the future, after the remaining Actions have been able to be written/tested.

func Stop Uses

func Stop() Action

Stop stops all navigation and pending resource retrieval.

func Submit Uses

func Submit(sel interface{}, opts ...QueryOption) Action

Submit is an action that submits the form of the first node matching the selector belongs to.

func Text Uses

func Text(sel interface{}, text *string, opts ...QueryOption) Action

Text retrieves the visible text of the first node matching the selector.

func Title Uses

func Title(title *string) Action

Title retrieves the document title.

Code:

ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()

ts := httptest.NewServer(writeHTML(`
<head>
	<title>fancy website title</title>
</head>
<body>
	<div id="content"></div>
</body>
	`))
defer ts.Close()

var title string
if err := chromedp.Run(ctx,
    chromedp.Navigate(ts.URL),
    chromedp.Title(&title),
); err != nil {
    panic(err)
}
fmt.Println(title)

Output:

fancy website title

func Value Uses

func Value(sel interface{}, value *string, opts ...QueryOption) Action

Value retrieves the value of the first node matching the selector.

func WaitEnabled Uses

func WaitEnabled(sel interface{}, opts ...QueryOption) Action

WaitEnabled waits until the selected element is enabled (does not have attribute 'disabled').

func WaitNotPresent Uses

func WaitNotPresent(sel interface{}, opts ...QueryOption) Action

WaitNotPresent waits until no elements match the specified selector.

func WaitNotVisible Uses

func WaitNotVisible(sel interface{}, opts ...QueryOption) Action

WaitNotVisible waits until the selected element is not visible.

func WaitReady Uses

func WaitReady(sel interface{}, opts ...QueryOption) Action

WaitReady waits until the element is ready (ie, loaded by chromedp).

func WaitSelected Uses

func WaitSelected(sel interface{}, opts ...QueryOption) Action

WaitSelected waits until the element is selected (has attribute 'selected').

func WaitVisible Uses

func WaitVisible(sel interface{}, opts ...QueryOption) Action

WaitVisible waits until the selected element is visible.

type ActionFunc Uses

type ActionFunc func(context.Context) error

ActionFunc is a adapter to allow the use of ordinary func's as an Action.

func (ActionFunc) Do Uses

func (f ActionFunc) Do(ctx context.Context) error

Do executes the func f using the provided context and frame handler.

type Allocator Uses

type Allocator interface {
    // Allocate creates a new browser. It can be cancelled via the provided
    // context, at which point all the resources used by the browser (such
    // as temporary directories) will be freed.
    Allocate(context.Context, ...BrowserOption) (*Browser, error)

    // Wait blocks until an allocator has freed all of its resources.
    // Cancelling the allocator context will already perform this operation,
    // so normally there's no need to call Wait directly.
    Wait()
}

An Allocator is responsible for creating and managing a number of browsers.

This interface abstracts away how the browser process is actually run. For example, an Allocator implementation may reuse browser processes, or connect to already-running browsers on remote machines.

type Browser Uses

type Browser struct {
    // LostConnection is closed when the websocket connection to Chrome is
    // dropped. This can be useful to make sure that Browser's context is
    // cancelled (and the handler stopped) once the connection has failed.
    LostConnection chan struct{}
    // contains filtered or unexported fields
}

Browser is the high-level Chrome DevTools Protocol browser manager, handling the browser process runner, WebSocket clients, associated targets, and network, page, and DOM events.

func NewBrowser Uses

func NewBrowser(ctx context.Context, urlstr string, opts ...BrowserOption) (*Browser, error)

NewBrowser creates a new browser. Typically, this function wouldn't be called directly, as the Allocator interface takes care of it.

func (*Browser) Execute Uses

func (b *Browser) Execute(ctx context.Context, method string, params easyjson.Marshaler, res easyjson.Unmarshaler) error

type BrowserOption Uses

type BrowserOption func(*Browser)

BrowserOption is a browser option.

func WithBrowserDebugf Uses

func WithBrowserDebugf(f func(string, ...interface{})) BrowserOption

WithBrowserDebugf is a browser option to specify a func to log actual websocket messages.

func WithBrowserErrorf Uses

func WithBrowserErrorf(f func(string, ...interface{})) BrowserOption

WithBrowserErrorf is a browser option to specify a func to receive error logging.

func WithBrowserLogf Uses

func WithBrowserLogf(f func(string, ...interface{})) BrowserOption

WithBrowserLogf is a browser option to specify a func to receive general logging.

func WithConsolef Uses

func WithConsolef(f func(string, ...interface{})) BrowserOption

WithConsolef is a browser option to specify a func to receive chrome log events.

Note: NOT YET IMPLEMENTED.

func WithDialTimeout Uses

func WithDialTimeout(d time.Duration) BrowserOption

WithDialTimeout is a browser option to specify the timeout when dialing a browser's websocket address. The default is ten seconds; use a zero duration to not use a timeout.

type Conn Uses

type Conn struct {
    // contains filtered or unexported fields
}

Conn implements Transport with a gobwas/ws websocket connection.

func DialContext Uses

func DialContext(ctx context.Context, urlstr string, opts ...DialOption) (*Conn, error)

DialContext dials the specified websocket URL using gobwas/ws.

func (*Conn) Close Uses

func (c *Conn) Close() error

Close satisfies the io.Closer interface.

func (*Conn) Read Uses

func (c *Conn) Read(_ context.Context, msg *cdproto.Message) error

Read reads the next message.

func (*Conn) Write Uses

func (c *Conn) Write(_ context.Context, msg *cdproto.Message) error

Write writes a message.

type Context Uses

type Context struct {
    // Allocator is used to create new browsers. It is inherited from the
    // parent context when using NewContext.
    Allocator Allocator

    // Browser is the browser being used in the context. It is inherited
    // from the parent context when using NewContext.
    Browser *Browser

    // Target is the target to run actions (commands) against. It is not
    // inherited from the parent context, and typically each context will
    // have its own unique Target pointing to a separate browser tab (page).
    Target *Target
    // contains filtered or unexported fields
}

Context is attached to any context.Context which is valid for use with Run.

func FromContext Uses

func FromContext(ctx context.Context) *Context

FromContext extracts the Context data stored inside a context.Context.

type ContextOption Uses

type ContextOption func(*Context)

ContextOption is a context option.

func WithBrowserOption Uses

func WithBrowserOption(opts ...BrowserOption) ContextOption

WithBrowserOption allows passing a number of browser options to the allocator when allocating a new browser. As such, this context option can only be used when NewContext is allocating a new browser.

func WithDebugf Uses

func WithDebugf(f func(string, ...interface{})) ContextOption

WithDebugf is a shortcut for WithBrowserOption(WithBrowserDebugf(f)).

func WithErrorf Uses

func WithErrorf(f func(string, ...interface{})) ContextOption

WithErrorf is a shortcut for WithBrowserOption(WithBrowserErrorf(f)).

func WithLogf Uses

func WithLogf(f func(string, ...interface{})) ContextOption

WithLogf is a shortcut for WithBrowserOption(WithBrowserLogf(f)).

func WithTargetID Uses

func WithTargetID(id target.ID) ContextOption

WithTargetID sets up a context to be attached to an existing target, instead of creating a new one.

type DialOption Uses

type DialOption func(*Conn)

DialOption is a dial option.

func WithConnDebugf Uses

func WithConnDebugf(f func(string, ...interface{})) DialOption

WithConnDebugf is a dial option to set a protocol logger.

type Error Uses

type Error string

Error is a chromedp error.

const (
    // ErrInvalidWebsocketMessage is the invalid websocket message.
    ErrInvalidWebsocketMessage Error = "invalid websocket message"

    // ErrInvalidDimensions is the invalid dimensions error.
    ErrInvalidDimensions Error = "invalid dimensions"

    // ErrNoResults is the no results error.
    ErrNoResults Error = "no results"

    // ErrHasResults is the has results error.
    ErrHasResults Error = "has results"

    // ErrNotVisible is the not visible error.
    ErrNotVisible Error = "not visible"

    // ErrVisible is the visible error.
    ErrVisible Error = "visible"

    // ErrDisabled is the disabled error.
    ErrDisabled Error = "disabled"

    // ErrNotSelected is the not selected error.
    ErrNotSelected Error = "not selected"

    // ErrInvalidBoxModel is the invalid box model error.
    ErrInvalidBoxModel Error = "invalid box model"

    // ErrChannelClosed is the channel closed error.
    ErrChannelClosed Error = "channel closed"

    // ErrInvalidTarget is the invalid target error.
    ErrInvalidTarget Error = "invalid target"

    // ErrInvalidContext is the invalid context error.
    ErrInvalidContext Error = "invalid context"
)

Error types.

func (Error) Error Uses

func (err Error) Error() string

Error satisfies the error interface.

type EvaluateOption Uses

type EvaluateOption func(*runtime.EvaluateParams) *runtime.EvaluateParams

EvaluateOption is the type for script evaluation options.

func EvalObjectGroup Uses

func EvalObjectGroup(objectGroup string) EvaluateOption

EvalObjectGroup is a evaluate option to set the object group.

type ExecAllocator Uses

type ExecAllocator struct {
    // contains filtered or unexported fields
}

ExecAllocator is an Allocator which starts new browser processes on the host machine.

Code:

dir, err := ioutil.TempDir("", "chromedp-example")
if err != nil {
    panic(err)
}
defer os.RemoveAll(dir)

opts := []chromedp.ExecAllocatorOption{
    chromedp.NoFirstRun,
    chromedp.NoDefaultBrowserCheck,
    chromedp.Headless,
    chromedp.DisableGPU,
    chromedp.UserDataDir(dir),
}

allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()

// also set up a custom logger
taskCtx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf))
defer cancel()

// ensure that the browser process is started
if err := chromedp.Run(taskCtx); err != nil {
    panic(err)
}

path := filepath.Join(dir, "DevToolsActivePort")
bs, err := ioutil.ReadFile(path)
if err != nil {
    panic(err)
}
lines := bytes.Split(bs, []byte("\n"))
fmt.Printf("DevToolsActivePort has %d lines\n", len(lines))

Output:

DevToolsActivePort has 2 lines

func (*ExecAllocator) Allocate Uses

func (a *ExecAllocator) Allocate(ctx context.Context, opts ...BrowserOption) (*Browser, error)

Allocate satisfies the Allocator interface.

func (*ExecAllocator) Wait Uses

func (a *ExecAllocator) Wait()

Wait satisfies the Allocator interface.

type ExecAllocatorOption Uses

type ExecAllocatorOption func(*ExecAllocator)

ExecAllocatorOption is a exec allocator option.

func ExecPath Uses

func ExecPath(path string) ExecAllocatorOption

ExecPath returns an ExecAllocatorOption which uses the given path to execute browser processes. The given path can be an absolute path to a binary, or just the name of the program to find via exec.LookPath.

func Flag Uses

func Flag(name string, value interface{}) ExecAllocatorOption

Flag is a generic command line option to pass a flag to Chrome. If the value is a string, it will be passed as --name=value. If it's a boolean, it will be passed as --name if value is true.

func ProxyServer Uses

func ProxyServer(proxy string) ExecAllocatorOption

ProxyServer is the command line option to set the outbound proxy server.

func UserAgent Uses

func UserAgent(userAgent string) ExecAllocatorOption

UserAgent is the command line option to set the default User-Agent header.

func UserDataDir Uses

func UserDataDir(dir string) ExecAllocatorOption

UserDataDir is the command line option to set the user data dir.

Note: set this option to manually set the profile directory used by Chrome. When this is not set, then a default path will be created in the /tmp directory.

func WindowSize Uses

func WindowSize(width, height int) ExecAllocatorOption

WindowSize is the command line option to set the initial window size.

type KeyOption Uses

type KeyOption func(*input.DispatchKeyEventParams) *input.DispatchKeyEventParams

KeyOption is a key action option.

func KeyModifiers Uses

func KeyModifiers(modifiers ...input.Modifier) KeyOption

KeyModifiers is a key action option to add additional modifiers on the key press.

type MouseOption Uses

type MouseOption func(*input.DispatchMouseEventParams) *input.DispatchMouseEventParams

MouseOption is a mouse action option.

func Button Uses

func Button(btn string) MouseOption

Button is a mouse action option to set the button to click from a string.

func ButtonModifiers Uses

func ButtonModifiers(modifiers ...input.Modifier) MouseOption

ButtonModifiers is a mouse action option to add additional input modifiers for a button click.

func ButtonType Uses

func ButtonType(button input.ButtonType) MouseOption

ButtonType is a mouse action option to set the button to click.

func ClickCount Uses

func ClickCount(n int) MouseOption

ClickCount is a mouse action option to set the click count.

type QueryOption Uses

type QueryOption func(*Selector)

QueryOption is a element query selector option.

func After Uses

func After(f func(context.Context, ...*cdp.Node) error) QueryOption

After is a query option to set a func that will be executed after the wait has succeeded.

func AtLeast Uses

func AtLeast(n int) QueryOption

AtLeast is a query option to wait until at least n elements are returned from the query selector.

func ByFunc Uses

func ByFunc(f func(context.Context, *cdp.Node) ([]cdp.NodeID, error)) QueryOption

ByFunc is a query option to set the func used to select elements.

func WaitFunc Uses

func WaitFunc(wait func(context.Context, *cdp.Frame, ...cdp.NodeID) ([]*cdp.Node, error)) QueryOption

WaitFunc is a query option to set a custom wait func.

type RemoteAllocator Uses

type RemoteAllocator struct {
    // contains filtered or unexported fields
}

RemoteAllocator is an Allocator which connects to an already running Chrome process via a websocket URL.

func (*RemoteAllocator) Allocate Uses

func (a *RemoteAllocator) Allocate(ctx context.Context, opts ...BrowserOption) (*Browser, error)

Allocate satisfies the Allocator interface.

func (*RemoteAllocator) Wait Uses

func (a *RemoteAllocator) Wait()

Wait satisfies the Allocator interface.

type Selector Uses

type Selector struct {
    // contains filtered or unexported fields
}

Selector holds information pertaining to an element query select action.

func (*Selector) Do Uses

func (s *Selector) Do(ctx context.Context) error

Do satisfies the Action interface.

type Target Uses

type Target struct {
    SessionID target.SessionID
    TargetID  target.ID
    // contains filtered or unexported fields
}

Target manages a Chrome DevTools Protocol target.

func (*Target) Execute Uses

func (t *Target) Execute(ctx context.Context, method string, params easyjson.Marshaler, res easyjson.Unmarshaler) error

type TargetOption Uses

type TargetOption func(*Target)

type Tasks Uses

type Tasks []Action

Tasks is a sequential list of Actions that can be used as a single Action.

func (Tasks) Do Uses

func (t Tasks) Do(ctx context.Context) error

Do executes the list of Actions sequentially, using the provided context and frame handler.

type Transport Uses

type Transport interface {
    Read(context.Context, *cdproto.Message) error
    Write(context.Context, *cdproto.Message) error
    io.Closer
}

Transport is the common interface to send/receive messages to a target.

Directories

PathSynopsis
kbPackage kb provides keyboard mappings for Chrome DOM Keys for use with input events.

Package chromedp imports 35 packages (graph) and is imported by 2 packages. Updated 2019-05-23. Refresh now. Tools for package owners.