Open younes-zeboudj opened 6 days ago
Please share your code. It should work fine.
I will try to avoid sharing code because of some sensitive data. If we need at after all I will try to make a reproducible snippet. For now I add the following:
page.evaluate
always throws the same error:ProtocolError: Protocol error (Runtime.evaluate): Promise was collected
disabling the patch makes the code executes flawlessly
Here is the full stack of the error above:
bundle.js:2471 ProtocolError: Protocol error (Runtime.evaluate): Promise was collected at <instance_members_initializer> (c:\Users\DELL\Desktop\newtls\node_modules\puppeteer-core-patch\lib\cjs\puppeteer\common\CallbackRegistry.js:93:14) at new Callback (c:\Users\DELL\Desktop\newtls\node_modules\puppeteer-core-patch\lib\cjs\puppeteer\common\CallbackRegistry.js:97:16) at CallbackRegistry.create (c:\Users\DELL\Desktop\newtls\node_modules\puppeteer-core-patch\lib\cjs\puppeteer\common\CallbackRegistry.js:22:26) at Connection._rawSend (c:\Users\DELL\Desktop\newtls\node_modules\puppeteer-core-patch\lib\cjs\puppeteer\cdp\Connection.js:89:26) at CdpCDPSession.send (c:\Users\DELL\Desktop\newtls\node_modules\puppeteer-core-patch\lib\cjs\puppeteer\cdp\CDPSession.js:66:33) at #evaluate (c:\Users\DELL\Desktop\newtls\node_modules\puppeteer-core-patch\lib\cjs\puppeteer\cdp\ExecutionContext.js:410:18) at ExecutionContext.evaluate (c:\Users\DELL\Desktop\newtls\node_modules\puppeteer-core-patch\lib\cjs\puppeteer\cdp\ExecutionContext.js:280:36) at IsolatedWorld.evaluate (c:\Users\DELL\Desktop\newtls\node_modules\puppeteer-core-patch\lib\cjs\puppeteer\cdp\IsolatedWorld.js:99:30) at CdpFrame.evaluate (c:\Users\DELL\Desktop\newtls\node_modules\puppeteer-core-patch\lib\cjs\puppeteer\api\Frame.js:339:43) at CdpFrame.<anonymous> (C:\Users\DELL\Desktop\newtls\node_modules\puppeteer-core-patch\lib\cjs\puppeteer\util\decorators.js:98:27) {#code: undefined, #originalMessage: 'Promise was collected', name: 'ProtocolError', message: 'Protocol error (Runtime.evaluate): Promise was collected', stack: 'ProtocolError: Protocol error (Runtime.evalua…b\\cjs\\puppeteer\\util\\decorators.js:98:27)'}
The code I am executing is a long script that does only attach some functions to the window
object or the navigator object
The code is now working without changing anything... I will add new comments if the error occurs again
The problem is still there. It seems it has nothing to do with the reported error because disabling the patch makes the code executes correctly while printing the same error
@younes-zeboudj Isn't this related to https://github.com/rebrowser/rebrowser-patches/issues/13?
@younes-zeboudj I don't think I can help without seeing the code... It feels like there are some misuse of async/await + #13 in effect.
I am not sure if it is related to #13, it may be. However, here is a minimal example:
process.env.REBROWSER_PATCHES_RUNTIME_FIX_MODE = "enableDisable";
async function main() {
const puppeteer = require("puppeteer-core");
const browser = await puppeteer.launch({
headless: false,
executablePath:
"C:\\Program Files\\BraveSoftware\\Brave-Browser\\Application\\brave.exe",
});
const page = (await browser.pages())[0];
await page.goto("https://fr.tlscontact.com/", { waitUntil: "load" });
console.log("page loaded");
await page.evaluate(`document.body.appendChild(document.createTextNode('test patch'))`);
console.log((await page.content()).includes("test patch"), "url:", page.url());
}
main();
step by step:
npm init -y
npm i puppteer-core
npx rebrowser-patches@latest patch
console prints:
page loaded
true url: https://fr.tlscontact.com/
In the dev tools' elements inspector you get:
You can see that the string is not appended to the top document. Re-running the test may results in inserting the string in some other iframe. You may need to run the code a couple of times since it may by chance get inserted in the correct place.
Commenting the first line makes things run as expected.
As a workaround, I am using the top window. Something like:
page.evaluate('window.top.func= ()=>{console.log('my func')}
// in the case of a complex code
page.evaluate(`window.top.setTimeout("${myJsCodeThatTargetsTopWindow}")
The downside of this approach is some complexity and risk of having the code injected in a deeper iframe in which case window.top
will not refer to the main frame window.
PS: I tried puppteer's page.mainFrame().evaluate(...)
with no success. Also tried
let mainFrame= page.mainFrame()
while (mainFrame.parentFrame()) mainFrame= mainFrame.parentFrame()
mainFrame.evaluate(...)
But still the same problem is there.
@younes-zeboudj thanks for sharing the code. It's really interesting behavior.. So, if you disable the patch (comment the first line), then this text node will be added not in the iframe, but in the parent DOM? Did I get you right?
So, I tried to run this code right in devtools, and it added the node into the root DOM as it suppopsed to add. I don't see this swagger iframe #ie-view at all on the page. Do I have to log in to be able to see it?
@nwebson thanks for reporting your experimentation. Do you mean you ran the js code directly in the devtools? And did you try to run the program a couple of times? 3 times should be sufficient to get the unexpected behavior, I noticed that it gets inserted in different iframes when retrying, sometimes in the top DOM. You don't need to be logged in in order to get that result.
@younes-zeboudj btw why do you use enableDisable approach instead of alwaysIsolated? Just noticed that.
Even when using 'enableDisable' approach, my code injected using
page.evaluate(...)
has no access to the main frame DOM. I can seeconsole.log
message done by my code but the DOM manipulations are not there. What is the problem?