View Source Playwright.Page (playwright v1.44.0-alpha.4)

Page provides methods to interact with a single tab in a Playwright.Browser, or an extension background page in Chromium.

One Playwright.Browser instance might have multiple Page instances.

Example

Create a page, navigate it to a URL, and save a screenshot:

page = Browser.new_page(browser)
resp = Page.goto(page, "https://example.com")

Page.screenshot(page, %{path: "screenshot.png"})
:ok = Page.close(page)

The Page module is capable of handling various emitted events (described below).

Example

Log a message for a single page load event (WIP: once is not yet implemented):

Page.once(page, :load, fn e ->
  IO.puts("page loaded!")
end)

Unsubscribe from events with the remove_lstener function (WIP: remove_listener is not yet implemented):

def log_request(request) do
  IO.inspect(label: "A request was made")
end

Page.on(page, :request, fn e ->
  log_request(e.pages.request)
end)

Page.remove_listener(page, log_request)

Summary

Functions

Adds a script to be evaluated before other scripts.

Closes the Page.

Get the Playwright.BrowserContext that the page belongs to.

Adds a function called param:name on the window object of every frame in this page.

Adds a function called param:name on the window object of every frame in the page.

Allows locating elements that contain given text.

A shortcut for the main frame's Playwright.Frame.hover/2.

Optional callback implementation for Playwright.SDK.ChannelOwner.init/2.

Reloads the current page.

Types

@type dimensions() :: map()
@type expression() :: binary()
@type function_or_options() :: (... -> any()) | options() | nil
@type options() :: map()
@type selector() :: binary()
@type serializable() :: any()
@type t() :: %Playwright.Page{
  bindings: term(),
  guid: term(),
  initializer: term(),
  is_closed: term(),
  listeners: term(),
  main_frame: term(),
  owned_context: term(),
  parent: term(),
  routes: term(),
  session: term(),
  type: term()
}

%Playwright.Page{}

Functions

Link to this function

add_init_script(page, script)

View Source
@spec add_init_script(t(), binary() | map()) :: :ok

Adds a script to be evaluated before other scripts.

The script is evaluated in the following scenarios:

  • Whenever the page is navigated.
  • Whenever a child frame is attached or navigated. In this case, the script is evaluated in the context of the newly attached frame.

The script is evaluated after the document is created but before any of its scripts are run. This is useful to amend the JavaScript environment, e.g. to seed Math.random.

Returns

  • :ok

Arguments

key/nametypedescription
scriptparambinary() or map()As binary(): an inlined script to be evaluated; As %{path: path}: a path to a JavaScript file.

Example

Overriding Math.random before the page loads:

# preload.js
Math.random = () => 42;

Page.add_init_script(page, %{path: "preload.js"})

Notes

While the official Node.js Playwright implementation supports an optional param: arg for this function, the official Python implementation does not. This implementation matches the Python for now.

The order of evaluation of multiple scripts installed via Playwright.BrowserContext.add_init_script/2 and Playwright.Page.add_init_script/2 is not defined.

Link to this function

click(page, selector, options \\ %{})

View Source
@spec click(t(), binary(), options()) :: :ok
Link to this function

close(page, options \\ %{})

View Source
@spec close(t(), options()) :: :ok

Closes the Page.

If the Page has an "owned context" (1-to-1 co-dependency with a Playwright.BrowserContext), that context is closed as well.

If option: run_before_unload is false, does not run any unload handlers and waits for the page to be closed. If option: run_before_unload is true the function will run unload handlers, but will not wait for the page to close. By default, Playwright.Page.close/1 does not run :beforeunload handlers.

Returns

  • :ok

Arguments

key/nametypedescription
run_before_unloadoptionboolean()Whether to run the before unload page handlers. (default: false)

NOTE

if option: run_before_unload is passed as true, a :beforeunload dialog might be summoned and should be handled manually via Playwright.Page.on/3.

@spec context(t()) :: Playwright.BrowserContext.t()

Get the Playwright.BrowserContext that the page belongs to.

Link to this function

dblclick(page, selector, options \\ %{})

View Source
@spec dblclick(t(), binary(), options()) :: :ok

A shortcut for the main frame's Playwright.Frame.dblclick/3.

Link to this function

dispatch_event(page, selector, type, event_init \\ nil, options \\ %{})

View Source
@spec dispatch_event(
  t(),
  binary(),
  atom() | binary(),
  Playwright.Frame.evaluation_argument(),
  options()
) :: :ok

A shortcut for the main frame's Playwright.Frame.dispatch_event/5.

Link to this function

drag_and_drop(page, source, target, options \\ %{})

View Source
@spec drag_and_drop(t(), binary(), binary(), options()) :: t()
Link to this function

eval_on_selector(page, selector, expression, arg \\ nil, options \\ %{})

View Source
@spec eval_on_selector(t(), binary(), binary(), term(), map()) :: term()
Link to this function

evaluate(page, expression, arg \\ nil)

View Source
@spec evaluate(t(), expression(), any()) :: serializable()
Link to this function

evaluate_handle(page, expression, arg \\ nil)

View Source
@spec evaluate_handle(t(), expression(), any()) :: serializable()
Link to this function

expect_event(page, event, options \\ %{}, trigger \\ nil)

View Source
Link to this function

expose_binding(page, name, callback, options \\ %{})

View Source
@spec expose_binding(t(), binary(), function(), options()) :: t()

Adds a function called param:name on the window object of every frame in this page.

When called, the function executes param:callback and resolves to the return value of the callback.

The first argument to the callback function includes the following details about the caller:

%{
  context: %Playwright.BrowserContext{},
  frame:   %Playwright.Frame{},
  page:    %Playwright.Page{}
}

