简体   繁体   中英

Async/Await in Puppeteer Wait till Page Loads

Using async/await, how I do handle waiting for a page to load until reading it?

I have a long running PHP cron on a website. To get around timeouts, it breaks the problem up in chunks, redirects the browser to the next step, and finally ends with die('Done'); This means the final html of the page looks something like this.

<html>
    <head></head>
    <body>Done</body>
</html>

So with Puppeteer, I'm trying to hit that long running script and just let it go until I get 'Done' to close out my test.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({args:['--no-sandbox']});
  const page = await browser.newPage();
  await page.goto('https://example.com/Test/', {waitUntil: 'networkidle2'}); //dev
  //await page.screenshot({path: 'example.png'});

  // Eventually if script runs, will get run in PHP die('Done') so select text and quit.
  // await page.waitForNavigation();
  let element = await page.$("body").catch(() => {});
  let text = await page.evaluate(element => element.textContent, element).catch(() => {});

  if (text == 'Done') {
    await browser.close();
  }
})();

Right now on a test site that just does redirects, not data processing, it works and closes. On the page that actually processes data all that get's me is the values of both element and text are undefined.

If you could add an id to an element like <div id="done">Done!</div> then you could try using the page.waitForSelector() method like await page.waitForSelector('#done') .

First modify the PHP slightly because it isn't emitting valid HTML in the first place. Note that these waits do timeout, so disable the timeout. Finally, switch to text.includes for the check because the value of text may have Whitespace around it that makes it evaluate to false.

Poster's final code:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({args:['--no-sandbox']});
  const page = await browser.newPage();
  await page.goto('https://example.com/Test/', {waitUntil: 'networkidle2'});

  // Eventually if script runs, will get run in PHP die('Done') so select text and quit.
  await page.waitForSelector('#done', {timeout: 0});
  let element = await page.$("body").catch(() => {});
  let text = await page.evaluate(element => element.textContent, element).catch(() => {});

  if (text.includes("Done")) {
    await browser.close();
  }
})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM