krzyhub
krzyhub

Reputation: 6540

Puppeteer: waitForFunction sometimes never resolves

I have simple Puppeteer script:

await page.goto(MY_URL, {waitUntil: 'load'});
const html = await page.evaluate(() => document.body.innerHTML);

Then I check if html contains some key strings and this part always pass (I mentioned in case if this could anyhow influence further process). Abd after that, I wait for a function to be included in window object.

await page.waitForFunction(() => 'myFunction' in window);

This function is written at the bottom of the script attached in <head> by <script> tag of the page.
Mostly, waitForFunction resolves as it should, but sometimes it doesn't.
And when I pass {timeout: 0} it gonna wait forever and never resolves. It also seems that this happen only in headless mode.

What could be the cause of such a behavior? How to overcome or debug such issue?

Upvotes: 0

Views: 1828

Answers (1)

krzyhub
krzyhub

Reputation: 6540

Seems that JavaScript files sometimes weren't loaded. The solution for me was:

await page.goto(MY_URL, {waitUntil: 'networkidle2'});

Upvotes: 1

Related Questions