A page (tab) handle and the operations you run against it.
A CDPEx.Page is a lightweight struct — not a process — holding the page's
CDPEx.Connection pid and target id. Operations are functions over that
connection, so the OTP properties (supervision, crash isolation) live in the
connection/browser layer while page calls stay ergonomic.
Obtain one with CDPEx.new_page/2. If the underlying page dies (navigation to
a new target, a crash), operations return {:error, :noproc} and you should
open a fresh page.
Operations
- Navigation —
navigate/3,wait_for_navigation/2 - Evaluation —
evaluate/3,call_function/4,html/2 - Waiting —
wait_for_selector/3,wait_for_function/3 - Elements —
text/3,attribute/4,visible?/3,click/3 - Capture —
screenshot/2,pdf/2 - Emulation —
set_viewport/4,set_user_agent/3 - Cookies & headers —
cookies/2,set_cookies/3,clear_cookies/2,set_extra_headers/3
Summary
Functions
Returns attribute name of the first element matching css, or nil when the
element or attribute is absent.
Calls a JavaScript function with args and returns its value.
Clears all browser cookies. Lazily enables Network. Options: :timeout.
Clicks the first element matching css (a synthetic JS .click()).
Returns all browser cookies as a list of CDP cookie maps.
Evaluates a JavaScript expression and returns its value (returnByValue).
Returns the page's full serialized HTML (document.documentElement.outerHTML).
Navigates to url and (by default) waits until the network is almost idle.
Renders the page to PDF (Page.printToPDF).
Captures a PNG screenshot.
Sets cookies. Each is a CDP CookieParam map — at least "name", "value",
and a "url" or "domain". Lazily enables Network. Options: :timeout.
Sets extra HTTP headers sent with every subsequent request on this page.
Overrides the page's User-Agent (Emulation.setUserAgentOverride).
Overrides the viewport via Emulation.setDeviceMetricsOverride.
Returns the textContent of the first element matching css, or nil when
no element matches.
Returns {:ok, true} when the first element matching css is rendered and
visible (has layout boxes, not display: none / visibility: hidden),
{:ok, false} otherwise — including when no element matches.
Polls a JavaScript expression until it is truthy, or timeout elapses.
Waits for a navigation lifecycle milestone, without issuing a navigation.
Polls until css matches an element, or timeout elapses.
Types
Functions
@spec attribute(t(), String.t(), String.t(), keyword()) :: {:ok, String.t() | nil} | {:error, term()}
Returns attribute name of the first element matching css, or nil when the
element or attribute is absent.
Calls a JavaScript function with args and returns its value.
function_declaration is a JS function expression (e.g. "(a, b) => a + b").
It is interpolated into the page script verbatim, so treat it as trusted
code — never build it from untrusted input. args, by contrast, are JSON-encoded
(not string-interpolated) before being applied, so passing data values through
them is safe. A thrown exception is {:error, {:evaluate_exception, details}};
non-serializable args return {:error, {:invalid_args, reason}}.
Options: :timeout (default 15_000), :await_promise (default false).
Clears all browser cookies. Lazily enables Network. Options: :timeout.
Clicks the first element matching css (a synthetic JS .click()).
Returns :ok, or {:error, {:selector_not_found, css}} when nothing matches.
Returns all browser cookies as a list of CDP cookie maps.
Lazily enables the Network domain. Options: :timeout (default 10_000).
Evaluates a JavaScript expression and returns its value (returnByValue).
A thrown JS exception is {:error, {:evaluate_exception, details}}.
Options: :timeout (default 15_000), :await_promise (default false).
Returns the page's full serialized HTML (document.documentElement.outerHTML).
Renders the page to PDF (Page.printToPDF).
Returns {:ok, data} where data is the PDF bytes — or, when :path is
given, the written file path (also a binary). Options: :path, :landscape
(default false), :print_background (default true), :timeout (default 30_000).
Captures a PNG screenshot.
Returns {:ok, data} where data is the PNG bytes — or, when :path is
given, the written file path (also a binary).
Options: :path (write to file), :full_page (capture beyond the viewport,
default false), :timeout (default 30_000).
Sets cookies. Each is a CDP CookieParam map — at least "name", "value",
and a "url" or "domain". Lazily enables Network. Options: :timeout.
@spec set_extra_headers(t(), %{optional(String.t()) => String.t()}, keyword()) :: :ok | {:error, term()}
Sets extra HTTP headers sent with every subsequent request on this page.
headers is a map of header name => value; set them before navigating for
them to apply to that navigation. Lazily enables Network. Options: :timeout.
Overrides the page's User-Agent (Emulation.setUserAgentOverride).
Options: :timeout (default 10_000).
@spec set_viewport(t(), pos_integer(), pos_integer(), keyword()) :: :ok | {:error, term()}
Overrides the viewport via Emulation.setDeviceMetricsOverride.
width/height are CSS pixels. Options: :device_scale_factor (default 1),
:mobile (default false), :timeout. Returns :ok.
Returns the textContent of the first element matching css, or nil when
no element matches.
Returns {:ok, true} when the first element matching css is rendered and
visible (has layout boxes, not display: none / visibility: hidden),
{:ok, false} otherwise — including when no element matches.
Polls a JavaScript expression until it is truthy, or timeout elapses.
The expression is coerced with !!(...), so JS truthiness applies. Returns
:ok, {:error, :timeout}, or {:error, reason} if a non-transient evaluate
error occurs (e.g. a thrown exception or a dropped connection). Options:
:timeout (default 5_000), :interval (poll interval ms, default 100).
Polls until css matches an element, or timeout elapses.
Returns :ok, {:error, :timeout}, or {:error, reason} if a non-transient
evaluate error occurs (e.g. the connection drops). Options: :timeout
(default 5_000), :interval (poll interval ms, default 100).