Playwright Get Current URL

Profile picture for user arilio666

In Playwright, we can get the current page URL Using the url() method linked with page instance.

Table of Contents

  1. Syntax
  2. Example
  3. Video Tutorial

Syntax

page.url()

Example

test.only('Get Current URL', async({page}) =>{
    await page.goto('https://www.programsbuzz.com')
    await page.locator("li.we-mega-menu-li>a[href='/about-us-programsbuzz']").click()
    console.log(page.url())
})

Once you get the URL, you may want to assert the URL; for that, a different method exists; refer Verify URL in Playwright.

Also, sometimes client-side router doesn't immediately change the URL after clicking on the button or any other element that triggers the navigation. In that case, you can use waitForURL() method.

Video Tutorial: How to Get Current URL Playwright