class Playwright::Page

“‘
page.remove_listener(“request”, log_request)
# sometime later…
page.on(“request”, log_request)
print(“a request was made:”, intercepted_request.url)
def log_request(intercepted_request):
“`py
To unsubscribe from events use the `removeListener` method:
“`
page.once(“load”, lambda: print(“page loaded!”))
“`py
This example logs a message for a single page `load` event:<br><br>`removeListener`.<br>(nodejs.org/api/events.html#events_class_eventemitter) methods, such as `on`, `once` or
The Page class emits various events (described below) which can be handled using any of Node’s native
“‘
run(playwright)
with sync_playwright() as playwright:
browser.close()
page.screenshot(path=“screenshot.png”)
page.goto(“example.com”)
page = context.new_page()
context = browser.new_context()
browser = webkit.launch()
webkit = playwright.webkit
def run(playwright: Playwright):
from playwright.sync_api import sync_playwright, Playwright
“`python sync
This example creates a page, navigates it to a URL, and then saves a screenshot:
instance might have multiple `Page` instances.
[extension background page](developer.chrome.com/extensions/background_pages) in Chromium. One `Browser`
Page provides methods to interact with a single tab in a `Browser`, or an
- extends: [EventEmitter]

def accessibility # property

property
def accessibility # property
  wrap_impl(@impl.accessibility)
end

def add_init_script(path: nil, script: nil)

[`method: Page.addInitScript`] is not defined.
**NOTE**: The order of evaluation of multiple scripts installed via [`method: BrowserContext.addInitScript`] and

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

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

**Usage**

the JavaScript environment, e.g. to seed `Math.random`.
The script is evaluated after the document was created but before any of its scripts were run. This is useful to amend

- Whenever the child frame is attached or navigated. In this case, the script is evaluated in the context of the newly attached frame.
- Whenever the page is navigated.
Adds a script which would be evaluated in one of the following scenarios:
def add_init_script(path: nil, script: nil)
  wrap_impl(@impl.add_init_script(path: unwrap_impl(path), script: unwrap_impl(script)))
end

def add_locator_handler(locator, handler)

```
page.get_by_role("button", name="Start here").click()
page.goto("https://example.com")
# Write the test as usual.

page.add_locator_handler(page.locator("body"), handler)
page.evaluate("window.removeObstructionsForTestIfNeeded()")
def handler():
# Setup the handler.
```python sync

An example with a custom callback on every actionability check. It uses a `` locator that is always visible, so the handler is called before every actionability check:

```
page.get_by_role("button", name="Start here").click()
page.goto("https://example.com")
# Write the test as usual.

page.add_locator_handler(page.get_by_text("Confirm your security details"), handler)
page.get_by_role("button", name="Remind me later").click()
def handler():
# Setup the handler.
```python sync

An example that skips the "Confirm your security details" page when it is shown:

```
page.get_by_role("button", name="Start here").click()
page.goto("https://example.com")
# Write the test as usual.

page.add_locator_handler(page.get_by_role("button", name="Accept all cookies"), handler)
page.get_by_role("button", name="Reject all cookies").click()
def handler():
# Setup the handler.
```python sync

An example that closes a cookie dialog when it appears:

**Usage**

Another example is a series of mouse actions, where [`method: Mouse.move`] is followed by [`method: Mouse.down`]. Again, when the handler runs between these two actions, the mouse position will be wrong during the mouse down. Prefer methods like [`method: Locator.click`] that are self-contained.




For example, consider a test that calls [`method: Locator.focus`] followed by [`method: Keyboard.press`]. If your handler clicks a button between these two actions, the focused element most likely will be wrong, and key press will happen on the unexpected element. Use [`method: Locator.press`] instead to avoid this problem.




**NOTE**: Running the interceptor will alter your page state mid-test. For example it will change the currently focused element and move the mouse. Make sure that the actions that run after the interceptor are self-contained and do not rely on the focus and mouse state.

You can register multiple handlers. However, only a single handler will be running at a time. Any actions inside a handler must not require another handler to run.

Note that execution time of the handler counts towards the timeout of the action/assertion that executed the handler.

This method registers a handler for an overlay that is executed once the locator is visible on the page. The handler should get rid of the overlay so that actions blocked by it can proceed. This is useful for nondeterministic interstitial pages or dialogs, like a cookie consent dialog.

Sometimes, the web page can show an overlay that obstructs elements behind it and prevents certain actions, like click, from completing. When such an overlay is shown predictably, we recommend dismissing it as a part of your test flow. However, sometimes such an overlay may appear non-deterministically, for example certain cookies consent dialogs behave this way. In this case, [`method: Page.addLocatorHandler`] allows handling an overlay during an action that it would block.
def add_locator_handler(locator, handler)
  raise NotImplementedError.new('add_locator_handler is not implemented yet.')
end

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

fires or when the script content was injected into frame.
Adds a `
document.addEventListener('click', event => window.clicked(event.target));

}
document.querySelector('div').textContent = await window.pageURL();
async function onClick() {

}
document.querySelector('div').textContent = await window.sha256('PLAYWRIGHT');
async function onClick() {