How do I handle timeouts and retries in Puppeteer?
Davide S
davide s profile pic

Handling timeouts and retries in Puppeteer involves implementing logic to handle scenarios where certain operations take longer than expected or encounter errors. Here's a detailed explanation of how to handle timeouts and retries in Puppeteer: 1. Launching a new browser instance and creating a new page:

1
2
3
4
5
6
7
8
9
10
11
12
13

   const puppeteer = require('puppeteer');

   (async () => {
     const browser = await puppeteer.launch();
     const page = await browser.newPage();

     // Perform actions with the page here

     // Close the browser
     await browser.close();
   })();
   

This code sets up a basic Puppeteer script. It launches a new headless browser instance and creates a new page to work with. 2. Handling timeouts withpage.setDefaultTimeout(): Puppeteer provides a method calledpage.setDefaultTimeout() that allows you to set a default timeout for all operations on the page.

1
2
3

   page.setDefaultTimeout(5000); // Set the default timeout to 5 seconds
   

In this example, the default timeout is set to 5 seconds (5000 milliseconds). This means that any Puppeteer operations that don't have a specific timeout set will use this default timeout value. 3. Handling individual timeouts withpage.waitFor*() methods: Puppeteer provides variouspage.waitFor*() methods that allow you to wait for specific conditions or events with a custom timeout.

1
2
3

   await page.waitForSelector('your-selector', { timeout: 3000 }); // Wait for selector with a timeout of 3 seconds
   

In this code snippet,page.waitForSelector() is used to wait for a selector to appear on the page. Thetimeout option is set to 3000 milliseconds (3 seconds) to specify a custom timeout for this particular operation. 4. Implementing retries with a loop: To handle retries, you can use a loop along withtry andcatch blocks to repeat an operation multiple times until it succeeds or reaches a maximum retry count.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

   const maxRetries = 3;
   let retries = 0;
   let success = false;

   while (retries < maxRetries && !success) {
     try {
       // Perform the operation that needs retries
       // If successful, set success = true

       success = true; // Set success to true to exit the loop if the operation is successful
     } catch (error) {
       console.error('Operation failed:', error);
       retries++; // Increment the retry count on failure
     }
   }

   if (!success) {
     console.error('Operation failed after maximum retries');
   }
   

In this example, the operation that needs retries is placed inside the loop. If the operation fails, an error is caught, and the retry count is incremented. The loop continues until the operation succeeds or the maximum retry count is reached. If the operation fails after the maximum retries, an appropriate error message is displayed. By following these steps, you can handle timeouts and retries in Puppeteer. By setting a default timeout usingpage.setDefaultTimeout(), you can define a timeout duration for all operations. Additionally, you can set custom timeouts for individual operations using the appropriatepage.waitFor*() methods. Implementing retries with a loop and appropriate error handling allows you to repeat operations until they succeed or reach a maximum retry count, ensuring more robust automation in scenarios where operations may encounter delays or failures.