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

Last updated on April 18th, 2026 at 09:24 am

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.

In many test scenarios, checking the URL alone is not enough and is often combined with other validations like verifying the page title to confirm correct navigation.

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');

In many test scenarios, checking the URL alone is not enough. You may also need to get the page title in Playwright to confirm that the correct page has loaded.

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.

Comparing URLs too early can sometimes give incorrect results if navigation is still in progress. It is better to wait for the page or element to be visible before validating the URL.

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.

author avatar
Aravind QA Automation Engineer & Technical Blogger
Aravind is a QA Automation Engineer and technical blogger specializing in Playwright, Selenium, and AI in software testing. He shares practical tutorials to help QA professionals improve their automation skills.

Leave a Reply

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

Are you human? Please solve:Captcha