puppeteer-real-browser: Can't get window instance after page load

Hey!

I’m trying to get the window instance after page page.waitForNavigation but the window is undefined.

PAGE_PUPPETEER_OPTS = {
  networkIdle2Timeout: 5000,
  waitUntil: 'networkidle0',
  timeout: 3000000,
};
await page.waitForNavigation({ waitUntil: 'domcontentloaded' });
// await page.waitForFunction(() => window.__NUXT__ !== undefined);
// await page.waitForSelector('#__nuxt');
const windowObject = await page.evaluate(() => {
  return window.__NUXT__;
});

console.log(windowObject);

result is undefined or catching timeout

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 20 (3 by maintainers)

Most upvoted comments

@nwebson It’s working. Many Thanks)

@jarmo-fi I’m glad it worked! 🎉

@pwnz22 just use the code from the article, but instead of

await page.evaluate(() => window.evaluateMain(() => document.getElementsByClassName('div'))

use

await page.evaluate(() => window.evaluateMain(() => window.__NUXT__)

@nwebson This works nicely. Thanks!

@zfcsoftware So no need for me REBROWSER_PATCHES_RUNTIME_FIX_MODE=enableDisable

Are Events safer than somewhat simpler BroadcastChannel for bot detection?

I would suggest using REBROWSER_PATCHES_RUNTIME_FIX_MODE=alwaysIsolated as it’s more secure and stable.

📣 I just made a post how to access main world objects from isolated context, please read it here: How to Access Main Context Objects from Isolated Context in Puppeteer & Playwright

ℹ️ TLDR: use page.evaluateOnNewDocument to listen for window messages and eval code, use page.evaluate to send window message with your payload and get response from the main world. It worked just fine for several clients of mine, please let me know (discord or here) if it doesn’t work for you.

@zfcsoftware for me the suggested solution does not work as I need to be able to call a function (which I have injected earlier) in the page.

So for function calling there is no way currently in puppeteer-real-browser? As said, this works fine with std puppeteer + rebrowser-puppeteer with environment variable REBROWSER_PATCHES_RUNTIME_FIX_MODE=enableDisable. Maybe something similar could be implemented to puppeteer-real-browser too? I’d prefer to use puppeteer-real-browser as it can access cloudflare sites while rebrowser cannot.

Also I tried to use puppeteer-real-browser side-by-side with rebrowser-puppeteer for my different usecases, but looks like puppeteer-real-browser somehow takes over and the evaluate does not work even when puppeteer is later started by rebrowser

@nwebson my code is way too complicated (and private) to be copied here. But what exact code part you are looking for? In short I launch rebrowser-puppeteer normally and all the evaluate and evaluateOnNewDocument functions work the same as in normal puppeteer. But only if REBROWSER_PATCHES_RUNTIME_FIX_MODE=enableDisable and set before app is launched

https://github.com/zfcsoftware/puppeteer-real-browser/tree/access-window https://github.com/zfcsoftware/puppeteer-real-browser?tab=readme-ov-file#i-cannot-access-functions-in-window-object-what-should-i-do Hi, I added a branch for this. You can access it using the example there. If I find a better method in the future, I will add it.

@zfcsoftware Sorry, but i dont understand this use case. What javascript i must add to access it? In my page i also have javascript that is nuxt in script tag. I have access to it in Devtools like so: image but not when using puppeteer-real-browser

I added a new test, please check it. It is possible to access the whole window object with the related method.

https://github.com/zfcsoftware/puppeteer-real-browser/blob/access-window/success2.js

https://github.com/user-attachments/assets/b31df979-d783-4a33-a4f8-331f0345db41

Please follow https://github.com/rebrowser/rebrowser-patches/issues/13 - it’s exactly the same issue. I’m gonna post a solution for this issue probably this week.