Puppeteer: Get Current URL
Use these methods to get the current URL of one or multiple pages within a Puppeteer browser instance.
Puppeteer is an immensely popular control API for Chrome. It is well-supported, updated often, and has hundreds of different methods. Since developers primarily use it to navigate web pages, it’s helpful to be able to get the current URL easily. Here is how to grab URLs from Puppeteer instances and several other valuable methods.
Get the Current URL in Puppeteer
Use the page.url()
method to return a URL string of the current page. Here is a full coding example of how you would use the page.url()
method.
const puppeteer = require('puppeteer')
;(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('https://techozu.com')
let currentURL = page.url()
console.log(currentURL) // https://techozu.com
await browser.close()
});
Getting the current URL of the Puppeteer instance is useful if, at some point, the browser performed redirects or your code performed any clicks.
Note that page.url()
is not an asynchronous operation and does not require you to append await
to it.
Get All Open URLs in Puppeteer
Use the browsers.pages() method to return all current pages open in the browser instance. Save this information to a variable and then loop over the variable to get all of the URLs. Here is a coding example:
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('https://example.com')
const page2 = await browser.newPage()
await page2.goto('https://techozu.com')
let pages = await browser.pages()
pages.forEach((p) => {
console.log(p.url())
})
// about:blank
// https://exmaple.com
// https://techozu.com
await browser.close()
The example above loops through each page and calls the page.url()
method, but you can also use the map
function if you want simply get an array of open URLs.
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('https://example.com')
const page2 = await browser.newPage()
await page2.goto('https://techozu.com')
let pages = await browser.pages()
let urls = pages.map((p) => p.url())
console.log(urls)
// [ 'about:blank', 'https://example.com/', 'https://techozu.com/' ]
await browser.close()
Other Useful Puppeteer URL Methods
Here are a few other methods that you may find useful when working with URLs in puppeteer.
Use page.goto(URL)
to navigate to a URL on the referenced page.
await page.goto('https://techozu.com')
Use page.goBack()
to navigate backward in the browsing history
await page.goBack()
Use page.goForward()
to navigate forward in the browsing history
await page.goForward()
Use page.close()
to close the referenced page but not the browser instance itself.
await page.close()
Those are just a few functions that might come in handy. You can read the full API documentation here.
We hope the info above helps you with your coding project. You can check out our Javascript Section for more helpful guides. Happy Coding!