Playwright + Python · Lesson 24 of 37

0/37 done

Module 24: Browser Events

24. Real-Time Event Listeners: Console Logs & Page Errors

15 minlesson

Your task

Implement capture_console_errors(page, error_list):

  1. page.on("console", lambda msg: error_list.append(msg.text) if msg.type == "error" else None)

Write it in the editor, then press Check answer (Ctrl + Enter).

Listening to Browser Events in Python

python
page.on("pageerror", lambda err: print(f"Frontend JS Exception: {err}"))

def on_console(msg):
    if msg.type == "error":
        print(f"Console error: {msg.text}")

page.on("console", on_console)

Key Takeaways & Walkthrough Summary

Playwright allows you to listen to internal browser events in real time. In this lesson, you will capture console errors, unhandled JavaScript exceptions, and network traffic during test execution. Let's monitor browser events!

Part of the Playwright + Python course (Playwright with Python). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Can I assert that zero console errors occurred during my test?

Yes! Collecting console errors in a list and asserting len(errors) == 0 in teardown catches frontend regressions.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Python
Skip