Handling errors in Puppeteer, a Node.js library that provides a high-level API to control Chrome or Chromium over the DevTools Protocol, is crucial for building a robust web scraping or testing setup.
Basic Error Handling
First, let's discuss basic error handling. In Puppeteer, most operations are asynchronous and return promises. Therefore, using try/catch
blocks or .catch()
method on promises is a common way to handle potential errors.
Here's an example:
const puppeteer = require('puppeteer');
(async () => {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
} catch (error) {
console.error('An error occurred:', error);
}
})();
Handling Timeout Errors
Puppeteer operations can throw timeout errors if they're not able to complete within a certain timeframe. This can be handled by either setting a larger timeout or catching the timeout error and handling it.
const puppeteer = require('puppeteer');
(async () => {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Set a larger navigation timeout
await page.setDefaultNavigationTimeout(60000);
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
} catch (error) {
// Handle timeout errors specifically
if (error instanceof puppeteer.errors.TimeoutError) {
console.error('A timeout error occurred:', error);
} else {
console.error('An error occurred:', error);
}
}
})();
Handling Network Errors
Sometimes, you may encounter network errors, such as when a resource fails to load. You can handle these by listening to the page.on('requestfailed')
event.
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.on('requestfailed', request => {
console.error(`Request failed: ${request.url()}`);
});
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();
By effectively handling errors in Puppeteer, you can ensure that your scripts are robust and resilient, capable of dealing with unexpected situations. Remember, error handling is just as important as the core functionality of your scripts!