Closed younes-zeboudj closed 1 week ago
workaround, didn't test it thoroughly yet
async function evaluateInPage(page, content) {
while (true) {
let retry;
let r= await page
.evaluate(content)
.catch(
(e) => (retry = e?.toString().includes("acquireContextId failed"))
);
if (retry) {
await new Promise((R) => setTimeout(R, 300));
} else {
return r;
}
}
}
Do you have any specific website where it happens the most? Could it be something because of many iframes on the page?
Also, try to set process.env.REBROWSER_PATCHES_DEBUG=1
, it will give you some debug messages right before this error happens.
Btw, I just added another place for logging this error, please use the latest patch and process.env.REBROWSER_PATCHES_DEBUG=1
.
Thank you for providing the patches! However, I haven't been able to use them successfully so far. For instance, I encounter the error 'acquireContextId failed' already with this code snippet. This is just an example to show that fetch does not work inside evaluate.
const browser = await puppeteer.launch({
headless: false,
executablePath: config.googleChromePath,
});
const context = await browser.createBrowserContext();
const page = await context.newPage();
const url = "https://httpbin.org/ip";
await page.goto(url);
const ip = await page.evaluate(async (url) => {
const response = await fetch(url);
const data = await response.json();
return data.origin;
}, url);
console.log("IP:", ip);
Moreover, I get the following issue occasionally:
Exception has occurred: ProtocolError: Protocol error (Runtime.evaluate): Cannot find context with specified id
at <instance_members_initializer> (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\common\CallbackRegistry.js:93:14)
at new Callback (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\common\CallbackRegistry.js:97:16)
at CallbackRegistry.create (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\common\CallbackRegistry.js:22:26)
at Connection._rawSend (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\Connection.js:89:26)
at CdpCDPSession.send (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\CDPSession.js:66:33)
at #evaluate (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\ExecutionContext.js:410:18)
at ExecutionContext.evaluate (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\ExecutionContext.js:280:36)
at IsolatedWorld.evaluate (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\cdp\IsolatedWorld.js:99:30)
at CdpFrame.evaluate (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\api\Frame.js:339:43)
at CdpFrame.<anonymous> (C:\Users\DELL\Desktop\newtls\job\node_modules\puppeteer-core\lib\cjs\puppeteer\util\decorators.js:98:27)
I couldn't find a solution yet, even retrying the evaluation many times. It seems like the library is causing puppeteer to lose access to contexts's ids in some situations.
📣 I just released a new version of rebrowser-patches with a completely new fix for Runtime.Enable leak. This fix doesn't lose access to main context and also works in workers 🎉 It's a complete drop-in replacement, so you don't need to change any of your existing code. Please try your code with new version.
Sometimes,
page.evaluate(...)
throws the following error:This happens quite often but I can't figure out what triggers it. This may be due to the fast loading of a page.