Playwright.Page (playwright v0.1.17-preview-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:

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

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

:ok = Page.close(page)

The Page module is capable of hanlding 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

Closes the Page.

Get the full HTML contents of the page, including the doctype.

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.

See Playwright.Frame.query_selector!/3.

See Playwright.Frame.url/1.

Link to this section Types

Specs

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

Specs

options() :: map()

Specs

t() :: %Playwright.Page{
  connection: term(),
  guid: term(),
  initializer: term(),
  is_closed: term(),
  listeners: term(),
  main_frame: term(),
  owned_context: term(),
  parent: term(),
  type: term()
}

%Playwright.Page{}

Link to this section Functions

Link to this function

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

View Source

See Playwright.Frame.click/3.

Link to this function

close(owner, options \\ %{})

View Source

Specs

close(t() | {:ok, 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

content(t() | {:ok, t()}) :: {:ok, binary()}

Get the full HTML contents of the page, including the doctype.

Specs

context(t() | {:ok, 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() | {:ok, 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

See Playwright.Frame.evaluate/3.

Link to this function

evaluate_handle(page, expression, arg \\ nil)

View Source

See Playwright.Frame.evaluate_handle/3.

Link to this function

expect_event(owner, event, trigger)

View Source

See Playwright.BrowserContext.expect_event/3.

Link to this function

fill(page, selector, value)

View Source

See Playwright.Frame.fill/3.

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.

Link to this function

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

View Source

See Playwright.Frame.get_attribute/4.

Link to this function

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

View Source

See Playwright.Frame.goto/3.

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(owner, _initializer) do
  Channel.bind(owner, :close, fn event ->
    Logger.warn("Closing #{inspect(event.target)}")
  end)

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

Returns

  • {:ok, %{}}

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(), binary()) :: Playwright.Locator.t()
Link to this function

on(owner, event, callback)

View Source
Link to this function

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

View Source

See Playwright.Frame.press/4.

Link to this function

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

View Source

See Playwright.Frame.q/3.

Link to this function

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

View Source

See Playwright.Frame.q!/3.

Link to this function

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

View Source

See Playwright.Frame.qq/3.

Link to this function

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

View Source

See Playwright.Frame.query_selector/3.

Link to this function

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

View Source

See Playwright.Frame.query_selector!/3.

Link to this function

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

View Source

See Playwright.Frame.query_selector_all/3.

Link to this function

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

View Source

Specs

route(t(), binary(), function(), map()) :: {atom(), t()}
Link to this function

screenshot(owner, options \\ %{})

View Source

Specs

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

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

View Source

Specs

select_option(t(), binary(), any(), options()) :: {:ok, [binary()]}

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

Link to this function

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

View Source

See Playwright.Frame.set_content/3.

Link to this function

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

View Source

See Playwright.Frame.text_content/3.

See Playwright.Frame.title/1.

See Playwright.Frame.url/1.

Link to this function

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

View Source

Specs

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

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

View Source

See Playwright.Frame.wait_for_selector/3.