10

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.

MeT
  • 675
  • 3
  • 6
  • 21
oon arfiandwi
  • 515
  • 1
  • 8
  • 21
  • 2
    I'm following this pull request to see if it solves this issue: https://github.com/microsoft/playwright/pull/17033 – Zev Sep 06 '22 at 12:57

1 Answers1

0

As introduced in v1.26,may use maxRedirects optional argument in get requests.

This provides as maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20. Pass 0 to not follow redirects.

References:

https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-get-option-max-redirects

https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-get-option-max-redirects

https://github.com/microsoft/playwright/pull/17033

https://github.com/microsoft/playwright/issues/12911

Vishal Aggarwal
  • 1,929
  • 1
  • 13
  • 23