How to wait for N seconds in Puppeteer

Puppeteer is a Node library that provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.

This article will demonstrate the options you can use in order to add a time delay in Puppeteer before executing the next line. There are a few options to do so, we can either build our own functionality to add some delay, or we can use the Puppeteer built-in functions, Let’s check both options and see how it can be done.


Examples

1- Using custom delay implementation.

We can create the following JS delay function, that will delay the execution of the next line for N seconds.

const delay = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));

In the above function, we’ve passed a parameter milliseconds to the setTimeout function, along with the resolve function from the Promise.

The setTimeout will call the resolve function after it waits for the milliseconds time, then the delay function will return to its outer scope and will continue in executing the next line.

Samples:

const delay = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));

async function LaunchBrowser() {
    console.log('Starting...');
    // let browser = await puppeteer.launch();
    // 👇👇 Wait for 2 seconds
    await delay(2000);
    // 👇👇 This log will show after 2 seconds
    console.log('Logging after 2 seconds');
    // 👇👇 Wait for 5 seconds
    await delay(5000);
    // 👇👇 This log will show after 5 seconds
    console.log('Logging after 5 seconds');
}

(async () => {
    await LaunchBrowser();
})();

And so on and so forth you can call the delay function whenever you want and can pass the suitable waiting time as a parameter.

Note that, every 1000 milliseconds = 1 second, and because the setTimeout function accepts the delay parameter in milliseconds, that’s why we had to pass the time in milliseconds.


2- Using Puppeteer built-in wait functions.

Puppeteer comes with already built-in wait functions for different use cases, an example of those functions:

  • waitForSelector: Wait for the selector to appear in page. If at the moment of calling the method the selector already exists, the method will return immediately. If the selector doesn’t appear after the timeout milliseconds of waiting, the function will throw.
  • waitForXPath: Wait for the xpath to appear in page. If at the moment of calling the method the xpath already exists, the method will return immediately. If the xpath doesn’t appear after the timeout milliseconds of waiting, the function will throw.
  • waitForNavigation: Waits for the page to navigate to a new URL or to reload. It is useful when you run code that will indirectly cause the page to navigate.
  • waitForNetworkIdle: Waits for when the network is idle.
  • waitForResponse: Waits for HTTP request response.
  • waitForFunction: Waits for a function to finish evaluating in the page/frame context.

There are also some other functionalities that come from Puppeteer you can use for waiting.


Wrap

It’s much better and easier to go with the puppeteer built-in functionalities to wait for something to happen and act accordingly, I’m pretty sure Puppeteer does cover most of the use-cases anyone would face.

If Puppeteer functionalities still not providing what you want, then go for the custom delay implementation we’ve added in Option1.


Photo from Unsplash.

Related Posts

How to Capture Screenshots with Puppeteer In NodeJS

How to Capture Screenshots with Puppeteer In NodeJS

To Capture Screenshots with Puppeteer: Launch a Browser Instance Navigate to the Web Page Capture the Screenshot Introduction: Puppeteer is a powerful Node.js library that allows developers…

How to Minimize Puppeteer Browser Window To Tray

How to Minimize Puppeteer Browser Window To Tray

Puppeteer is a powerful tool for automating tasks in headless or non-headless web browsers using JavaScript. While Puppeteer is often used to perform actions within a browser,…

Intercepting Responses in Node.js with Puppeteer

Intercepting Responses in Node.js with Puppeteer

Introduction: Puppeteer is a powerful Node.js library that provides a high-level API for controlling headless Chrome or Chromium browsers. It’s widely used for web scraping, automated testing,…

Mastering React Component Re-rendering in Jest

Mastering React Component Re-rendering in Jest

In this hands-on guide, we’ll explore the art of optimizing React component re-rendering within Jest tests. By combining theory with practical coding examples, you’ll gain a deep…

Eliminating Nesting Loops in React Rendering

Eliminating Nesting Loops in React Rendering

React has ushered in a new era of web application development with its component-based structure, promoting code reusability and maintainability. But as projects evolve, achieving optimal performance…

Exploring Type and Interface Usage in TypeScript

Exploring Type and Interface Usage in TypeScript

TypeScript has gained immense popularity by bridging the gap between dynamic JavaScript and static typing. Two of its fundamental features, “Type” and “Interface,” play pivotal roles in…

This Post Has One Comment

  1. Option 1 delay is okay but keep in mind that it would be a blocking operation. It’s ideal for use case where you’re waiting for a page to load it’s initial content, but after that it can be a huge cause of timing bugs.

Leave a Reply

%d bloggers like this: