Next-Gen App & Browser
Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

How to handle dynamic or asynchronous content on web pages in Puppeteer?

Puppeteer is a Node library that provides a high-level API to control Chrome or Chromium over the DevTools Protocol. It is a powerful tool for automating web tasks, including working with dynamic or asynchronous content.

You can use the page.waitForSelector() or page to work with dynamic or asynchronous content in Puppeteer.waitForXPath() method to wait for an element to be added to the page, or you can use page.waitForFunction() to wait for a specific condition to be true.

Here's an example of using page.waitForSelector() to wait for an element to be added to the page:


await page.goto('https://example.com');

// Wait for the element to be added to the page
await page.waitForSelector('#some-element');

// Now we can do something with the element
const element = await page.$('#some-element');

You can also use page.waitForFunction() for a specific condition to be true. For example, if you want to wait for a specific element to have a certain class, you can use the following code:


await page.goto('https://example.com');

// Wait for the element to have the class "active"
await page.waitForFunction(() => {
  const element = document.querySelector('#some-element');
  return element.classList.contains('active');
});

// Now we can do something with the element
const element = await page.$('#some-element');

In both cases, Puppeteer will continuously check the condition until it becomes true or until the specified timeout is reached. If the timeout is reached, the method will throw an error.

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...
ShadowLT Logo

Start your journey with LambdaTest

Get 100 minutes of automation test minutes FREE!!

Signup for free