How to set Playwright not automatically follow the redirect?

Viewed 1307

I want to open a website using Playwright, but I don't want to be automatically redirected.

In some other web clients, they have parameter link follow=False to disable automatically following the redirection. But I can't find it on Playwright.

async def run(playwright):
    chromium = playwright.chromium
    browser = await chromium.launch()
    context = await browser.new_context()
    page = await context.new_page()

    def handle_response(response):
        print(f'status: {response.status} {response.url}')
    page.on('response', handle_response)

    await page.goto("https://google.com")
    await browser.close()

that's the sample code, as we know google.com would respond 301 and will be redirected to www.google.com. Is it possible to stop the process after I got the 301, so I don't need to continue processing www.google.com and all the responses after that?

From the Request documentation, I got that Page.on('response') emitted when/if the response status and headers are received for the request.

But how to stop the Request after the Page got on('response') callback? I saw some other questions similar, using Route.abort() or Route.fulfill(), but I still don't get the answer for my case.

thank you for your help.

0 Answers
Related