Skip to main content

Page

  • extends: [EventEmitter]

Page provides methods to interact with a single tab in a Browser, or an extension background page in Chromium. One Browser instance might have multiple Page instances.

This example creates a page, navigates it to a URL, and then saves a screenshot:

playwright.webkit.launch do |browser|
page = browser.new_page
page.goto('https://example.com/')
page.screenshot(path: 'screenshot.png')
end

The Page class emits various events (described below) which can be handled using any of Node's native EventEmitter methods, such as on, once or removeListener.

This example logs a message for a single page load event:

page.once("load", -> (page) { puts "page loaded!" })

To unsubscribe from events use the removeListener method:

listener = -> (req) { puts "a request was made: #{req.url}" }
page.on('request', listener)
page.goto('https://example.com/') # => prints 'a request was made: https://example.com/'
page.off('request', listener)
page.goto('https://example.com/') # => no print

add_init_script

def add_init_script(path: nil, script: nil)

Adds a script which would be evaluated in one of the following scenarios:

  • Whenever the page is navigated.
  • Whenever the 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 was created but before any of its scripts were run. This is useful to amend the JavaScript environment, e.g. to seed Math.random.

An example of overriding Math.random before the page loads:

# in your playwright script, assuming the preload.js file is in same directory
page.add_init_script(path: "./preload.js")

NOTE: The order of evaluation of multiple scripts installed via BrowserContext#add_init_script and Page#add_init_script is not defined.

add_script_tag

def add_script_tag(content: nil, path: nil, type: nil, url: nil)

Adds a <script> tag into the page with the desired url or content. Returns the added tag when the script's onload fires or when the script content was injected into frame.

Shortcut for main frame's Frame#add_script_tag.

add_style_tag

def add_style_tag(content: nil, path: nil, url: nil)

Adds a <link rel="stylesheet"> tag into the page with the desired url or a <style type="text/css"> tag with the content. Returns the added tag when the stylesheet's onload fires or when the CSS content was injected into frame.

Shortcut for main frame's Frame#add_style_tag.

bring_to_front

def bring_to_front

Brings page to front (activates tab).

check

def check(
selector,
force: nil,
noWaitAfter: nil,
position: nil,
strict: nil,
timeout: nil,
trial: nil)

This method checks an element matching selector by performing the following steps:

  1. Find an element matching selector. If there is none, wait until a matching element is attached to the DOM.
  2. Ensure that matched element is a checkbox or a radio input. If not, this method throws. If the element is already checked, this method returns immediately.
  3. Wait for actionability checks on the matched element, unless force option is set. If the element is detached during the checks, the whole action is retried.
  4. Scroll the element into view if needed.
  5. Use Page#mouse to click in the center of the element.
  6. Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set.
  7. Ensure that the element is now checked. If not, this method throws.

When all steps combined have not finished during the specified timeout, this method throws a TimeoutError. Passing zero timeout disables this.

Shortcut for main frame's Frame#check.

click

def click(
selector,
button: nil,
clickCount: nil,
delay: nil,
force: nil,
modifiers: nil,
noWaitAfter: nil,
position: nil,
strict: nil,
timeout: nil,
trial: nil)

This method clicks an element matching selector by performing the following steps:

  1. Find an element matching selector. If there is none, wait until a matching element is attached to the DOM.
  2. Wait for actionability checks on the matched element, unless force option is set. If the element is detached during the checks, the whole action is retried.
  3. Scroll the element into view if needed.
  4. Use Page#mouse to click in the center of the element, or the specified position.
  5. Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set.

When all steps combined have not finished during the specified timeout, this method throws a TimeoutError. Passing zero timeout disables this.

Shortcut for main frame's Frame#click.

close

def close(runBeforeUnload: nil)

If runBeforeUnload is false, does not run any unload handlers and waits for the page to be closed. If runBeforeUnload is true the method will run unload handlers, but will not wait for the page to close.

By default, page.close() does not run beforeunload handlers.

NOTE: if runBeforeUnload is passed as true, a beforeunload dialog might be summoned and should be handled manually via [event: Page.dialog] event.

content

def content

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

context

def context

Get the browser context that the page belongs to.

dblclick

def dblclick(
selector,
button: nil,
delay: nil,
force: nil,
modifiers: nil,
noWaitAfter: nil,
position: nil,
strict: nil,
timeout: nil,
trial: nil)

This method double clicks an element matching selector by performing the following steps:

  1. Find an element matching selector. If there is none, wait until a matching element is attached to the DOM.
  2. Wait for actionability checks on the matched element, unless force option is set. If the element is detached during the checks, the whole action is retried.
  3. Scroll the element into view if needed.
  4. Use Page#mouse to double click in the center of the element, or the specified position.
  5. Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set. Note that if the first click of the dblclick() triggers a navigation event, this method will throw.

When all steps combined have not finished during the specified timeout, this method throws a TimeoutError. Passing zero timeout disables this.

