puppeteer: When would an error "Cannot find context with specified id undefined" happen?
I am getting the following error:
Error: Protocol error (Runtime.callFunctionOn): Cannot find context with specified id undefined
at Session._onMessage (/srv/node_modules/puppeteer-edge/lib/Connection.js:205:25)
at Connection._onMessage (/srv/node_modules/puppeteer-edge/lib/Connection.js:105:19)
at emitOne (events.js:115:13)
at WebSocket.emit (events.js:210:7)
at Receiver._receiver.onmessage (/srv/node_modules/ws/lib/WebSocket.js:143:47)
at Receiver.dataMessage (/srv/node_modules/ws/lib/Receiver.js:389:14)
at Receiver.getData (/srv/node_modules/ws/lib/Receiver.js:330:12)
at Receiver.startLoop (/srv/node_modules/ws/lib/Receiver.js:165:16)
at Receiver.add (/srv/node_modules/ws/lib/Receiver.js:139:10)
at Socket._ultron.on (/srv/node_modules/ws/lib/WebSocket.js:139:22)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at addChunk (_stream_readable.js:266:12)
at readableAddChunk (_stream_readable.js:253:11)
at Socket.Readable.push (_stream_readable.js:211:10)
at TCP.onread (net.js:585:20)
It is a large codebase and it is unclear whats triggering this error.
Any guides?
On that note, there needs to be a better way to throw error. Without knowing the origin of the error in the code it is impossible to trace down these exceptions.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 134
- Comments: 103 (29 by maintainers)
Commits related to this issue
- fix(Frame): postpone evaluations until execution context gets created In Blink, frames don't necesserily have execution context all the time. DevTools Protocol precisely reports this situation, which... — committed to aslushnikov/puppeteer by aslushnikov 7 years ago
- fix(Frame): postpone evaluations until execution context gets created (#1415) In Blink, frames don't necesserily have execution context all the time. DevTools Protocol precisely reports this situati... — committed to puppeteer/puppeteer by aslushnikov 7 years ago
- Updated GitHub login to work with newest Puppeteer by circumventing Puppeteer bug (GoogleChrome/puppeteer#1325). Also added support for Github credentials as environment variables (`GITHUB_USERNAME` a... — committed to Webstrates/Webstrates by kbadk 6 years ago
- tests: increase reload timeouts This is to prevent "Cannot find context with specified id" on Travis. Maybe related: https://github.com/GoogleChrome/puppeteer/issues/1325 — committed to binaryage/dirac by darwin 6 years ago
- fix(page): fix race condition in WaitTask This patch eliminates a common race condition with WaitTask, that happens when predicate function gets resolved right before the execution context gets destr... — committed to aslushnikov/puppeteer by aslushnikov 6 years ago
- fix(page): fix race condition in WaitTask (#2739) This patch eliminates a common race condition with WaitTask, that happens when predicate function gets resolved right before the execution context ... — committed to puppeteer/puppeteer by aslushnikov 6 years ago
Hi, encountered this problem when updating code that was using puppeteer < 1.0 to newest version. I was also able to find source of the problem.
First, my reproduction script
with error:
Please note that it doesn’t happen every time so I added the loop, but possibly 10 runs may be not enough.
End the source of the problem is that line: https://github.com/GoogleChrome/puppeteer/blob/808bf8e5582482a1d849ff22a51e52024810905c/lib/FrameManager.js#L875 You can replace evaluation with
If you look at
rerunfunction as a whole you will see that earlier it is wrapping evaluation in try/catch and handles errors but that evaluation is not protected in any way. Ironically later there is check forCannot find context with specified idbut it is never reached if second evaluation fails./cc @JoelEinbinder @aslushnikov author and reviewer of #1746 that introduced the problematic line
Then why not release
0.14.0? Like, now.There is literally no reason whatsoever not to automate the releases. There is no reason whatsoever not to release a new version every time a PR has been merged into the master. If you merge something into the master, then it is ready for the release.
This needs more attention. It hasn’t even been acknowledged as a bug.
I have ceased using Puppeteer and ended up developing our own abstraction mostly because of this issue (and general project maintainer attitude towards this project).
Really a shame to see that what Google Chrome team are pushing as an “official” abstraction is being treated with such negligence.
As I am no longer using Puppeteer, I am closing this issue and unsubscribing. Project maintainers are welcome to re-open.
@gajus installing from master gives you the cadence you want…
I had the same issue that was happening when I was doing a page.click(‘selector’).
I reverted to puppeteer 0.12.0 and I had a much clearer error message (it was related to the element I wanted to click on that was not available yet). I fixed the issue by waiting for the element then updated again to 0.13.0 and it is now working fine.
FYI I seem to have worked around this temporarily with this:
wait-for-navigation-and-context.js
Usage:
At the risk of spamming, I’m going to post this again, so we can stay on top of the actual issue:
The underlying problem is that there is a race condition between the event that clears execution contexts and the event that adds new execution contexts.
When you navigate to a new page, sometimes headless chrome will add the new contexts, before clearing the old ones. When it goes to clear the contexts, it blows away the recently added context, causing:
Cannot find context with specified id undefined.I think the fix should come from upstream, since I think it’s going to be quite difficult to handle correctly in puppeteer.
Just adding another voice on this one: if you hit this while trying to do a form submission or something using
evaluate()that causes navigation to occur, try not returning anything fromevaluate().This throws the error:
This works correctly:
I’ve installed from master but I’m still getting the same error thrown. Anyone else?
I found a way to get around this issue that works for me. I made a little utility function as follows:
In my code I had the following calls next to each other. And the
getProperty('href')would throw the exception. But once I put thewaitFor(3000)in there things started working again.❗️ I’m getting the same error. I can’t share the entire codebase or provide a full repro since it’s closed source and has lots of moving parts, but here is the code that I’m pretty sure is causing the issue:
The output I get when I run it is:
Notice that the error occurs after
after 123is already logged, and the error never get’s caught by thetry-catch. 😟Another important thing to note is that in my
page.evaluateHandlecall I’m submitting a form. My theory is that maybe since I’m navigating to another page before the evaluation completes that’s causing the issue 🤷♂️I noticed that this issue has been getting a ton of comments and very little replies from contributors, so it would be amazing if one you guys can just quickly explain what’s the status of this bug and let us know if you need any more info you need to help reproduce it. If I would understand the issue better I’d be happy to submit a PR.
@gajus the library is still prerelease. Automating deployment is probably best when things have stabilized all around. Things are still in a good bit of flux, so imo it is better updates be batched in case multiple breaking changes occur at once. Then developers just look over their existing code once instead on multiple times for multiple updates.
Hey folks, are we sure that
runtime.clearExecutionContextsis doing the right thing?In the code it clears out all execution contexts in all frames. The reason I ask is that I’m also running into this in a different library. It doesn’t happen all the time, only occassionally.
Here’s a full feed of the actions (most of these logs correspond to puppeteer actions):
From the looks of it, it seems like the most important bit is here:
Oh! You know I bet it’s a race condition between adding new execution contexts and clearing out previous execution contexts 😀
The order should probably be:
Follow up, do we even need to clear the executions? Or can we just let
runtime.executionContextDestroyeddo it’s thing?The version in the package.json is not relevant. The version information is pulled from NPM prior to making a release.
See this explanation, https://github.com/semantic-release/semantic-release#why-is-the-packagejsons-version-not-updated-in-my-repository.
Whats the current process to publish the documentation?
It would be simply a matter of automating publishing of the documentation after
semantic-releasescript is done.For the same reason we have semantic versioning in the first place.
Someone who is consuming Puppeteer does not want to be using
headbecause that can introduce a breaking change. Equally, locking to a particular commit provides no way of checking if there is a more recent version.When projects are setup to properly use semver, updating project dependencies is simply the matter of running a program such as https://www.npmjs.com/package/npm-check-updates. Imagine if every repository started to use your approach – maintaining project dependencies up to date would be a nightmare.
I am getting this error occasionally, and I have no idea where to catch this exception, so my browser hangs.
For now, I am disabling the load of websocket resources (because I don’t need them) to see if that helps.
What solved it for me was changing:
to
… in one instance at least
I’ve done:
and just dotted
await waitForPageReady();around my code when I think the URL may change.I had the problem where form buttons were being pressed and one of them triggered a page load (submit) but even though the mouse control was on an await, the script continued running and then I tried to screenshot the page but the page hadn’t loaded at that point. Using the above code worked around it.
A heads up for anyone hitting this issue - check through your code to make sure there is not a missing
awaiton an earlier call. This can lead to a race condition and cause the error on a later, seemingly unrelated line.I hit this issue and reading through the comments, I can see a few people that have a similar possible error in their code, so hopefully that helps someone.
I was digging into this the other day because it was causing issues for us pretty consistently for a site. What triggered it for us was a race condition between page navigation and trying to find an element. What it boiled down to was that a navigation event happened between these two lines. The
documentobject was retrieved from one context, the navigation happened, and then thedocumentobject was used to try to find an element but it fails with the error discussed here because the context of thedocumentwas destroyed by the navigation.Adding an explicit wait time before all of this obviously helps, but that’s fairly non-deterministic. Is there an easy way to check if a context has been destroyed without having to actually make a call via devtools protocol? For now, when encountering this error, we just retry what we were trying to do.
@dxinteractive yes, if the
page.evaluatecauses a navigation, than the execution context where the evaluation is happenning will self-destruct, andpage.evaluatecall will throw an error.I you anticipate that your evaluation might trigger a navigation, than I’d recommend to gracefully handle this exception in your script.
@aslushnikov , I am getting Error: Protocol error (Runtime.callFunctionOn): Cannot find context with specified id. when I am selecting in dropdown, but it can able to select still throws error. ssample code is below page.waitForSelector(‘#selector’); page.select(‘#selector’, value);
🎉
I solved this by adding await page.waitForNavigation({waitUntil: ‘load’}); before typing in A couple of updates ocurred which I think made the error start, one being changing puppeteer from 1.2.0 to 1.3.0
As crazy as this might sound, I’ve found a quick workaround for this “Error: Protocol error (Runtime.evaluate): Cannot find context with specified id undefined’” issue.
I was trying to evaluate whatever contents within a page “on load” event handler, but I kept getting this error for one specific website (never happened with hundred of others).
I simply added an
await page.waitFor("//html")line, and it fixed it…If you are open to start using semantic-release, I am happy to raise a PR that integrates semantic-release. Alternatively, you can have a look into any of the projects that I maintain, http://github.com/gajus.
Example:
From https://github.com/gajus/isomorphic-webpack
semantic-releaseas a dev dependency.NPM_TOKENandGH_TOKENsecrets variables to Travis configuration.Might as well not use package manager then, because you are telling users to pull code from the repository.
Based on the info, the change probably happened in a Chromium roll between 0.12.0 and 0.13.0. There were a couple of updates in between those releases.
@aslushnikov @JoelEinbinder any weird changes to the protocol/chrome that would be related to this?
Did that help?
@aslushnikov I will add debugging ASAP and report back
Me too, version 1.1.1 seems to still have this issue for me.
@dallashuggins thank you for sharing the details. In this case, the
Cannot find context with specified id undefinederror seems to be related to thepage crashedfailure. Thepage crashederror is a major problem; let’s debug it first.I’ve created a designated issue for this here: https://github.com/GoogleChrome/puppeteer/issues/1785
@aslushnikov Can this be reopened? The bug still exists and affects the rest of us.
@ggobbe Thanks for your trouble shooting
I also had this error on 0.13.0 It errors on
let text = await page.evaluate(() => document.body.textContent) Protocol error (Runtime.callFunctionOn): Cannot find context with specified id undefined at Session._onMessage (node_modules/puppeteer/lib/Connection.js:205:25)Adding
await page.waitFor(4000)gives PASS.Is there a ‘better practice’ way?
Thanks
this error presented for me when changing passing
timeout: 0topage.launchoptions. Changing it to a high value (300k, though not sure magnitude matters) solved the problem.I am in the same situation since I upgraded to v.0.13.0, this error appear before the page even start navigating…