Playwright current url

Playwright Current URL

The current URL of a webpage in Playwright can be retrieved using the page.url() function.

Here’s an example:

// Launching a browser instance
const { chromium } = require('playwright');
(async () => {
  const browser = await chromium.launch();
  
  // Creating a new page
  const context = await browser.newContext();
  const page = await context.newPage();
  
  // Navigating to a URL
  await page.goto('https://www.example.com');
  
  // Retrieving the current URL
  const currentURL = await page.url();
  console.log(currentURL);
  
  await browser.close();
})();

In the above example, we first launch a Chromium browser instance using chromium.launch(). Then, we create a new context and a new page within that context. The page navigates to the URL ‘https://www.example.com’ using page.goto() method.

Finally, we retrieve the current URL using page.url() and print it to the console. In this case, the current URL will be ‘https://www.example.com’.

Leave a comment