Navigate to URLs and monitor page loading with the Page navigation API.
Methods
page.goto()
Navigates to a URL. Returns true if navigation succeeded, false otherwise.
page.goto(url: string, headers?: Record<string, string>, timeout?: number): Promise<boolean>
Parameters:
url: URL to navigate to
headers (optional): Custom HTTP headers to send with the request
timeout (optional): Navigation timeout in milliseconds (default: varies)
Returns: Promise<boolean> - true if navigation succeeded
Examples:
// Basic navigation
await page.goto('https://example.com')
// With custom headers
await page.goto('https://api.example.com', {
'Authorization': 'Bearer token123',
'X-Custom-Header': 'value'
})
// With timeout (60 seconds)
await page.goto('https://slow-site.com', {}, 60000)
Use waitForSelector() after goto() to ensure specific content has loaded before proceeding.
page.url()
Gets the current page URL.
page.url(): Promise<string>
Returns: Promise<string> - Current URL
Example:
await page.goto('https://example.com/page1')
const url = await page.url()
console.log('Current URL:', url) // https://example.com/page1
// Check if navigation succeeded
await page.click('#link')
await new Promise(resolve => setTimeout(resolve, 1000))
const newUrl = await page.url()
console.log('Navigated to:', newUrl)
page.progress()
Gets page load progress as a percentage (0-100).
page.progress(): Promise<number>
Returns: Promise<number> - Load progress percentage
Example:
page.goto('https://example.com')
// Poll progress while loading
const interval = setInterval(async () => {
const progress = await page.progress()
console.log(`Loading: ${progress}%`)
if (progress === 100) {
clearInterval(interval)
console.log('Page fully loaded')
}
}, 500)
Complete examples
Navigation with verification
const targetUrl = 'https://example.com/dashboard'
const success = await page.goto(targetUrl)
if (!success) {
throw new Error('Navigation failed')
}
// Verify we're on the right page
const currentUrl = await page.url()
if (!currentUrl.includes('/dashboard')) {
throw new Error('Wrong page loaded')
}
console.log('Successfully navigated to dashboard')
Handling redirects
const initialUrl = 'https://example.com/redirect-me'
await page.goto(initialUrl)
// Wait a bit for redirect
await new Promise(resolve => setTimeout(resolve, 2000))
const finalUrl = await page.url()
console.log('Redirected from:', initialUrl)
console.log('Redirected to:', finalUrl)
Authenticated navigation
// Navigate with auth header
const success = await page.goto('https://api.example.com/data', {
'Authorization': 'Bearer ' + accessToken,
'Accept': 'application/json'
})
if (!success) {
console.error('Failed to access protected resource')
return
}
// Extract data
const data = await page.evaluate(() => {
const pre = document.querySelector('pre')
return pre ? JSON.parse(pre.textContent) : null
})
console.log('API response:', data)
Progress monitoring
async function navigateWithProgress(url) {
console.log('Starting navigation to:', url)
// Start navigation
const navigationPromise = page.goto(url)
// Monitor progress
let lastProgress = 0
const progressInterval = setInterval(async () => {
const progress = await page.progress()
if (progress !== lastProgress) {
console.log(`Progress: ${progress}%`)
lastProgress = progress
}
if (progress === 100) {
clearInterval(progressInterval)
}
}, 200)
// Wait for navigation to complete
await navigationPromise
console.log('Navigation complete')
}
await navigateWithProgress('https://example.com')
Single-page application navigation
// For SPAs that use client-side routing
await page.goto('https://spa-example.com')
// Click a navigation link
await page.click('#nav-link')
// Wait for URL to change
const initialUrl = await page.url()
let newUrl = initialUrl
while (newUrl === initialUrl) {
await new Promise(resolve => setTimeout(resolve, 100))
newUrl = await page.url()
}
console.log('Navigated to:', newUrl)
// Or wait for content to change
await page.waitForSelector('.new-page-content')
Error handling
try {
const success = await page.goto('https://example.com', {}, 30000)
if (!success) {
console.error('Navigation returned false')
}
} catch (error) {
if (error.message.includes('timeout')) {
console.error('Navigation timed out after 30 seconds')
} else {
console.error('Navigation error:', error)
}
}