0

I am using Playwright in Python to collect data from some websites. As part of my instrumentation, I am clicking certain elements on the webpage. Some of these open up a new tab and after multiple redirects reach a final landing page. I want to capture the sequence of redirects that happens right up to the final landing page. How do I achieve this?

I tried adding the following listener on my browser context, but it prints only the final landing URL:

def handle_request3(route, request):
    # Prints out the URL that the request was redirected to
    route.continue_()
    response = route.request.response()
    if response is not None and "url" in response.all_headers():
        print(f"Redirected to {response.url}")

    # If the response was redirected to a new URL, wait for the new tab to load
    if response.status == 302 and 'location' in response.headers:
        new_url = response.headers['location']
        print(f"Redirection to {new_url}")
        browser.contexts[0].route("**", handle_request3)

ggorlen
  • 44,755
  • 7
  • 76
  • 106

0 Answers0