NOTE: page.dblclick() dispatches two click events and a single dblclick event.

Shortcut for main frame's Frame#dblclick.

dispatch_event

def dispatch_event(
selector,
type,
eventInit: nil,
strict: nil,
timeout: nil)

The snippet below dispatches the click event on the element. Regardless of the visibility state of the element, click is dispatched. This is equivalent to calling element.click().

page.content = '<button id="submit">Send</button>'
page.dispatch_event("button#submit", "click")

Under the hood, it creates an instance of an event based on the given type, initializes it with eventInit properties and dispatches it on the element. Events are composed, cancelable and bubble by default.

Since eventInit is event-specific, please refer to the events documentation for the lists of initial properties:

You can also specify JSHandle as the property value if you want live objects to be passed into the event:

page.content = '<div id="source">Drag</div>'

# note you can only create data_transfer in chromium and firefox
data_transfer = page.evaluate_handle("new DataTransfer()")
page.dispatch_event("#source", "dragstart", eventInit: { dataTransfer: data_transfer })

drag_and_drop

def drag_and_drop(
source,
target,
force: nil,
noWaitAfter: nil,
sourcePosition: nil,
strict: nil,
targetPosition: nil,
timeout: nil,
trial: nil)

This method drags the source element to the target element. It will first move to the source element, perform a mousedown, then move to the target element and perform a mouseup.

page.drag_and_drop("#source", "#target")
# or specify exact positions relative to the top-left corners of the elements:
page.drag_and_drop(
"#source",
"#target",
sourcePosition: { x: 34, y: 7 },
targetPosition: { x: 10, y: 20 },
)

emulate_media

def emulate_media(colorScheme: nil, forcedColors: nil, media: nil, reducedMotion: nil)

This method changes the CSS media type through the media argument, and/or the 'prefers-colors-scheme' media feature, using the colorScheme argument.

page.evaluate("matchMedia('screen').matches") # => true
page.evaluate("matchMedia('print').matches") # => false

page.emulate_media(media: "print")
page.evaluate("matchMedia('screen').matches") # => false
page.evaluate("matchMedia('print').matches") # => true

page.emulate_media
page.evaluate("matchMedia('screen').matches") # => true
page.evaluate("matchMedia('print').matches") # => false
page.emulate_media(colorScheme="dark")
page.evaluate("matchMedia('(prefers-color-scheme: dark)').matches") # => true
page.evaluate("matchMedia('(prefers-color-scheme: light)').matches") # => false
page.evaluate("matchMedia('(prefers-color-scheme: no-preference)').matches") # => false

eval_on_selector

def eval_on_selector(selector, expression, arg: nil, strict: nil)

NOTE: This method does not wait for the element to pass actionability checks and therefore can lead to the flaky tests. Use Locator#evaluate, other Locator helper methods or web-first assertions instead.

The method finds an element matching the specified selector within the page and passes it as a first argument to expression. If no elements match the selector, the method throws an error. Returns the value of expression.

If expression returns a Promise, then Page#eval_on_selector would wait for the promise to resolve and return its value.

Examples:

search_value = page.eval_on_selector("#search", "el => el.value")
preload_href = page.eval_on_selector("link[rel=preload]", "el => el.href")
html = page.eval_on_selector(".main-container", "(e, suffix) => e.outer_html + suffix", arg: "hello")

Shortcut for main frame's Frame#eval_on_selector.

eval_on_selector_all

def eval_on_selector_all(selector, expression, arg: nil)

NOTE: In most cases, Locator#evaluate_all, other Locator helper methods and web-first assertions do a better job.

The method finds all elements matching the specified selector within the page and passes an array of matched elements as a first argument to expression. Returns the result of expression invocation.

If expression returns a Promise, then Page#eval_on_selector_all would wait for the promise to resolve and return its value.

Examples:

div_counts = page.eval_on_selector_all("div", "(divs, min) => divs.length >= min", arg: 10)

evaluate

def evaluate(expression, arg: nil)

Returns the value of the expression invocation.

If the function passed to the Page#evaluate returns a Promise, then Page#evaluate would wait for the promise to resolve and return its value.

If the function passed to the Page#evaluate returns a non-Serializable value, then Page#evaluate resolves to undefined. Playwright also supports transferring some additional values that are not serializable by JSON: -0, NaN, Infinity, -Infinity.

Passing argument to expression:

result = page.evaluate("([x, y]) => Promise.resolve(x * y)", arg: [7, 8])
puts result # => "56"

A string can also be passed in instead of a function:

puts page.evaluate("1 + 2") # => 3
x = 10
puts page.evaluate("1 + #{x}") # => "11"

ElementHandle instances can be passed as an argument to the Page#evaluate:

body_handle = page.query_selector("body")
html = page.evaluate("([body, suffix]) => body.innerHTML + suffix", arg: [body_handle, "hello"])
body_handle.dispose

