Continuous Test Orchestration And Execution Platform Online

Perform automated and live-interactive testing on 3000+ real desktop and mobile devices online.

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.

LambdaTest

Test your websites, web-apps, or mobile apps seamlessly with LambdaTest.

Start Free Testing
LambdaTest

Earn resume-worthy Selenium certifications that help you land a top job.

Learn More
LambdaTest

Test your web or mobile apps

Test orchestration and execution cloud of 3000+ browsers and OS

Support

24/7 support

Security

Enterprise grade security

Cloud

Fastest test execution cloud