Playwright: How to Get Current URL (With Examples)
When automating browser actions using Playwright, it's often necessary to check or store the current URL — whether for debugging, redirection checks, or test assertions.
This guide shows how to get the current page URL in Playwright with simple code examples and useful tips.
✅ Quick Answer
const currentUrl = page.url();
console.log(currentUrl);
- Returns the current page URL as a string
- No need to
await
— it’s a synchronous method
🧪 Full Example
import { test, expect } from '@playwright/test';
test('should redirect to dashboard after login', async ({ page }) => {
await page.goto('https://example.com/login');
await page.fill('#username', 'admin');
await page.fill('#password', 'password123');
await page.click('button[type="submit"]');
await page.waitForURL('**/dashboard');
const url = page.url();
expect(url).toContain('/dashboard');
});
🔍 When to Use page.url()
Use Case | Description |
---|---|
Check after redirect | Validate redirection paths after actions |
Debugging failed tests | Log current URL on failure |
Capture dynamic routes | Test route-based navigation (e.g. /user/123 ) |
Compare with expected URL | Perform assertions on navigation flow |
🔗 Combine With waitForURL()
await Promise.all([
page.waitForURL('**/dashboard'),
page.click('text=Go to Dashboard'),
]);
console.log('Arrived at:', page.url());
- Ensures navigation is complete before checking the URL
🧠 Pro Tips
- No
await
needed —page.url()
is synchronous - Use wildcard patterns (
**
) withwaitForURL()
for flexibility - Great for asserting query params or anchor tags:
expect(page.url()).toContain('?status=success');
🧰 Edge Case: Inside Frames or Popups
If working with frames:
const frame = page.frame({ name: 'my-frame' });
const frameUrl = frame?.url();
Or for a popup page:
const [popup] = await Promise.all([
page.waitForEvent('popup'),
page.click('a[target=_blank]'),
]);
console.log('Popup URL:', popup.url());
🏁 Conclusion
To get the current page URL in Playwright:
const url = page.url();
Use it to debug, assert, or log page navigation and ensure your automation flows correctly through your app.