Shortcut for main frame's Frame#evaluate.

evaluate_handle

def evaluate_handle(expression, arg: nil)

Returns the value of the expression invocation as a JSHandle.

The only difference between Page#evaluate and Page#evaluate_handle is that Page#evaluate_handle returns JSHandle.

If the function passed to the Page#evaluate_handle returns a Promise, then Page#evaluate_handle would wait for the promise to resolve and return its value.

a_window_handle = page.evaluate_handle("Promise.resolve(window)")
a_window_handle # handle for the window object.

A string can also be passed in instead of a function:

a_handle = page.evaluate_handle("document") # handle for the "document"

JSHandle instances can be passed as an argument to the Page#evaluate_handle:

body_handle = page.evaluate_handle("document.body")
result_handle = page.evaluate_handle("body => body.innerHTML", arg: body_handle)
puts result_handle.json_value
result_handle.dispose

expose_binding

def expose_binding(name, callback, handle: nil)

The method adds a function called name on the window object of every frame in this page. When called, the function executes callback and returns a Promise which resolves to the return value of callback. If the callback returns a Promise, it will be awaited.

The first argument of the callback function contains information about the caller: { browser_context: BrowserContext, page: Page, frame: Frame }.

See BrowserContext#expose_binding for the context-wide version.

NOTE: Functions installed via Page#expose_binding survive navigations.

An example of exposing page URL to all frames in a page:

page.expose_binding("pageURL", ->(source) { source[:page].url })
page.content = <<~HTML
<script>
async function onClick() {
document.querySelector('div').textContent = await window.pageURL();
}
</script>
<button onclick="onClick()">Click me</button>
<div></div>
HTML
page.locator("button").click

An example of passing an element handle:

def print_text(source, element)
element.text_content
end

page.expose_binding("clicked", method(:print_text), handle: true)
page.content = <<~HTML
<script>
document.addEventListener('click', async (event) => {
alert(await window.clicked(event.target));
})
</script>
<div>Click me</div>
<div>Or click me</div>
HTML

page.locator('div').first.click

expose_function

def expose_function(name, callback)

The method adds a function called name on the window object of every frame in the page. When called, the function executes callback and returns a Promise which resolves to the return value of callback.

If the callback returns a Promise, it will be awaited.

See BrowserContext#expose_function for context-wide exposed function.

NOTE: Functions installed via Page#expose_function survive navigations.

An example of adding a sha256 function to the page:

require 'digest'

def sha1(text)
Digest::SHA256.hexdigest(text)
end

page.expose_function("sha256", method(:sha256))
page.content = <<~HTML
<script>
async function onClick() {
document.querySelector('div').textContent = await window.sha256('PLAYWRIGHT');
}
</script>
<button onclick="onClick()">Click me</button>
<div></div>
HTML
page.locator("button").click

fill

def fill(
selector,
value,
force: nil,
noWaitAfter: nil,
strict: nil,
timeout: nil)

This method waits for an element matching selector, waits for actionability checks, focuses the element, fills it and triggers an input event after filling. Note that you can pass an empty string to clear the input field.

If the target element is not an <input>, <textarea> or [contenteditable] element, this method throws an error. However, if the element is inside the <label> element that has an associated control, the control will be filled instead.

To send fine-grained keyboard events, use Page#type.

Shortcut for main frame's Frame#fill.

focus

def focus(selector, strict: nil, timeout: nil)

This method fetches an element with selector and focuses it. If there's no element matching selector, the method waits until a matching element appears in the DOM.

Shortcut for main frame's Frame#focus.

frame

def frame(name: nil, url: nil)

Returns frame matching the specified criteria. Either name or url must be specified.

frame = page.frame(name: "frame-name")
frame = page.frame(url: /.*domain.*/)

frame_locator

def frame_locator(selector)

When working with iframes, you can create a frame locator that will enter the iframe and allow selecting elements in that iframe. Following snippet locates element with text "Submit" in the iframe with id my-frame, like <iframe id="my-frame">:

locator = page.frame_locator("#my-iframe").get_by_text("Submit")
locator.click

frames

def frames

An array of all frames attached to the page.

get_attribute

def get_attribute(selector, name, strict: nil, timeout: nil)

Returns element attribute value.

get_by_alt_text

def get_by_alt_text(text, exact: nil)

Allows locating elements by their alt text. For example, this method will find the image by alt text "Castle":

<img alt='Castle'>

get_by_label

def get_by_label(text, exact: nil)

Allows locating input elements by the text of the associated label. For example, this method will find the input by label text Password in the following DOM:

<label for="password-input">Password:</label>
<input id="password-input">

get_by_placeholder

def get_by_placeholder(text, exact: nil)

Allows locating input elements by the placeholder text. For example, this method will find the input by placeholder "Country":

<input placeholder="Country">

