question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

page.screenshot hangs if an alert() on page ready

See original GitHub issue

I’ve run into an issue with page.screenshot() that occurs whenever an alert() dialog on the page happens after page ready. The page.screenshot() hangs forever and if you end-task the node process, you get

 UnhandledPromiseRejectionWarning: Error: Protocol error (Page.captureScreenshot): Target closed.
    at Promise (/var/www/html/puppeteer-bug/node_modules/puppeteer/lib/Connection.js:200:56)
    at new Promise (<anonymous>)
    at CDPSession.send (/var/www/html/puppeteer-bug/node_modules/puppeteer/lib/Connection.js:199:12)
    at Page._screenshotTask (/var/www/html/puppeteer-bug/node_modules/puppeteer/lib/Page.js:738:39)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

This is running puppeteer 1.3.0 under Ubuntu with Node 5.6.0 and the Chromium version that ships with Puppeteer. Also replicated using full Chrome 65.

Basic Node app:

'use strict';
const puppeteer = require('puppeteer');

async function go(){
    const browser = await puppeteer.launch();
    console.log('opened browser');
    const page = await browser.newPage();
    console.log('created page');
    await page.goto('http://testing/puppeteer/', { timeout: 30000 });
    console.log('loaded page');
    await page.screenshot({
        path: './test.jpg'
    });
    console.log('took screenshot');
    await browser.close();
    console.log('closed browser');
}

go();

The html it’s loading is:

<div style="position: absolute; background-color: #99dd99; top: 10px; left: 40px; width: 300px; height: 250px; padding: 5px 15px; border: 1px solid #444;">Stuff</div>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script>
	$(document).ready(function(){
		alert('test');
	});
</script>

If you comment out the alert, everything works. With the alert in place, you get a console log for ‘loaded page’ and then everything hangs indefinitely.

A workaround is to use Promise.race:

    await Promise.race([
        page.screenshot({
            path: '/test.jpg'
        }),
        new Promise((resolve, reject) => setTimeout(function(){ reject('Screenshot timeout');}, 3000))
    ]);

The screenshot still doesn’t complete, but at least it doesn’t hang the app.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
hoperyycommented, Apr 28, 2021

my solution:

headlessPage.on('dialog', async (dialog) => {
     // "screenshot / evaluate" will go on by closing dialog
     await dialog.dismiss()
 })
3reactions
accampercommented, May 29, 2020

This bug just caused me to waste an entire day. For me, it was page.click and page.evaluate that was hanging for the same underlying reason. Puppeteer should at least reject immediately in all functions if there is an active dialog

Read more comments on GitHub >

github_iconTop Results From Across the Web

page.screenshot hangs if an alert() on page ready · Issue #2481
I've run into an issue with page.screenshot() that occurs whenever an alert() dialog on the page happens after page ready.
Read more >
How to freeze browser window intentionally (like alert, confirm ...
Just place a window sized div above the page with fixed positioning. That will prevent the user from being able to interact with...
Read more >
Full Page Screenshot function not working. - Apple Community
I took a screenshot from safari to take a full page but it does not give me the option to a full page....
Read more >
Troubleshoot Chrome crashes - Google Support
Crashing issues with Chrome or a ChromeOS device can be caused a number of things. Here are some potential issues you might encounter:...
Read more >
Resolve blocking problem caused by lock escalation - SQL ...
This article describes how to determine whether lock escalation is causing blocking and how to resolve the problem.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found