Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protocol error (Page.captureScreenshot): Target closed when I try take screenshot #1385

Closed
kauecastro opened this issue Nov 14, 2017 · 10 comments

Comments

@kauecastro
Copy link

kauecastro commented Nov 14, 2017

Running this example code:


async takeScreenshot() {  
        console.log('trying take Screenshot [...]');
        let image = this.page.screenshot({fullPage: true, type: 'jpeg'});
        timeout(image, 30000)
        .then((thing) => console.log("screenshot successfull!"))
        .catch((err) => {
            if(err instanceof TimeoutError) {
                console.log(err);
                image = null;
            }
        })

        console.log('Completed!');
        return image;
    }

I get the following error output:

0|era-moni | Error: Protocol error (Page.captureScreenshot): Target closed.
0|era-moni | at Session._onClosed (/home/ubuntu/project/ra-monitors/era-monitor-browser/node_modules/puppeteer/lib/Connection.js:209:23)
0|era-moni | at Connection._onClose (/home/ubuntu/project/ra-monitors/era-monitor-browser/node_modules/puppeteer/lib/Connection.js:116:15)
0|era-moni | at emitTwo (events.js:106:13)
0|era-moni | at WebSocket.emit (events.js:194:7)
0|era-moni | at WebSocket.emitClose (/home/ubuntu/project/ra-monitors/era-monitor-browser/node_modules/ws/lib/WebSocket.js:213:10)
0|era-moni | at _receiver.cleanup (/home/ubuntu/project/ra-monitors/era-monitor-browser/node_modules/ws/lib/WebSocket.js:195:41)
0|era-moni | at Receiver.cleanup (/home/ubuntu/project/ra-monitors/era-monitor-browser/node_modules/ws/lib/Receiver.js:520:15)
0|era-moni | at WebSocket.finalize (/home/ubuntu/project/ra-monitors/era-monitor-browser/node_modules/ws/lib/WebSocket.js:195:22)
0|era-moni | at emitNone (events.js:91:20)
0|era-moni | at Socket.emit (events.js:188:7)

@joelgriffith
Copy link
Contributor

Error: Protocol error (Page.captureScreenshot): Target closed.
It's possible that the page is large enough to cause Chrome to crash. What version of puppeteer and what operating system?

@tofixx
Copy link

tofixx commented Nov 20, 2017

I get Protocol error (Emulation.setDeviceMetricsOverride): Target closed.after page.setViewport with any properties for calling page.screenshot({'fullPage':true}). Seems a bug.

@trentmwillis
Copy link
Contributor

@kauecastro I believe this line:

let image = this.page.screenshot({fullPage: true, type: 'jpeg'});

Is missing an await:

let image = await this.page.screenshot({fullPage: true, type: 'jpeg'});

So the rest of your code is probably executing synchronously and not actually waiting for the screenshot to complete.

@smelukov
Copy link

We're using docker container like that:

FROM ......

# Install missing shared libs for Chromium
RUN apt-get update && \
    apt-get install -yq gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 \
    libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 \
    libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 \
    libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 \
    ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget

# Project
COPY ./ /app
WORKDIR /app

# Install puppeteer so it's available in the container
RUN npm i --production

# Info
RUN npm -v
RUN node -v

# Run
ENTRYPOINT ["npm", "start"]

And inside this container we're getting an error Protocol error (Page.captureScreenshot): Target closed. when fullPage property is true and trying to take a screenshot from a huge page.

In a local machine, everything is fine.

@seekshiva
Copy link

I can confirm that this only happens on large pages. It also happens only when I run it on my remote server that has low processing power and RAM. It works fine when I run it on personal machine.

I suspect the RAM on my server wasn't enough. If puppeteer can stream the data instead of holding it all in memory, it may solve the issue?

@smelukov
Copy link

smelukov commented Dec 26, 2017

@seekshiva a docker container that I ran on my local machine has no RAM or CPU limit, but outside a container - everything is ok
Even if I set RAM limit to 6GB - it doesn't help

@seekshiva
Copy link

I was previously running the script on the $5 digital ocean instance with 512mb RAM.. I've upgraded to 3GB RAM and the script is able to generate screenshots without any issues.

@aslushnikov
Copy link
Contributor

The issue in the OP seems to be addressed by the comment.

@Saibamen
Copy link

Saibamen commented Oct 4, 2019

@aslushnikov This not solved my issue.
I use var pdfBytes = await page.PdfDataAsync(pdfOptions); and still have this error

@Zamaroht
Copy link

Zamaroht commented Oct 8, 2022

I was having this issue in MacOS. It got fixed after I started saving the screenshot under os.tmpdir() path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants