How to Get the Current Page URL in Playwright Using page.url()

Get the Current Page URL Using page.url() in Playwright

This quick tutorial will show you how to get the current page URL in Playwright using the page.url() method. You’ll learn when and how to use this function to capture the active browser URL during your automation flow.

In real-world playwright test automation, you need the current page URL to verify navigation, perform conditional actions, or debug your script. You can use the page.url() method to get the current page’s URL in JavaScript.

Why You Need to Get the Current URL in Playwright

You need the current page URL in Playwright to validate the redirection after login or navigation, to check if you are on the correct page before taking action, to debug failed test cases, or to create conditional flows based on the URL.

Playwright: Get the Current Page URL in JavaScript

In JavaScript or TypeScript, you can use the page.url() method to get the current page URL.

JavaScript Example: Get Current URL in Playwright

const { test, expect } = require('@playwright/test');

test('Get current page URL', async ({ page }) => {
  await page.goto('https://www.google.com/');
  const newString = page.url();
  console.log("Current Page URL is: "+newString);   
});
JavaScript code example showing how to get the current page URL using Playwright.

Code Breakdown:

  • const newString = page.url(): This syntax will return the full URL of the page as a string and store it in the variable.
  • console.log(“Current Page URL is: “+newString): It will print the URL in the console.

How to Use URL in Playwright Assertions

You can also use the current URL to verify if the navigation was successful.

expect(page.url()).toBe('Expected url');

How to Compare Actual and Expected URL in Playwright

Once you’ve navigated to a page, you might want to assert that the current page URL matches the expected URL. This is useful for verifying:

  • Successful login or logout
  • Redirects after form submission
  • Navigation to the correct page after a button click

Let’s see how to compare the actual URL with the expected one in JavaScript.

JavaScript Example: Compare URLs in Playwright

const { test, expect } = require('@playwright/test');

test('Get current page URL', async ({ page }) => {
  await page.goto('https://only-testing-blog.blogspot.com/2025/04/playwright-practice-page.html');
  await page.getByRole('link', { name: 'Go to Home Page' }).click();
  const actualUrl = page.url();
  const expectedUrl = 'https://only-testing-blog.blogspot.com/';
  if (actualUrl === expectedUrl) {
    console.log('✅ URL matched:', actualUrl);
  } else {
    console.error('❌ URL mismatch!');
    console.log('Expected:', expectedUrl);
    console.log('Actual  :', actualUrl);
  }
});

In the above example, we have compared actual and expected URLs and printed the comparison result in a console.

Best Practices for Comparing URLs

  • Always use full URLs when possible for accuracy.
  • You can use page.waitForURL() to wait for navigation before checking the URL.
  • If the URL contains dynamic parts (e.g., query parameters), you can consider using regular expressions or a partial match.

Related Articles

Frequently Asked Questions – FAQ

How do I get the current URL of the Playwright?

Use the page.url() Method to fetch the current page’s URL in Playwright automation. It returns the URL as a string and is commonly used to verify navigation or redirection.

Example:

const currentUrl = page.url();
console.log(‘Current URL:’, currentUrl);

This is useful for debugging or asserting expected URLs in your test scripts.

How to navigate to a URL in Playwright?

In Playwright, use the page.goto() method to navigate to a specific URL. It waits for the page to load before proceeding, making it ideal for starting tests.

Example:

await page.goto(‘https://example.com’);

You can also pass options like waitUntil to control the loading behavior.

How to check link in Playwright?

To verify a link in Playwright, use a locator to find the anchor tag (<a>) and retrieve its href attribute using getAttribute().

Example:

const link = await page.locator(‘a#my-link’).getAttribute(‘href’);
console.log(link);

This helps validate that the link exists and points to the correct URL, especially useful in UI validations or broken link checks.

Leave a Reply

Your email address will not be published. Required fields are marked *