How do I use the 'waitFor' function in Puppeteer?

waitFor is a function provided by the Puppeteer library in Node.js to delay the execution of the next line of code until a particular condition is met. It is especially useful when dealing with web scraping or automated browser testing, because it allows you to wait for webpages to load, for elements to appear, or even for a certain amount of time to pass.

There are different versions of waitFor function in Puppeteer:

  • page.waitFor(selector): Waits for the selector to appear in the page.
  • page.waitForXPath(xpath): Waits for the XPath selection to appear in the page.
  • page.waitForFunction(pageFunction[, options[, ...args]]): Waits for the pageFunction to be true.
  • page.waitForTimeout(timeout): Waits for the specified amount of time (in milliseconds).

Here are some examples of how you can use waitFor in Puppeteer:

1. page.waitFor(selector)

This waits for a specific DOM element to appear on the webpage. The selector can be any string that identifies an element on the webpage, like a CSS or XPath selector.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.waitFor('div.container');
  // ... do something after the div.container has loaded ...
  await browser.close();
})();

2. page.waitForXPath(xpath)

This waits for a specific element identified by an XPath expression:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.waitForXPath('//p[@class="description"]');
  // ... do something after the paragraph with class 'description' has loaded ...
  await browser.close();
})();

3. page.waitForFunction(pageFunction[, options[, ...args]])

This waits for a particular JavaScript function to return true.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.waitForFunction(
    'document.querySelector("body").innerText.includes("Success!")'
  );
  // ... do something after the 'Success!' text is found in the body ...
  await browser.close();
})();

4. page.waitForTimeout(timeout)

This waits for a specific amount of time in milliseconds.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.waitForTimeout(3000); // wait for 3 seconds
  // ... do something after 3 seconds ...
  await browser.close();
})();

Remember to always handle potential exceptions with try/catch blocks. For instance, if the waitFor function times out (by default after 30 seconds), it will throw an error.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon