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
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
.
A shortcut for the main frame's Playwright.Frame.focus/3
.
A shortcut for the main frame's Playwright.Frame.hover/2
.
Optional callback implementation for Playwright.ChannelOwner.init/2
.
Reloads the current page.
A shortcut for the main frame's Playwright.Frame.select_option/4
.
Link to this section Types
Specs
dimensions() :: map()
Specs
expression() :: binary()
Specs
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
Specs
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(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
andPlaywright.Page.add_init_script/2
is not defined.
Specs
Specs
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
.
Specs
context(t()) :: Playwright.BrowserContext.t()
Get the Playwright.BrowserContext
that the page belongs to.
Specs
A shortcut for the main frame's Playwright.Frame.dblclick/3
.
dispatch_event(page, selector, type, event_init \\ nil, options \\ %{})
View SourceSpecs
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(owner, selector, expression, arg \\ nil, options \\ %{})
View SourceSpecs
Specs
evaluate(t(), expression(), any()) :: serializable()
Specs
evaluate_handle(t(), expression(), any()) :: serializable()
Specs
Specs
A shortcut for the main frame's Playwright.Frame.focus/3
.
Specs
frames(t()) :: [Playwright.Frame.t()]
Specs
Specs
goto(t(), binary(), options()) :: Playwright.Response.t() | nil | {:error, term()}
A shortcut for the main frame's Playwright.Frame.hover/2
.
Specs
Optional callback implementation for Playwright.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.warn("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. |
Specs
locator(t(), selector()) :: Playwright.Locator.t()
Specs
Specs
query_selector(t(), selector(), options()) :: Playwright.ElementHandle.t() | nil | {:error, :timeout}
Specs
query_selector_all(t(), binary(), map()) :: [Playwright.ElementHandle.t()]
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/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.
Specs
request(t()) :: Playwright.APIRequestContext.t()
Specs
Specs
Specs
A shortcut for the main frame's Playwright.Frame.select_option/4
.
Specs
Specs
set_viewport_size(t(), dimensions()) :: :ok
Specs
Specs
Specs
Specs
Specs
wait_for_selector(t(), binary(), map()) :: Playwright.ElementHandle.t() | nil