Skip to content

failed: [rebrowser-patches] acquireContextId failed on chrome extensions pages #59

Closed

Description

@n0m44

Hi!
I tried to use rebrowser-puppeteer on the chrome extension page (Metamask in the example) and when I try to get any element, I get an error:

 Error: Waiting for selector `body` failed: [rebrowser-patches] acquireContextId failed
    at ExecutionContext.acquireContextId (/home/user/Desktop/qwe/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/ExecutionContext.js:470:23)
    at async IsolatedWorld.adoptBackendNode (/home/user/Desktop/qwe/node_modules/puppeteer-core/src/cdp/IsolatedWorld.ts:229:3)
    at async IsolatedWorld.transferHandle (/home/user/Desktop/qwe/node_modules/puppeteer-core/src/cdp/IsolatedWorld.ts:65:1)
    at async Function.waitFor (/home/user/Desktop/qwe/node_modules/puppeteer-core/src/common/QueryHandler.ts:204:14)
    at async CdpFrame.waitForSelector (/home/user/Desktop/qwe/node_modules/puppeteer-core/src/api/Frame.ts:729:13)
    at async CdpPage.waitForSelector (/home/user/Desktop/qwe/node_modules/puppeteer-core/src/api/Page.ts:2942:12)

code snippet:

import puppeteer from "rebrowser-puppeteer";

const run = async () => {
  const browser = await puppeteer.launch({
    headless: false,
    ignoreDefaultArgs: true,
    args: [
      '--window-size=1920,1080',
      '--flag-switches-begin',
      '--flag-switches-end',
      '--origin-trial-disabled-features=WebGPU',
      '--disable-blink-features=AutomationControlled',
      `--user-data-dir=blablaToDelete`,
    ],
  });

  const page = await browser.newPage();

  await page.goto('chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/home.html#onboarding/welcome')

  await page.waitForSelector('body');
}

run();

versions:

    "rebrowser-puppeteer": "^23.6.101",
    "rebrowser-puppeteer-core": "^23.6.101",
    "typescript": "^5.6.3"

In addition to the attention:
Sometimes the chrome extension window may appear while the browser is running, I hope it will also work after the fix

Reproduce steps:

  1. Make first run of snippet
  2. Manually install metamask https://chromewebstore.google.com/detail/metamask/nkbihfbeogaeaoehlefnkodbefgpgknn
  3. Re-run script again

Activity

  1. nwebson commented on Nov 3, 2024

    @nwebson
    Contributor

    Please run it with REBROWSER_PATCHES_DEBUG=1 and show the full log here. That would be very helpful.

  2. n0m44 commented on Nov 3, 2024

    @n0m44
    Author

    @nwebson ty for answer

    log:

    [rebrowser-patches][FrameManager] initialize
    [rebrowser-patches] onFrameNavigated, navigationType = Navigation, id = 0E20ABA982C5E9EF4152E9AC573C5CF2, url = about:blank
    [rebrowser-patches] onFrameNavigated, navigationType = Navigation, id = 0E20ABA982C5E9EF4152E9AC573C5CF2, url = chrome-extension://nkbihfbeogaeaoehlefnkodbefgpgknn/home.html
    [rebrowser-patches] onFrameNavigated, navigationType = Navigation, id = 046C3F9CC0DFEEAB23747745208B41F8, url = about:blank
    [rebrowser-patches][acquireContextId] id = -2, name = util, fixMode = addBinding, tryCount = 1
    [rebrowser-patches][acquireContextId] Page.createIsolatedWorld result: { executionContextId: 2 }
    [rebrowser-patches][acquireContextId] id = -1, name = undefined, fixMode = addBinding, tryCount = 1
    [rebrowser-patches][acquireContextId] binding name = oiajfd6y6obzong34nb
    [rebrowser-patches][acquireContextId] failed, try again (tryCount = 1)
    [rebrowser-patches][acquireContextId] id = -1, name = undefined, fixMode = addBinding, tryCount = 2
    [rebrowser-patches][acquireContextId] binding name = j2q6wrrbl0kdff
    [rebrowser-patches][acquireContextId] failed, try again (tryCount = 2)
    [rebrowser-patches][acquireContextId] id = -1, name = undefined, fixMode = addBinding, tryCount = 3
    [rebrowser-patches][acquireContextId] binding name = an6uzc6f5tt5tr1o0mh
    /home/user/Desktop/qwe/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/ExecutionContext.js:470
                    throw new Error('[rebrowser-patches] acquireContextId failed');
                          ^
    Error: Waiting for selector `body` failed: [rebrowser-patches] acquireContextId failed
        at ExecutionContext.acquireContextId (/home/user/Desktop/qwe/node_modules/puppeteer-core/lib/cjs/puppeteer/cdp/ExecutionContext.js:470:23)
        at async IsolatedWorld.adoptBackendNode (/home/user/Desktop/qwe/node_modules/puppeteer-core/src/cdp/IsolatedWorld.ts:229:3)
        at async IsolatedWorld.transferHandle (/home/user/Desktop/qwe/node_modules/puppeteer-core/src/cdp/IsolatedWorld.ts:65:1)
        at async Function.waitFor (/home/user/Desktop/qwe/node_modules/puppeteer-core/src/common/QueryHandler.ts:204:14)
        at async CdpFrame.waitForSelector (/home/user/Desktop/qwe/node_modules/puppeteer-core/src/api/Frame.ts:729:13)
        at async CdpPage.waitForSelector (/home/user/Desktop/qwe/node_modules/puppeteer-core/src/api/Page.ts:2942:12)
    
  3. fred8617 commented on Jan 26, 2025

    @fred8617

    same issue here, any solution? thanks

  4. fred8617 commented on Jan 29, 2025

    @fred8617

    change to REBROWSER_PATCHES_RUNTIME_FIX_MODE=alwaysIsolated, the meta mask can be loaded, but the page.exposeFunction and page.on("console",..) are all invalid

  5. rebrowsernet commented on Oct 18, 2026

    @rebrowsernet
    Collaborator

    Found the cause. MetaMask's pages use LavaMoat "scuttling", which makes reading the global self throw, and the patch's context handshake called self[...]. We reproduced it with MetaMask 13.52 on 25.14.0. With #132, the patch finds the page's context without running anything in the page (and its fallback uses window, which pages can't redefine). With it, waitForSelector() and evaluate() on MetaMask's home.html work in the default mode, the same as with stock Puppeteer. There's a regression test for it, and it ships in the next release. I'll close this then.

  6. rebrowsernet commented on Oct 18, 2026

    @rebrowsernet
    Collaborator

    Released in v1.1.1: rebrowser-puppeteer-core / rebrowser-puppeteer 25.14.100, rebrowser-playwright-core / rebrowser-playwright 1.64.100, and rebrowser-playwright 1.64.100 on PyPI. (npm doesn't allow re-publishing 25.14.0 / 1.64.1, so this fix release uses .100.) Extension pages such as MetaMask's home.html now work in the default mode. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions