Skip to main content
Version: 1.17

Verification

Console logs

Console messages logged in the page can be brought into the Playwright context.

# Listen for all console logs
page.on("console", lambda msg: print(msg.text))

# Listen for all console events and handle errors
page.on("console", lambda msg: print(f"error: {msg.text}") if msg.type == "error" else None)

# Get the next console log
with page.expect_console_message() as msg_info:
# Issue console.log inside the page
page.evaluate("console.log('hello', 42, { foo: 'bar' })")
msg = msg_info.value

# Deconstruct print arguments
msg.args[0].json_value() # hello
msg.args[1].json_value() # 42

API reference


Page errors

Listen for uncaught exceptions in the page with the pagerror event.

# Log all uncaught errors to the terminal
page.on("pageerror", lambda exc: print(f"uncaught exception: {exc}"))

# Navigate to a page with an exception.
page.goto("data:text/html,<script>throw new Error('test')</script>")

API reference


Page events

"requestfailed"

page.on("requestfailed", lambda request: print(request.url + " " + request.failure.error_text))

"dialog" - handle alert, confirm, prompt

page.on("dialog", lambda dialog: dialog.accept())
with page.expect_popup() as popup_info:
page.click("#open")
popup = popup_info.value

API reference