See Playwright.BrowserContext.expose_binding/4 for a similar, context-scoped version.

Link to this function

expose_function(page, name, callback)

View Source
@spec expose_function(t(), String.t(), function()) :: t()

Adds a function called param:name on the window object of every frame in the page.

When called, the function executes param:callback and resolves to the return value of the callback.

See Playwright.BrowserContext.expose_function/3 for a similar, context-scoped version.

Link to this function

fill(page, selector, value, options \\ %{})

View Source
@spec fill(t(), binary(), binary(), options()) :: :ok
Link to this function

focus(page, selector, options \\ %{})

View Source
@spec focus(t(), binary(), options()) :: :ok

A shortcut for the main frame's Playwright.Frame.focus/3.

@spec frames(t()) :: [Playwright.Frame.t()]
Link to this function

get_attribute(page, selector, name, options \\ %{})

View Source
@spec get_attribute(t(), binary(), binary(), map()) :: binary() | nil
Link to this function

get_by_text(page, text, options \\ %{})

View Source
@spec get_by_text(t(), binary(), %{optional(:exact) => boolean()}) ::
  Playwright.Locator.t() | nil

Allows locating elements that contain given text.

Arguments

key/nametypedescription
textparambinary()Text to locate the element for.
:exactoptionboolean()Whether to find an exact match: case-sensitive and whole-string. Default to false. Ignored when locating by a regular expression. Note that exact match still trims whitespace.
Link to this function

goto(page, url, options \\ %{})

View Source
@spec goto(t(), binary(), options()) ::
  Playwright.Response.t() | nil | {:error, term()}

A shortcut for the main frame's Playwright.Frame.hover/2.

@spec init(
  struct(),
  map()
) :: {atom(), struct()}

Optional callback implementation for Playwright.SDK.ChannelOwner.init/2.

If implemented, the callback will receive:

  1. The newly created "channel owner" struct.
  2. The :initializer received from the Playwright browser server.

The implementation has the option of "patching" the struct as stored in the catalog, and/or binding event handlers.

Example

def init(%{session: session} = owner, _initializer) do
  Channel.bind(session, {:guid, owner.guid}, :close, fn event ->
    Logger.warning("Closing #{inspect(event.target)}")
  end)

  {:ok, %{owner | version: "1.2.3"}}
end

Returns

  • {:ok, struct()}

Arguments

key/nametypedescription
ownerparamstruct()The newly created channel owner (resource).
initializerparamstruct()The initializer received from with the channel owner instance was derived.
@spec locator(t(), selector()) :: Playwright.Locator.t()
Link to this function

on(page, event, callback)

View Source
Link to this function

press(page, selector, key, options \\ %{})

View Source
@spec press(t(), binary(), binary(), options()) :: :ok
Link to this function

q(page, selector, options \\ %{})

View Source

See Playwright.Page.query_selector/3.

Link to this function

qq(page, selector, options \\ %{})

View Source

See Playwright.Page.query_selector_all/3.

Link to this function

query_selector(page, selector, options \\ %{})

View Source
@spec query_selector(t(), selector(), options()) ::
  Playwright.ElementHandle.t() | nil | {:error, :timeout}
Link to this function

query_selector_all(page, selector, options \\ %{})

View Source
@spec query_selector_all(t(), binary(), map()) :: [Playwright.ElementHandle.t()]
Link to this function

reload(page, options \\ %{})

View Source
@spec reload(t(), options()) :: Playwright.Response.t() | nil

Reloads the current page.

Reloads in the same way as if the user had triggered a browser refresh.

Returns the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect.

Returns

  • Playwright.Response.t() | nil

Arguments

key/nametypedescription
:timeoutoptionnumber()Maximum time in milliseconds. Pass 0 to disable timeout. The default value can be changed via Playwright.BrowserContext.set_default_timeout/2 or Playwright.Page.set_default_timeout/2. (default: 30 seconds)
:wait_untiloptionbinary()"load", "domcontentloaded", "networkidle", or "commit". When to consider the operation as having succeeded. (default: "load")

On Wait Events

  • domcontentloaded - consider operation to be finished when the DOMContentLoaded event is fired.
  • load - consider operation to be finished when the load event is fired.
  • networkidle - consider operation to be finished when there are no network connections for at least 500 ms.
  • commit - consider operation to be finished when network response is received and the document started loading.
@spec request(t()) :: Playwright.APIRequestContext.t()
Link to this function

route(page, pattern, handler, options \\ %{})

View Source
@spec route(t(), binary(), function(), map()) :: :ok
Link to this function

screenshot(page, options \\ %{})

View Source
@spec screenshot(t(), options()) :: binary()
Link to this function

select_option(page, selector, values \\ nil, options \\ %{})

View Source
@spec select_option(t(), binary(), any(), options()) :: [binary()]

A shortcut for the main frame's Playwright.Frame.select_option/4.

Link to this function

set_content(page, html, options \\ %{})

View Source
@spec set_content(t(), binary(), options()) :: :ok
Link to this function

set_viewport_size(page, dimensions)

View Source
@spec set_viewport_size(t(), dimensions()) :: :ok
Link to this function

text_content(page, selector, options \\ %{})

View Source
@spec text_content(t(), binary(), map()) :: binary() | nil
@spec title(t()) :: binary()
@spec url(t()) :: binary()
Link to this function

wait_for_load_state(page, state \\ "load", options \\ %{})

View Source
@spec wait_for_load_state(t(), binary(), options()) :: t()
Link to this function

wait_for_selector(page, selector, options \\ %{})

View Source
@spec wait_for_selector(t(), binary(), map()) :: Playwright.ElementHandle.t() | nil