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.
If you still have questions, please ask a question and I will try to answer it.
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].