I have a seemingly simple problem with Puppeteer: when I try to take a screenshot, like so ...
console.log(1)
await page.screenshot({ path: 'screenshot.png' });
console.log(2)
Puppeteer hangs. It doesn't error out or anything, it just waits forever (logging "1" but not "2"), until the whole test times out.
I'm not sure how to resolve this, as screenshot
doesn't even have a timeout
option I can use to force it to timeout faster (as many other Puppeteer functions do). Can anyone suggest:
A) any conditions that would cause this (eg. could I somehow be on a page that failed to load, or something like that)?
B) how I can get screenshot
to take the screenshot, or at least throw an error if it can't?
I finally did what I should have done from the start, and retried doing what my test was doing manually. When I did, I noticed that just before the screenshot was taken, my site was doing a plain old window.alert
.
As soon as I commented that alert
out, the screenshot worked successfully!
So, for future reference: if your page.screenshot
is timing out, check for any alert
s (or, presumably, confirm
s or prompt
s), as apparently Puppeteer can't handle taking a screenshot of one of the web's oldest UI features.
P.S. I filed https://github.com/puppeteer/puppeteer/issues/9801, so hopefully it will get fixed and I can delete this question/answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With