'intercepting response with substring in url using playwright

I have been learning playwright on python, but it appears that I cannot get it to successfully find a response whose URL contains a substring, while on node I am indeed able to do so, is there anything I am doing wrong?

async with page.expect_response("*") as response:
    if "getVerify" in response.url:
        print("found")

i have also tried using getVerify and in to no avail.

node code:

page.on('response', response => {
    if (response.url().includes('getVerify')) {
        console.log(response.url())


Solution 1:[1]

With the node case, it's a bit different as you're passively subscribing to an event. In the python snippet, you are basically doing the equivalent of page.waitForResponse. Typically you'll do that in conjunction with some action that triggers the response (such as submitting a form).

Try using the python page.on API. Like this:

import asyncio
from playwright.async_api import async_playwright

def check_response(response):
    print(response.url)
    if 'getVerify' in response.url:
        print("Response URL: ", response.url)

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch()
        page = await browser.new_page()
        page.on("response", check_response)
        await page.goto("http://playwright.dev")
        print(await page.title())
        await browser.close()

asyncio.run(main())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nico Mee