LightCDP.Page (light_cdp v0.2.1)

Copy Markdown View Source

Page interactions via native CDP methods.

All functions return {:ok, result}, :ok, or {:error, exception} where exception is one of:

  • %LightCDP.ElementNotFoundError{} — selector matched no element
  • %LightCDP.TimeoutError{} — operation exceeded its deadline
  • %LightCDP.JavaScriptError{} — JS expression threw an exception
  • %LightCDP.CDPError{} — CDP protocol error

Native CDP methods used

FunctionCDP methods
click/3DOM.querySelector -> DOM.getBoxModel -> Input.dispatchMouseEvent
fill/4DOM.resolveNode -> Runtime.callFunctionOn -> Input.insertText
content/1DOM.getDocument -> DOM.getOuterHTML
navigate/3Page.navigate + Page.loadEventFired event
evaluate/3Runtime.evaluate
url/1Runtime.evaluate (no native equivalent)
submit/4Runtime.evaluate (no native equivalent for form submission)

Timeouts

All functions accept a timeout: option in milliseconds.

DefaultFunctions
30,000 msnavigate, wait_for_navigation, submit
15,000 msevaluate, click, fill, wait_for_selector

Summary

Functions

Clicks an element matching selector.

Returns the full page HTML including doctype.

Evaluates a JavaScript expression and returns the result.

Fills an input element matching selector with value.

Navigates to url and waits for the page to load.

Captures a screenshot of the page as a PNG binary.

Fills form fields, submits the form, and waits for navigation.

Returns the current page URL.

Registers an event waiter, calls fun, then waits for a Page.loadEventFired event.

Polls the DOM until an element matching selector appears.

Functions

click(page, selector, opts \\ [])

Clicks an element matching selector.

Uses native CDP: finds the element via DOM.querySelector, computes its center point from DOM.getBoxModel, and dispatches mouse events via Input.dispatchMouseEvent.

Options

  • :timeout - milliseconds (default: 15_000)

Example

:ok = LightCDP.Page.click(page, "#submit-btn")
{:error, %LightCDP.ElementNotFoundError{selector: "#nope"}} = LightCDP.Page.click(page, "#nope")

content(page)

Returns the full page HTML including doctype.

Uses native DOM.getOuterHTML instead of JavaScript.

Example

{:ok, html} = LightCDP.Page.content(page)

evaluate(page, expression, opts \\ [])

Evaluates a JavaScript expression and returns the result.

Options

  • :timeout - milliseconds (default: 15_000)

Examples

{:ok, "Example Domain"} = LightCDP.Page.evaluate(page, "document.title")
{:ok, 42} = LightCDP.Page.evaluate(page, "21 * 2")
{:error, %LightCDP.JavaScriptError{}} = LightCDP.Page.evaluate(page, "throw new Error('boom')")

fill(page, selector, value, opts \\ [])

Fills an input element matching selector with value.

Uses native CDP: resolves the DOM node, focuses it via Runtime.callFunctionOn, clears existing content, then types via Input.insertText.

Options

  • :timeout - milliseconds (default: 15_000)

Example

:ok = LightCDP.Page.fill(page, "#email", "user@example.com")

screenshot(page, opts \\ [])

Captures a screenshot of the page as a PNG binary.

Uses native Page.captureScreenshot.

Options

  • :timeout - milliseconds (default: 15_000)

Example

{:ok, png} = LightCDP.Page.screenshot(page)
File.write!("screenshot.png", png)

submit(page, form_selector, fields \\ %{}, opts \\ [])

Fills form fields, submits the form, and waits for navigation.

fields is a map of %{selector => value} pairs to fill before submitting.

Options

  • :timeout - milliseconds (default: 30_000)

Example

:ok = LightCDP.Page.submit(page, "#login-form", %{
  "#email" => "user@example.com",
  "#password" => "secret"
})

# Submit with no fields
:ok = LightCDP.Page.submit(page, "form")

url(page)

Returns the current page URL.

Example

{:ok, "https://example.com/"} = LightCDP.Page.url(page)

wait_for_navigation(page, fun, opts \\ [])

Registers an event waiter, calls fun, then waits for a Page.loadEventFired event.

If fun returns {:error, _}, short-circuits immediately without waiting.

Options

  • :timeout - milliseconds (default: 30_000)

Example

:ok = LightCDP.Page.wait_for_navigation(page, fn ->
  LightCDP.Page.click(page, "a.next-page")
end)

wait_for_selector(page, selector, opts \\ [])

Polls the DOM until an element matching selector appears.

Returns :ok when found, {:error, :timeout} if it doesn't appear within the timeout.

Options

  • :timeout - milliseconds (default: 15_000)
  • :interval - polling interval in milliseconds (default: 100)

Example

:ok = LightCDP.Page.wait_for_selector(page, ".search-results", timeout: 5_000)