Playwright.Page (playwright v1.18.0-alpha.1) View Source

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)

Link to this section Summary

Functions

Adds a script to be evaluated before other scripts.

Closes the Page.

Get the Playwright.BrowserContext that the page belongs to.

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

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

Reloads the current page.

Link to this section Types

Specs

dimensions() :: map()

Specs

expression() :: binary()

Specs

function_or_options() :: (... -> any()) | options() | nil

Specs

options() :: map()

Specs

selector() :: binary()

Specs

serializable() :: any()

Specs

t() :: %Playwright.Page{
  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{}

Link to this section Functions

Link to this function

add_init_script(page, script)

View Source

Specs

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(context, %{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

Specs

click(t(), binary(), options()) :: :ok
Link to this function

close(page, options \\ %{})

View Source

Specs

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.

Specs

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

Get the Playwright.BrowserContext that the page belongs to.

Link to this function

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

View Source

Specs

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

Specs

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

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

View Source

Specs

eval_on_selector(t(), binary(), binary(), term(), map()) :: term()
Link to this function

evaluate(page, expression, arg \\ nil)

View Source

Specs

evaluate(t(), expression(), any()) :: serializable()
Link to this function

evaluate_handle(page, expression, arg \\ nil)

View Source

Specs

evaluate_handle(t(), expression(), any()) :: serializable()
Link to this function

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

View Source
Link to this function

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

View Source

Specs

fill(t(), binary(), binary(), options()) :: :ok
Link to this function

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

View Source

Specs

focus(t(), binary(), options()) :: :ok

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

Specs

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

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

View Source

Specs

get_attribute(t(), binary(), binary(), map()) :: binary() | nil
Link to this function

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

View Source

Specs

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

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

Specs

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

Optional callback implementation for Playwright.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.warn("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.

Specs

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

Specs

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

Specs

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

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

View Source

Specs

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

reload(page, options \\ %{})

View Source

Specs

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.

Specs

Link to this function

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

View Source

Specs

route(t(), binary(), function(), map()) :: :ok
Link to this function

screenshot(page, options \\ %{})

View Source

Specs

screenshot(t(), options()) :: binary()
Link to this function

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

View Source

Specs

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

Specs

set_content(t(), binary(), options()) :: :ok
Link to this function

set_viewport_size(page, dimensions)

View Source

Specs

set_viewport_size(t(), dimensions()) :: :ok
Link to this function

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

View Source

Specs

text_content(t(), binary(), map()) :: binary() | nil

Specs

title(t()) :: binary()

Specs

url(t()) :: binary()
Link to this function

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

View Source

Specs

wait_for_load_state(t(), binary(), options()) :: t()
Link to this function

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

View Source

Specs

wait_for_selector(t(), binary(), map()) :: Playwright.ElementHandle.t() | nil