Search code examples
javascriptnode.jstry-catchpuppeteerapify

Try/catch handler not behaving consistantly


I have the following error handler in a Puppeteer script. When ran from node on my computer, it runs fine. However, when ran from an Apify Puppeteer-Scraper Actor, it works only if I uncomment console.log(e). If not, it will always set notfound as true, regardless of the existence of a foobar class element.

let notfound = false;
try {
    await page.waitForSelector('.foobar', { timeout: 10 });
} catch(e) {
    // console.log(e);
    console.log('Foobar not found.');
    notfound = true;
}

In other languages I would suspect bad memory handling, but I'm not sure it can be the case here ? What could explain this behaviour ?


Solution

  • It wasn't in the original question (my bad), but waitForSelector() had a custom timeout :

        await page.waitForSelector('.foobar', { timeout: 10});
    

    Now this timeout is set in milliseconds, not in seconds.

    Evidently 10 ms is not enough for the foobar class object to be rendered by Puppeteer. Increasing this value solved the issue.