get_by_role

def get_by_role(
role,
checked: nil,
disabled: nil,
expanded: nil,
includeHidden: nil,
level: nil,
name: nil,
pressed: nil,
selected: nil)

Allows locating elements by their ARIA role, ARIA attributes and accessible name. Note that role selector does not replace accessibility audits and conformance tests, but rather gives early feedback about the ARIA guidelines.

Note that many html elements have an implicitly defined role that is recognized by the role selector. You can find all the supported roles here. ARIA guidelines do not recommend duplicating implicit roles and attributes by setting role and/or aria-* attributes to default values.

get_by_test_id

def get_by_test_id(testId)

Locate element by the test id. By default, the data-testid attribute is used as a test id. Use Selectors#set_test_id_attribute to configure a different test id attribute if necessary.

get_by_text

def get_by_text(text, exact: nil)

Allows locating elements that contain given text.

get_by_title

def get_by_title(text, exact: nil)

Allows locating elements by their title. For example, this method will find the button by its title "Submit":

<button title='Place the order'>Order Now</button>

go_back

def go_back(timeout: nil, waitUntil: nil)

Returns the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. If can not go back, returns null.

Navigate to the previous page in history.

go_forward

def go_forward(timeout: nil, waitUntil: nil)

Returns the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. If can not go forward, returns null.

Navigate to the next page in history.

goto

def goto(url, referer: nil, timeout: nil, waitUntil: nil)

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

The method will throw an error if:

  • there's an SSL error (e.g. in case of self-signed certificates).
  • target URL is invalid.
  • the timeout is exceeded during navigation.
  • the remote server does not respond or is unreachable.
  • the main resource failed to load.

The method will not throw an error when any valid HTTP status code is returned by the remote server, including 404 "Not Found" and 500 "Internal Server Error". The status code for such responses can be retrieved by calling Response#status.

NOTE: The method either throws an error or returns a main resource response. The only exceptions are navigation to about:blank or navigation to the same URL with a different hash, which would succeed and return null. NOTE: Headless mode doesn't support navigation to a PDF document. See the upstream issue.

Shortcut for main frame's Frame#goto

hover

def hover(
selector,
force: nil,
modifiers: nil,
position: nil,
strict: nil,
timeout: nil,
trial: nil)

This method hovers over an element matching selector by performing the following steps:

  1. Find an element matching selector. If there is none, wait until a matching element is attached to the DOM.
  2. Wait for actionability checks on the matched element, unless force option is set. If the element is detached during the checks, the whole action is retried.
  3. Scroll the element into view if needed.
  4. Use Page#mouse to hover over the center of the element, or the specified position.
  5. Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set.

When all steps combined have not finished during the specified timeout, this method throws a TimeoutError. Passing zero timeout disables this.

Shortcut for main frame's Frame#hover.

inner_html

def inner_html(selector, strict: nil, timeout: nil)

Returns element.innerHTML.

inner_text

def inner_text(selector, strict: nil, timeout: nil)

Returns element.innerText.

input_value

def input_value(selector, strict: nil, timeout: nil)

Returns input.value for the selected <input> or <textarea> or <select> element.

Throws for non-input elements. However, if the element is inside the <label> element that has an associated control, returns the value of the control.

checked?

def checked?(selector, strict: nil, timeout: nil)

Returns whether the element is checked. Throws if the element is not a checkbox or radio input.

closed?

def closed?

Indicates that the page has been closed.

disabled?

def disabled?(selector, strict: nil, timeout: nil)

Returns whether the element is disabled, the opposite of enabled.

editable?

def editable?(selector, strict: nil, timeout: nil)

Returns whether the element is editable.

enabled?

def enabled?(selector, strict: nil, timeout: nil)

Returns whether the element is enabled.

hidden?

def hidden?(selector, strict: nil, timeout: nil)

Returns whether the element is hidden, the opposite of visible. selector that does not match any elements is considered hidden.

visible?

def visible?(selector, strict: nil, timeout: nil)

Returns whether the element is visible. selector that does not match any elements is considered not visible.

locator

def locator(selector, has: nil, hasText: nil)

The method returns an element locator that can be used to perform actions on this page / frame. Locator is resolved to the element immediately before performing an action, so a series of actions on the same locator can in fact be performed on different DOM elements. That would happen if the DOM structure between those actions has changed.

Learn more about locators.

main_frame

def main_frame

The page's main frame. Page is guaranteed to have a main frame which persists during navigations.

opener

def opener

Returns the opener for popup pages and null for others. If the opener has been closed already the returns null.

pause

def pause

Pauses script execution. Playwright will stop executing the script and wait for the user to either press 'Resume' button in the page overlay or to call playwright.resume() in the DevTools console.

User can inspect selectors or perform manual steps while paused. Resume will continue running the original script from the place it was paused.

NOTE: This method requires Playwright to be started in a headed mode, with a falsy headless value in the BrowserType#launch.

pdf

def pdf(
displayHeaderFooter: nil,
footerTemplate: nil,
format: nil,
headerTemplate: nil,
height: nil,
landscape: nil,
margin: nil,
pageRanges: nil,
path: nil,
preferCSSPageSize: nil,
printBackground: nil,
scale: nil,
width: nil)

Returns the PDF buffer.

NOTE: Generating a pdf is currently only supported in Chromium headless.

page.pdf() generates a pdf of the page with print css media. To generate a pdf with screen media, call Page#emulate_media before calling page.pdf():

NOTE: By default, page.pdf() generates a pdf with modified colors for printing. Use the -webkit-print-color-adjust property to force rendering of exact colors.

# generates a pdf with "screen" media type.
page.emulate_media(media: "screen")
page.pdf(path: "page.pdf")

The width, height, and margin options accept values labeled with units. Unlabeled values are treated as pixels.

A few examples:

  • page.pdf({width: 100}) - prints with width set to 100 pixels
  • page.pdf({width: '100px'}) - prints with width set to 100 pixels
  • page.pdf({width: '10cm'}) - prints with width set to 10 centimeters.

All possible units are:

  • px - pixel
  • in - inch
  • cm - centimeter
  • mm - millimeter

The format options are:

  • Letter: 8.5in x 11in
  • Legal: 8.5in x 14in
  • Tabloid: 11in x 17in
  • Ledger: 17in x 11in
  • A0: 33.1in x 46.8in
  • A1: 23.4in x 33.1in
  • A2: 16.54in x 23.4in
  • A3: 11.7in x 16.54in
  • A4: 8.27in x 11.7in
  • A5: 5.83in x 8.27in
  • A6: 4.13in x 5.83in

NOTE: headerTemplate and footerTemplate markup have the following limitations: > 1. Script tags inside templates are not evaluated. > 2. Page styles are not visible inside templates.

press

def press(
selector,
key,
delay: nil,
noWaitAfter: nil,
strict: nil,
timeout: nil)

Focuses the element, and then uses Keyboard#down and Keyboard#up.

key can specify the intended keyboardEvent.key value or a single character to generate the text for. A superset of the key values can be found here. Examples of the keys are:

F1 - F12, Digit0- Digit9, KeyA- KeyZ, Backquote, Minus, Equal, Backslash, Backspace, Tab, Delete, Escape, ArrowDown, End, Enter, Home, Insert, PageDown, PageUp, ArrowRight, ArrowUp, etc.

Following modification shortcuts are also supported: Shift, Control, Alt, Meta, ShiftLeft.

Holding down Shift will type the text that corresponds to the key in the upper case.

If key is a single character, it is case-sensitive, so the values a and A will generate different respective texts.

Shortcuts such as key: "Control+o" or key: "Control+Shift+T" are supported as well. When specified with the modifier, modifier is pressed and being held while the subsequent key is being pressed.

page.goto("https://keycode.info")
page.press("body", "A")
page.screenshot(path: "a.png")
page.press("body", "ArrowLeft")
page.screenshot(path: "arrow_left.png")
page.press("body", "Shift+O")
page.screenshot(path: "o.png")

query_selector

def query_selector(selector, strict: nil)

NOTE: The use of ElementHandle is discouraged, use Locator objects and web-first assertions instead.

The method finds an element matching the specified selector within the page. If no elements match the selector, the return value resolves to null. To wait for an element on the page, use Locator#wait_for.

Shortcut for main frame's Frame#query_selector.

query_selector_all

def query_selector_all(selector)

NOTE: The use of ElementHandle is discouraged, use Locator objects and web-first assertions instead.

The method finds all elements matching the specified selector within the page. If no elements match the selector, the return value resolves to [].

Shortcut for main frame's Frame#query_selector_all.

reload

def reload(timeout: nil, waitUntil: nil)

This method reloads the current page, 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.

route

def route(url, handler, times: nil)

Routing provides the capability to modify network requests that are made by a page.

Once routing is enabled, every request matching the url pattern will stall unless it's continued, fulfilled or aborted.

NOTE: The handler will only be called for the first url if the response is a redirect. NOTE: Page#route will not intercept requests intercepted by Service Worker. See this issue. We recommend disabling Service Workers when using request interception by setting Browser.newContext.serviceWorkers to 'block'.

An example of a naive handler that aborts all image requests:

page.route("**/*.{png,jpg,jpeg}", ->(route, request) { route.abort })
page.goto("https://example.com")

or the same snippet using a regex pattern instead:

page.route(/\.(png|jpg)$/, ->(route, request) { route.abort })
page.goto("https://example.com")

It is possible to examine the request to decide the route action. For example, mocking all requests that contain some post data, and leaving all other requests as is:

def handle_route(route, request)
if request.post_data["my-string"]
mocked_data = request.post_data.merge({ "my-string" => 'mocked-data'})
route.fulfill(postData: mocked_data)
else
route.continue
end
end
page.route("/api/**", method(:handle_route))

Page routes take precedence over browser context routes (set up with BrowserContext#route) when request matches both handlers.

To remove a route with its handler you can use Page#unroute.

NOTE: Enabling routing disables http cache.

route_from_har

def route_from_har(har, notFound: nil, update: nil, url: nil)

If specified the network requests that are made in the page will be served from the HAR file. Read more about Replaying from HAR.

Playwright will not serve requests intercepted by Service Worker from the HAR file. See this issue. We recommend disabling Service Workers when using request interception by setting Browser.newContext.serviceWorkers to 'block'.

screenshot

def screenshot(
animations: nil,
caret: nil,
clip: nil,
fullPage: nil,
mask: nil,
omitBackground: nil,
path: nil,
quality: nil,
scale: nil,
timeout: nil,
type: nil)

Returns the buffer with the captured screenshot.

select_option

def select_option(
selector,
element: nil,
index: nil,
value: nil,
label: nil,
force: nil,
noWaitAfter: nil,
strict: nil,
timeout: nil)

This method waits for an element matching selector, waits for actionability checks, waits until all specified options are present in the <select> element and selects these options.

If the target element is not a <select> element, this method throws an error. However, if the element is inside the <label> element that has an associated control, the control will be used instead.

Returns the array of option values that have been successfully selected.

Triggers a change and input event once all the provided options have been selected.

# single selection matching the value
page.select_option("select#colors", value: "blue")
# single selection matching both the label
page.select_option("select#colors", label: "blue")
# multiple selection
page.select_option("select#colors", value: ["red", "green", "blue"])

Shortcut for main frame's Frame#select_option.

set_checked

def set_checked(
selector,
checked,
force: nil,
noWaitAfter: nil,
position: nil,
strict: nil,
timeout: nil,
trial: nil)

This method checks or unchecks an element matching selector by performing the following steps:

  1. Find an element matching selector. If there is none, wait until a matching element is attached to the DOM.
  2. Ensure that matched element is a checkbox or a radio input. If not, this method throws.
  3. If the element already has the right checked state, this method returns immediately.
  4. Wait for actionability checks on the matched element, unless force option is set. If the element is detached during the checks, the whole action is retried.
  5. Scroll the element into view if needed.
  6. Use Page#mouse to click in the center of the element.
  7. Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set.
  8. Ensure that the element is now checked or unchecked. If not, this method throws.

When all steps combined have not finished during the specified timeout, this method throws a TimeoutError. Passing zero timeout disables this.

Shortcut for main frame's Frame#set_checked.

set_content

def set_content(html, timeout: nil, waitUntil: nil)

alias: content=

set_default_navigation_timeout

def set_default_navigation_timeout(timeout)

alias: default_navigation_timeout=

This setting will change the default maximum navigation time for the following methods and related shortcuts:

NOTE: Page#set_default_navigation_timeout takes priority over Page#set_default_timeout, BrowserContext#set_default_timeout and BrowserContext#set_default_navigation_timeout.

set_default_timeout

def set_default_timeout(timeout)

alias: default_timeout=

This setting will change the default maximum time for all the methods accepting timeout option.

NOTE: Page#set_default_navigation_timeout takes priority over Page#set_default_timeout.

set_extra_http_headers

def set_extra_http_headers(headers)

alias: extra_http_headers=

The extra HTTP headers will be sent with every request the page initiates.

NOTE: Page#set_extra_http_headers does not guarantee the order of headers in the outgoing requests.

set_input_files

def set_input_files(
selector,
files,
noWaitAfter: nil,
strict: nil,
timeout: nil)

Sets the value of the file input to these file paths or files. If some of the filePaths are relative paths, then they are resolved relative to the current working directory. For empty array, clears the selected files.

This method expects selector to point to an input element. However, if the element is inside the <label> element that has an associated control, targets the control instead.

set_viewport_size

def set_viewport_size(viewportSize)

alias: viewport_size=

In the case of multiple pages in a single browser, each page can have its own viewport size. However, Browser#new_context allows to set viewport size (and more) for all pages in the context at once.

Page#set_viewport_size will resize the page. A lot of websites don't expect phones to change size, so you should set the viewport size before navigating to the page. Page#set_viewport_size will also reset screen size, use Browser#new_context with screen and viewport parameters if you need better control of these properties.

page.viewport_size = { width: 640, height: 480 }
page.goto("https://example.com")

tap_point

def tap_point(
selector,
force: nil,
modifiers: nil,
noWaitAfter: nil,
position: nil,
strict: nil,
timeout: nil,
trial: nil)

This method taps an element matching selector by performing the following steps:

  1. Find an element matching selector. If there is none, wait until a matching element is attached to the DOM.
  2. Wait for actionability checks on the matched element, unless force option is set. If the element is detached during the checks, the whole action is retried.
  3. Scroll the element into view if needed.
  4. Use Page#touchscreen to tap the center of the element, or the specified position.
  5. Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set.

When all steps combined have not finished during the specified timeout, this method throws a TimeoutError. Passing zero timeout disables this.

NOTE: Page#tap_point requires that the hasTouch option of the browser context be set to true.

Shortcut for main frame's Frame#tap_point.

text_content

def text_content(selector, strict: nil, timeout: nil)

Returns element.textContent.

title

def title

Returns the page's title. Shortcut for main frame's Frame#title.

type

def type(
selector,
text,
delay: nil,
noWaitAfter: nil,
strict: nil,
timeout: nil)

Sends a keydown, keypress/input, and keyup event for each character in the text. page.type can be used to send fine-grained keyboard events. To fill values in form fields, use Page#fill.

To press a special key, like Control or ArrowDown, use Keyboard#press.

page.type("#mytextarea", "hello") # types instantly
page.type("#mytextarea", "world", delay: 100) # types slower, like a user

Shortcut for main frame's Frame#type.

uncheck

def uncheck(
selector,
force: nil,
noWaitAfter: nil,
position: nil,
strict: nil,
timeout: nil,
trial: nil)

This method unchecks an element matching selector by performing the following steps:

  1. Find an element matching selector. If there is none, wait until a matching element is attached to the DOM.
  2. Ensure that matched element is a checkbox or a radio input. If not, this method throws. If the element is already unchecked, this method returns immediately.
  3. Wait for actionability checks on the matched element, unless force option is set. If the element is detached during the checks, the whole action is retried.
  4. Scroll the element into view if needed.
  5. Use Page#mouse to click in the center of the element.
  6. Wait for initiated navigations to either succeed or fail, unless noWaitAfter option is set.
  7. Ensure that the element is now unchecked. If not, this method throws.

When all steps combined have not finished during the specified timeout, this method throws a TimeoutError. Passing zero timeout disables this.

Shortcut for main frame's Frame#uncheck.

unroute

def unroute(url, handler: nil)

Removes a route created with Page#route. When handler is not specified, removes all routes for the url.

url

def url

Shortcut for main frame's Frame#url.

video

def video

Video object associated with this page.

viewport_size

def viewport_size

expect_console_message

def expect_console_message(predicate: nil, timeout: nil, &block)

Performs action and waits for a ConsoleMessage to be logged by in the page. If predicate is provided, it passes ConsoleMessage value into the predicate function and waits for predicate(message) to return a truthy value. Will throw an error if the page is closed before the [event: Page.console] event is fired.

expect_download

def expect_download(predicate: nil, timeout: nil, &block)

Performs action and waits for a new Download. If predicate is provided, it passes Download value into the predicate function and waits for predicate(download) to return a truthy value. Will throw an error if the page is closed before the download event is fired.

expect_event

def expect_event(event, predicate: nil, timeout: nil, &block)

Waits for event to fire and passes its value into the predicate function. Returns when the predicate returns truthy value. Will throw an error if the page is closed before the event is fired. Returns the event data value.

frame = page.expect_event("framenavigated") do
page.get_by_role("button")
end

expect_file_chooser

def expect_file_chooser(predicate: nil, timeout: nil, &block)

Performs action and waits for a new FileChooser to be created. If predicate is provided, it passes FileChooser value into the predicate function and waits for predicate.call(fileChooser) to return a truthy value. Will throw an error if the page is closed before the file chooser is opened.

wait_for_function

def wait_for_function(expression, arg: nil, polling: nil, timeout: nil)

Returns when the expression returns a truthy value. It resolves to a JSHandle of the truthy value.

The Page#wait_for_function can be used to observe viewport size change:

page.evaluate("window.x = 0; setTimeout(() => { window.x = 100 }, 1000);")
page.wait_for_function("() => window.x > 0")

To pass an argument to the predicate of Page#wait_for_function function:

selector = ".foo"
page.wait_for_function("selector => !!document.querySelector(selector)", arg: selector)

Shortcut for main frame's Frame#wait_for_function.

wait_for_load_state

def wait_for_load_state(state: nil, timeout: nil)

Returns when the required load state has been reached.

This resolves when the page reaches a required load state, load by default. The navigation must have been committed when this method is called. If current document has already reached the required state, resolves immediately.

page.get_by_role("button").click # click triggers navigation.
page.wait_for_load_state # the promise resolves after "load" event.
popup = page.expect_popup do
page.get_by_role("button").click # click triggers a popup.
end

# Following resolves after "domcontentloaded" event.
popup.wait_for_load_state("domcontentloaded")
puts popup.title # popup is ready to use.

Shortcut for main frame's Frame#wait_for_load_state.

expect_navigation

def expect_navigation(timeout: nil, url: nil, waitUntil: nil, &block)

Waits for the main frame navigation and returns the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. In case of navigation to a different anchor or navigation due to History API usage, the navigation will resolve with null.

This resolves when the page navigates to a new URL or reloads. It is useful for when you run code which will indirectly cause the page to navigate. e.g. The click target has an onclick handler that triggers navigation from a setTimeout. Consider this example:

page.expect_navigation do
page.click("a.delayed-navigation") # clicking the link will indirectly cause a navigation
end # Resolves after navigation has finished

NOTE: Usage of the History API to change the URL is considered a navigation.

Shortcut for main frame's Frame#expect_navigation.

expect_popup

def expect_popup(predicate: nil, timeout: nil, &block)

Performs action and waits for a popup Page. If predicate is provided, it passes popup Page value into the predicate function and waits for predicate.call(page) to return a truthy value. Will throw an error if the page is closed before the popup event is fired.

expect_request

def expect_request(urlOrPredicate, timeout: nil, &block)

Waits for the matching request and returns it. See waiting for event for more details about events.

page.content = '<form action="https://example.com/resource"><input type="submit" /></form>'
request = page.expect_request(/example.com\/resource/) do
page.click("input")
end
puts request.headers

page.wait_for_load_state # wait for request finished.

# or with a predicate
page.content = '<form action="https://example.com/resource"><input type="submit" /></form>'
request = page.expect_request(->(req) { req.url.start_with? 'https://example.com/resource' }) do
page.click("input")
end
puts request.headers

expect_request_finished

def expect_request_finished(predicate: nil, timeout: nil, &block)

Performs action and waits for a Request to finish loading. If predicate is provided, it passes Request value into the predicate function and waits for predicate(request) to return a truthy value. Will throw an error if the page is closed before the [event: Page.requestFinished] event is fired.

expect_response

def expect_response(urlOrPredicate, timeout: nil, &block)

Returns the matched response. See waiting for event for more details about events.

page.content = '<form action="https://example.com/resource"><input type="submit" /></form>'
response = page.expect_response(/example.com\/resource/) do
page.click("input")
end
puts response.body
puts response.ok?

page.wait_for_load_state # wait for request finished.

# or with a predicate
page.content = '<form action="https://example.com/resource"><input type="submit" /></form>'
response = page.expect_response(->(res) { res.url.start_with? 'https://example.com/resource' }) do
page.click("input")
end
puts response.body
puts response.ok?

wait_for_selector

def wait_for_selector(selector, state: nil, strict: nil, timeout: nil)

Returns when element specified by selector satisfies state option. Returns null if waiting for hidden or detached.

NOTE: Playwright automatically waits for element to be ready before performing an action. Using Locator objects and web-first assertions makes the code wait-for-selector-free.

Wait for the selector to satisfy state option (either appear/disappear from dom, or become visible/hidden). If at the moment of calling the method selector already satisfies the condition, the method will return immediately. If the selector doesn't satisfy the condition for the timeout milliseconds, the function will throw.

This method works across navigations:

%w[https://google.com https://bbc.com].each do |current_url|
page.goto(current_url, waitUntil: "domcontentloaded")
element = page.wait_for_selector("img")
puts "Loaded image: #{element["src"]}"
end

wait_for_timeout

def wait_for_timeout(timeout)

Waits for the given timeout in milliseconds.

Note that page.waitForTimeout() should only be used for debugging. Tests using the timer in production are going to be flaky. Use signals such as network events, selectors becoming visible and others instead.

page.wait_for_timeout(1000)

Shortcut for main frame's Frame#wait_for_timeout.

wait_for_url

def wait_for_url(url, timeout: nil, waitUntil: nil)

Waits for the main frame to navigate to the given URL.

page.click("a.delayed-navigation") # clicking the link will indirectly cause a navigation
page.wait_for_url("**/target.html")

Shortcut for main frame's Frame#wait_for_url.

expect_websocket

def expect_websocket(predicate: nil, timeout: nil, &block)

Performs action and waits for a new WebSocket. If predicate is provided, it passes WebSocket value into the predicate function and waits for predicate.call(web_socket) to return a truthy value. Will throw an error if the page is closed before the WebSocket event is fired.

expect_worker

def expect_worker(predicate: nil, timeout: nil, &block)

Performs action and waits for a new Worker. If predicate is provided, it passes Worker value into the predicate function and waits for predicate(worker) to return a truthy value. Will throw an error if the page is closed before the worker event is fired.

workers

def workers

This method returns all of the dedicated WebWorkers associated with the page.

NOTE: This does not contain ServiceWorkers

accessibility

DEPRECATED This property is deprecated. Please use other libraries such as Axe if you need to test page accessibility. See our Node.js guide for integration with Axe.

keyboard

mouse

request

API testing helper associated with this page. This method returns the same instance as BrowserContext#request on the page's context. See BrowserContext#request for more details.

touchscreen