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
Types
%Playwright.Page{}
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.dblclick/3
.
A shortcut for the main frame's Playwright.Frame.dispatch_event/5
.
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.
A shortcut for the main frame's Playwright.Frame.focus/3
.
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.
A shortcut for the main frame's Playwright.Frame.select_option/4
.
Types
@type dimensions() :: map()
@type expression() :: binary()
@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
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/name | type | description | |
---|---|---|---|
script | param | binary() 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
andPlaywright.Page.add_init_script/2
is not defined.
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/name | type | description | |
---|---|---|---|
run_before_unload | option | boolean() | Whether to run the before unload page handlers. (default: false) |
NOTE
if
option: run_before_unload
is passed astrue
, a:beforeunload
dialog might be summoned and should be handled manually viaPlaywright.Page.on/3
.
@spec context(t()) :: Playwright.BrowserContext.t()
Get the Playwright.BrowserContext
that the page belongs to.
A shortcut for the main frame's Playwright.Frame.dblclick/3
.
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
.
eval_on_selector(page, selector, expression, arg \\ nil, options \\ %{})
View Source@spec evaluate(t(), expression(), any()) :: serializable()
@spec evaluate_handle(t(), expression(), any()) :: serializable()
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.
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.
A shortcut for the main frame's Playwright.Frame.focus/3
.
@spec frames(t()) :: [Playwright.Frame.t()]
@spec get_by_text(t(), binary(), %{optional(:exact) => boolean()}) :: Playwright.Locator.t() | nil
Allows locating elements that contain given text.
Arguments
key/name | type | description | |
---|---|---|---|
text | param | binary() | Text to locate the element for. |
:exact | option | boolean() | 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. |
@spec goto(t(), binary(), options()) :: Playwright.Response.t() | nil | {:error, term()}
A shortcut for the main frame's Playwright.Frame.hover/2
.
Optional callback implementation for Playwright.SDK.ChannelOwner.init/2
.
If implemented, the callback will receive:
- The newly created "channel owner" struct.
- 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/name | type | description | |
---|---|---|---|
owner | param | struct() | The newly created channel owner (resource). |
initializer | param | struct() | The initializer received from with the channel owner instance was derived. |
@spec locator(t(), selector()) :: Playwright.Locator.t()
@spec query_selector(t(), selector(), options()) :: Playwright.ElementHandle.t() | nil | {:error, :timeout}
@spec query_selector_all(t(), binary(), map()) :: [Playwright.ElementHandle.t()]
@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/name | type | description | |
---|---|---|---|
:timeout | option | number() | 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_until | option | binary() | "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 theDOMContentLoaded
event is fired.load
- consider operation to be finished when theload
event is fired.networkidle
- consider operation to be finished when there are no network connections for at least500 ms
.commit
- consider operation to be finished when network response is received and the document started loading.
@spec request(t()) :: Playwright.APIRequestContext.t()
A shortcut for the main frame's Playwright.Frame.select_option/4
.
@spec set_viewport_size(t(), dimensions()) :: :ok
@spec wait_for_selector(t(), binary(), map()) :: Playwright.ElementHandle.t() | nil