Documentation
¶
Overview ¶
Package browserx drives a Chromium browser over the DevTools protocol, with the behaviour of Playwright's page API where callers depend on it: auto-waiting for visible/enabled/editable elements, Playwright's key names and selector extensions, load states and screenshots. It is built on chromedp and needs no Node runtime.
Index ¶
- Constants
- func EndpointAvailable(ctx context.Context, endpoint string) bool
- func FirstLine(s string) string
- func HasKey(s string) bool
- func StartDetached(executable string, args, env []string) error
- func WaitForEndpoint(ctx context.Context, endpoint string, timeout time.Duration) (bool, error)
- type Box
- type Browser
- func (b *Browser) Close()
- func (b *Browser) Connected() bool
- func (b *Browser) Cookies(ctx context.Context) ([]Cookie, error)
- func (b *Browser) Done() <-chan struct{}
- func (b *Browser) NewPage(ctx context.Context) (*Page, error)
- func (b *Browser) OnNewPage(fn func(*Page))
- func (b *Browser) Pages(ctx context.Context) ([]*Page, error)
- type Cookie
- type ExceptionError
- type GotoOptions
- type Keyboard
- func (k *Keyboard) Down(ctx context.Context, key string) error
- func (k *Keyboard) InsertText(ctx context.Context, text string) error
- func (k *Keyboard) Press(ctx context.Context, combo string, delay time.Duration) error
- func (k *Keyboard) Type(ctx context.Context, text string, delay time.Duration) error
- func (k *Keyboard) Up(ctx context.Context, key string) error
- type Mouse
- type Page
- func (p *Page) AddInitScript(ctx context.Context, source string) error
- func (p *Page) Close(ctx context.Context) error
- func (p *Page) Closed() bool
- func (p *Page) Content(ctx context.Context) (string, error)
- func (p *Page) Eval(ctx context.Context, expression string) (json.RawMessage, error)
- func (p *Page) EvalJSON(ctx context.Context, expression string, out any) error
- func (p *Page) EvalWithSelectors(ctx context.Context, body string, out any, args ...any) error
- func (p *Page) Fill(ctx context.Context, selector, value string, timeout time.Duration) error
- func (p *Page) Goto(ctx context.Context, url string, o GotoOptions) (int, error)
- func (p *Page) ID() string
- func (p *Page) PressOn(ctx context.Context, selector, key string, timeout time.Duration) error
- func (p *Page) PressSequentially(ctx context.Context, selector, text string, delay, timeout time.Duration) error
- func (p *Page) Reload(ctx context.Context, o GotoOptions) error
- func (p *Page) Screenshot(ctx context.Context, o ScreenshotOptions) ([]byte, error)
- func (p *Page) ScrollAndBox(ctx context.Context, selector string) (box *Box, found bool, err error)
- func (p *Page) ScrollAndBoxExpr(ctx context.Context, elementExpr string) (box *Box, found bool, err error)
- func (p *Page) Title(ctx context.Context) (string, error)
- func (p *Page) URL(ctx context.Context) (string, error)
- func (p *Page) WaitForLoadState(ctx context.Context, state string, timeout time.Duration) error
- func (p *Page) WaitForSelector(ctx context.Context, selector string, timeout time.Duration) error
- type ScreenshotOptions
- type StrictError
- type TimeoutError
Constants ¶
const DefaultTimeout = 30 * time.Second
DefaultTimeout is Playwright's default for navigations and actions.
Variables ¶
This section is empty.
Functions ¶
func EndpointAvailable ¶
EndpointAvailable reports whether a DevTools HTTP endpoint such as http://127.0.0.1:9222 answers /json/version.
func FirstLine ¶
FirstLine returns the first line of an error message, for callers that show only the headline of a page-side exception.
func HasKey ¶
HasKey reports whether the US layout can press s as one key (a single character or a key name); other text must be inserted instead.
func StartDetached ¶
StartDetached starts a program in its own session so it outlives the caller and is not killed with its process group. Its output is discarded. env replaces the environment when non-nil. The process is released, never waited on.
Types ¶
type Box ¶
type Box struct {
X, Y, Width, Height float64
}
Box is an element's bounding box in main-frame viewport coordinates.
type Browser ¶
type Browser struct {
// contains filtered or unexported fields
}
Browser is a connection to a running Chromium (its default browser context).
func Connect ¶
Connect attaches to the browser behind a DevTools endpoint (http://host:port or a ws:// URL). It opens no tab. Close detaches without closing the browser.
func (*Browser) Close ¶
func (b *Browser) Close()
Close drops the connection and leaves the browser, and its tabs, running.
func (*Browser) Cookies ¶
Cookies returns every cookie of the default browser context, shaped like Playwright's context.cookies().
func (*Browser) Done ¶
func (b *Browser) Done() <-chan struct{}
Done is closed when the connection to the browser is gone.
type Cookie ¶
type Cookie struct {
Name string `json:"name"`
Value string `json:"value"`
Domain string `json:"domain"`
Path string `json:"path"`
Expires float64 `json:"expires"`
HTTPOnly bool `json:"httpOnly"`
Secure bool `json:"secure"`
SameSite string `json:"sameSite"`
}
Cookie is the cookie shape Playwright returns, in its key order.
type ExceptionError ¶
type ExceptionError struct{ Message string }
ExceptionError is a JavaScript exception thrown by evaluated code.
func (*ExceptionError) Error ¶
func (e *ExceptionError) Error() string
type GotoOptions ¶
GotoOptions tune Goto. WaitUntil is commit, domcontentloaded, load or networkidle (default load, as in Playwright).
type Keyboard ¶
type Keyboard struct {
// contains filtered or unexported fields
}
Keyboard dispatches key events with Playwright's key names and modifier bookkeeping.
func (*Keyboard) InsertText ¶
InsertText inserts text as if pasted: no key events are generated.
func (*Keyboard) Press ¶
Press presses and releases a key or a combination such as "Control+A", holding the modifiers in between. delay is the pause between down and up.
type Mouse ¶
type Mouse struct {
// contains filtered or unexported fields
}
Mouse dispatches mouse events at tracked coordinates, like Playwright's page.mouse. It starts at (0, 0).
type Page ¶
Page is one tab.
func (*Page) AddInitScript ¶
AddInitScript runs source in every document the tab loads from now on, before the page's own scripts, and in the document that is already loaded.
func (*Page) Content ¶
Content is the page's HTML including its doctype, like Playwright's page.content().
func (*Page) Eval ¶
Eval evaluates a JavaScript expression in the page's main world, waits for a returned promise, and returns the result by value as JSON. A result of undefined comes back as the JSON value null.
func (*Page) EvalWithSelectors ¶
EvalWithSelectors evaluates the statements in body as an async function body with L (the selector helper: queryAll, visible, ...) and the JSON encoded args in scope. The body returns the value to report, e.g.
body: `return L.queryAll(args[0]).length`
func (*Page) Fill ¶
Fill sets the value of the single element matching selector with Playwright's locator.fill semantics: it waits for the element to be attached, visible, enabled and editable, then selects its content and replaces it. An empty value clears the field.
func (*Page) Goto ¶
Goto navigates and waits for the load state, returning the main document's HTTP status (0 for a navigation without a network response, such as a hash change or about:blank).
func (*Page) PressOn ¶
PressOn focuses the single element matching selector and presses a key, like locator.press.
func (*Page) PressSequentially ¶
func (p *Page) PressSequentially(ctx context.Context, selector, text string, delay, timeout time.Duration) error
PressSequentially focuses the single element matching selector and types text into it key by key, like locator.pressSequentially.
func (*Page) Reload ¶
func (p *Page) Reload(ctx context.Context, o GotoOptions) error
Reload reloads the tab and waits like Goto.
func (*Page) Screenshot ¶
Screenshot captures the viewport, the full page, or one element. When the selector matches nothing the whole page is captured instead.
func (*Page) ScrollAndBox ¶
ScrollAndBox scrolls the first element matching selector into view (when it is not already) and returns its bounding box. found is false when nothing matches; box is nil when the element has no layout.
func (*Page) ScrollAndBoxExpr ¶
func (p *Page) ScrollAndBoxExpr(ctx context.Context, elementExpr string) (box *Box, found bool, err error)
ScrollAndBoxExpr is ScrollAndBox for an element chosen by a JavaScript expression (with L in scope) that yields an Element or undefined.
func (*Page) WaitForLoadState ¶
WaitForLoadState waits until the current document reached the state (load, domcontentloaded or networkidle).
type ScreenshotOptions ¶
type ScreenshotOptions struct {
Format string
Quality int
FullPage bool
// Selector captures one element, scrolled into view first.
Selector string
}
ScreenshotOptions tune Screenshot. Format is "png" (default) or "jpeg".
type StrictError ¶
StrictError reports a selector that matched several elements where one was required, as Playwright's strict mode does.
func (*StrictError) Error ¶
func (e *StrictError) Error() string
type TimeoutError ¶
TimeoutError reports an operation that did not finish in time, with Playwright's wording ("page.goto: Timeout 30000ms exceeded.").
func (*TimeoutError) Error ¶
func (e *TimeoutError) Error() string