[Python] Playwright Python How to receive return value using page.on("response")

Discussão em 'Python' iniciado por Stack, Julho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to catch contents of response using Playwright Python, but I'm not sure how to achieve this. Here's my sample code.

    from playwright.sync_api import Playwright, sync_playwright

    def handle_response(response):
    if "api/list" in response.url and response.status == 200:
    return response.json() # Want to return this value to caller function

    def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(channel="chrome", headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.on("response", handle_response) # Want to receive return value from handle_response()
    page.goto("https://www.example.com/")
    # Do some stuff based on the return value
    context.close()
    browser.close()

    with sync_playwright() as playwright:
    run(playwright)


    This could be a silly question, but your help is highly appreciated. Thank you for your time to look into this.

    Reference: https://playwright.dev/python/docs/next/api/class-page#page-event-response

    Continue reading...

Compartilhe esta Página