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?"
Hi, if you know which URL to expect, you can add a https://playwright.dev/java/docs/api/class-page#page-wait-for-url
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
Related Ask AI answer for "How do I retrieve the updated URL after a page 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.
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].