Last updated on August 1st, 2025 at 04:33 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.
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);
});

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.
Basic Playwright Tutorial Quick Links
- Get Page Title Using page.title()
- Select DropDown Value Using selectOption()
- Simulate the Right Click Using the click() method
- Select Checkboxes Using check() and setChecked() Methods
- Clear Input Text Field Value in Playwright
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.
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 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.

Hi, I’m Aravind — a seasoned Automation Test Engineer with over 17 years of hands-on experience in the software testing industry. I specialize in tech troubleshooting and tools like Selenium, Playwright, Appium, JMeter, and Excel automation. Through this blog, I share practical tutorials, expert tips, and real-world insights to help testers and developers improve their automation skills.In addition to software testing, I also explore tech trends and user-focused topics, including Snapchat guides, codeless test automation, and more.