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.

Protocol error (Runtime.callFunctionOn): Target closed.

See original GitHub issue

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: 5.5.0
  • Platform / OS version: macOS 10.15.7
  • URLs (if applicable): -
  • Node.js version: v14.15.0

What steps will reproduce the problem?

Please include code that reproduces the issue.

  1. Open page
  2. Wait for non-existent selector
  3. Close page
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');

// not await!
page.waitForSelector('.non-existent').then(() => console.log('it should never happened));

await page.close();

What is the expected result?

Closing the page normally without errors.

What happens instead?

Error: Protocol error (Runtime.callFunctionOn): Target closed.
    at /app/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:208:63
    at new Promise (<anonymous>)
    at CDPSession.send (/app/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:207:16)
    at ExecutionContext._evaluateInternal (/app/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:200:50)
    at ExecutionContext.evaluateHandle (/app/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:151:21)
    at WaitTask.rerun (/app/node_modules/puppeteer/lib/cjs/puppeteer/common/DOMWorld.js:528:37)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:9

github_iconTop GitHub Comments

5reactions
mahnunchikcommented, Dec 4, 2020

@marr omitted await is important for the issue. It is simplified version of:

await Promise.race([
  page.waitForSelector('.existent').then(() => console.log('fire!')),
  page.waitForSelector('.non-existent').then(() => console.log('it should never happened')),
]);

await page.close();

When it is required to wait at least one element from set of elements.

3reactions
dpmottcommented, Mar 11, 2021

@marr still has a good point, in that you need know that each of those race operations are complete before closing the page out from under them.

Consider this:

  const operations = [ page.waitForSelector(...), page.waitForSelector(...), ];
  const winner = await promise.race(operations);
  // do something with the winner
  await Promise.all(operations); // wait for the rest of the operations to finish or time out
  await page.close();

It might also be possible to put a catch on each of your operations, so that their failure doesn’t cause an error in your scripting:

  const operations = [ page.waitForSelector(...), page.waitForSelector(...), ];
  const winner = await promise.race(operations.map(op => op.catch(() => {})));
  // do something with the winner, but check to see if it's undefined first
  await page.close();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Protocol error (Runtime.callFunctionOn): Target closed. · Issue ...
I'm running into this error after 22 intervals. Node.js 8 + Puppeteer on Debian. try { await page.evaluate(async () => ( new ...
Read more >
ProtocolError: Protocol error (Runtime.callFunctionOn): Target ...
I want to iterate over key-pairs of data.extractRules and get elements data from the page . This snippet inside forEach loop is causing...
Read more >
Error - Target closed - Checkly
Possible causes. Obvious possible cause: the browser, context or tab is being closed at the wrong time in the script. Not-so-obvious ...
Read more >
puppeteer-chat/Lobby - Gitter
For some reason the page opens but doesn't execute the code within. ... { Error: Protocol error (Runtime.callFunctionOn): Target closed.
Read more >
Protocol error (Runtime.callFunctionOn): Object is not ...
This is from Puppeteer unit tests like 'window.open should use parent tab context' in browsercontext.spec.js. Overall I can see it 16 times in...
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