Rayrun
← Back to Discord Forum

page.url() is returning the previous url instead of current url

Hi, currently I am using Playwright to login a website, after I click the submit button, it will redirect to the url, for an example:

try(Playwright playwright = Playwright.create()) {
    BrowserType chromium = playwright.chromium();
            Browser browser = chromium.launch(new           BrowserType.LaunchOptions().setHeadless(false).setChannel("chrome"));
            BrowserContext bc = browser.newContext();
            Page page = bc.newPage();
            page.navigate("htttps://abc.com");
            
            page.locator("input[name=username]").type("foo");
            page.locator("input[name=password]").type("foofoofoo123");
            page.locator("button[type=submit]").click();
            
            //here the browser will redirect to new domains, e.g.: https://def.com/dashboard/
            
            System.out.print("url = " + page.url()) //but when I call the page.url() it still returning the https://abc.com/ not the redirect's one.  
     
  } catch (Exception ex) {
    System.out.print(ex.getMessage());
    ex.printStackTrace()
  }

Does anyone knows how to deal with it?

This thread is trying to answer question "Why is page.url() returning the previous URL instead of the current one after a redirect, and how can this be fixed?"

10 replies
refactoreric

Hi, if you know which URL to expect, you can add a https://playwright.dev/java/docs/api/class-page#page-wait-for-url

thanks, both solutions works. but which one is better?

@mxschmitt: but , I want get the page current url

but , I want get the page current url

Possibly something is out of sync, an alternative would be to run some javascript and get the url fro window.locaiont.href. See what it says,... Same, different?

Ru opening the new url in new tab or in the current tab

if the new url is loading in new tab then u have to switch new tab then u will be able to get the current url

@_0rz9: Ru opening the new url in new tab or in the current tab

Ru opening the new url in new tab or in the current tab

Anyone know how to solve this? Having the same issue rn

refactoreric

Hi, don't the suggestions given above work for you?

refactoreric — 04/14/2023 9:55 PM Hi, if you know which URL to expect, you can add a https://playwright.dev/java/docs/api/class-page#page-wait-for-url mxschmitt — 04/15/2023 6:12 AM https://playwright.dev/java/docs/api/class-pageassertions#page-assertions-to-have-url

Answer

Retrieving the Current URL After a Redirect in Playwright

You can easily get the current URL after a redirect in Playwright using the page.url() method. This method returns the URL of the current page.

Here's a quick example:

import { chromium } from 'playwright';

async function getCurrentURL() {
  const browser = await chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();

  // Navigate to a page that performs a redirect
  await page.goto('https://ray.run/redirect');

  // Wait for the navigation to complete
  await page.waitForNavigation();

  // Get the current URL after the redirect
  const url = page.url();
  
  console.log(url); // Output: https://ray.run/redirected-page

  await browser.close();
}

getCurrentURL();

In this code, we're launching a Chromium browser, creating a new context and page, and then navigating to a webpage that performs a redirect. After the redirect is complete, we're using page.url() to retrieve the updated URL.

Remember to wait for navigation events using methods like waitForNavigation() before retrieving or interacting with elements on redirected pages. This ensures that Playwright has finished loading and processing all necessary resources before proceeding with further actions.

For more tips on efficient Playwright test scripts, check out this blog post.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].