Skip to content

Clicking inside a cross-origin iframe fails with "main world not ready for frame <id>" when the iframe is still loading #2324

Description

@archana-agrawal

Environment Information

Stagehand:

  • Language/SDK: TypeScript (Node.js 20)
  • Stagehand version: 3.6.0

Issue Description

[ERROR] [stagehand:action] error performing click {"error":{"value":"main world not ready for frame 70CB7189B07A8177D60AB6463B624950"},"xpath":{"value":"xpath=/html[1]/body[1]/form[1]/div[3]/section[2]/div[1]/div[1]/div[1]/div[2]/div[1]/iframe[1]"}}

StagehandClickError: Error Clicking Element with selector: main world not ready for frame 70CB7189B07A8177D60AB6463B624950 Reason: xpath=...iframe[1]
    at clickElement (dist/esm/lib/v3/handlers/handlerUtils/actHandlerUtils.js:268:15)
    at async performUnderstudyMethod (dist/esm/lib/v3/handlers/handlerUtils/actHandlerUtils.js:18:9)
    at async ActHandler.takeDeterministicAction (dist/esm/lib/v3/handlers/actHandler.js:227:21)

When act() targets an element that is (or is inside) a cross-origin iframe — e.g. a Vimeo/YouTube embed — and that iframe's document hasn't finished loading yet, the click always fails after ~1 second with main world not ready for frame <id>.

Cross-origin embeds (video players, payment widgets, etc.) often take a few seconds to load, so agent that interact with them fail intermittently The same click succeeds instantly (~35ms) when retried after the iframe finishes loading.

Steps to Reproduce

Real-world case: navigate to https://adequancanine.com/committed-to-dogs (Vimeo embeds) and run act("click the video iframe") while the player is still loading.

Deterministic repro without an LLM — the iframe's document is delayed to make the timing reliable:

import { Stagehand } from "@browserbasehq/stagehand";
import puppeteer from "puppeteer"; // only used to delay the iframe request

const XPATH = "xpath=/html[1]/body[1]/form[1]/div[3]/section[2]/div[1]/div[1]/div[1]/div[2]/div[1]/iframe[1]";

const stagehand = new Stagehand({ env: "LOCAL", verbose: 0 });
await stagehand.init();
const page = stagehand.context.activePage();

// Delay the iframe's document by 8s so the click reliably runs while it is still loading
const browser = await puppeteer.connect({ browserWSEndpoint: stagehand.connectURL(), defaultViewport: null });
const ppage = (await browser.pages())[0];
await ppage.setRequestInterception(true);
ppage.on("request", (req) => {
  if (req.url().includes("player.vimeo.com")) setTimeout(() => req.continue().catch(() => {}), 8000);
  else req.continue().catch(() => {});
});

await page.goto("https://adequancanine.com/committed-to-dogs", { waitUntil: "domcontentloaded" });
// wait until the iframe ELEMENT exists in the parent DOM (its document is still loading)
while ((await page.mainFrame().locator(XPATH).count()) === 0) await new Promise((r) => setTimeout(r, 300));

await page.deepLocator(XPATH).click();
// => throws: "main world not ready for frame <id>" after ~1s
// retrying the same click after the iframe loads succeeds in ~35ms

Activity

  1. harshmathurx commented on Jul 15, 2026

    @harshmathurx

    @filip-michalsky took a look at this repro and it feels like a real readiness bug rather than a flaky click heuristic. The interesting part is that the target element exists in the parent DOM, but the cross-origin frame main world is not ready yet, so the deterministic action path is racing frame readiness instead of gating on it. My bias would be to treat this like a frame-lifecycle wait problem: when the target resolves to an iframe or an element inside one, wait for the destination frame readiness signal before attempting the click, instead of failing fast with the raw Playwright frame error. If that matches intended behavior, I’d be happy to put up a focused fix with a delayed-frame regression test.

  2. harshmathurx commented on Jul 21, 2026

    @harshmathurx

    cc @shrey150 since you’ve been landing the recent act/browse reliability work here — could you confirm whether this should be treated as a frame-readiness wait in the deterministic action path?

    If yes, please assign this to me and I’ll put up a focused PR with a delayed cross-origin iframe regression. I’d keep the scope narrow: target resolves to an iframe / frame-owned element, wait for the frame execution context to be ready, then click; don’t change the broader click heuristic.

  3. ritikpal1122 commented on Aug 13, 2026

    @ritikpal1122
    Contributor

    Checked this against v4.0.0 (main @ 444dc3b) — still reproduces, just with a different error string.

    v3 threw main world not ready for frame <id> after ~1s. v4 throws Stagehand extension world not ready for frame <id>; checked contexts: none after ~3.1s. Same trigger, and the same "succeeds instantly once the iframe finishes loading".

    Deterministic repro, no LLM: two distinct sites mapped to local servers so site-isolation makes a real OOPIF, and the child document response is delayed 12s while the <iframe> element is in the parent DOM immediately. Identical across runs:

    [LOADING] PASS     7ms  css 'iframe' click                          (stays in main frame)
    [LOADING] FAIL  3072ms  xpath .../iframe[1] click                   (descends)
                            Stagehand extension world not ready for frame A267CAF9...
    [LOADING] FAIL  3058ms  xpath .../iframe[1]/html[1]/...button click (descends)
    [LOADED ] PASS  7-22ms  all of the above
    

    Two things that sharpen the original report:

    1. The trigger is frame descent, not the target element. A main-frame CSS selector clicks that same <iframe> fine while the child is loading; the deep XPath fails. Deep XPaths with iframe[n] steps are what act() emits, which is why it's reachable from act() but invisible to a CSS selector.
    2. It's no longer cross-origin-specific. A same-site, in-process delayed iframe fails identically on v4, so the surface looks wider than the v3 report.

    Path through the code: deepLocator.ts resolveLocatorTarget treats the trailing iframe[1] as a frame hop, so the target frame becomes the still-loading child even when the element being clicked is the iframe → frame.ts:186 evaluateInLocatorWorld → waitForLocatorWorld(…, 1000) → executionContextRegistry.ts:112 finds no candidate contexts because the document hasn't committed → isFallbackEligible returns false for http: → the error rethrows.

    Nothing on that path waits on frame lifecycle, unlike ensureChildFrameReady (frameLocator.ts:193), which gates on Page.lifecycleEvent and treats a timeout as best-effort. The timeouts are hardcoded and LocatorClickOptions only exposes button/clickCount, so there's no user-side knob — retrying in userland is the only workaround today.

    So @harshmathurx's framing looks right, with one amendment: the wait needs to trigger when the selector resolves through a frame, not only when the target is inside one.

    Repro script
    import http from "node:http";
    import { localBrowser, Stagehand } from "@browserbasehq/stagehand";
    
    const CHILD_DELAY_MS = 12000;
    const PARENT_PORT = 5311, CHILD_PORT = 5312;
    const XPATH_IFRAME = "xpath=/html[1]/body[1]/div[1]/iframe[1]";
    const XPATH_INNER = "xpath=/html[1]/body[1]/div[1]/iframe[1]/html[1]/body[1]/div[1]/button[1]";
    const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
    
    const parentHtml = `<!doctype html><html><body style="margin:0">
    <div style="padding:40px"><iframe src="http://child.test/child" width="600" height="360"></iframe></div>
    <button id="outside" style="width:200px;height:60px">outside</button>
    </body></html>`;
    const childHtml = `<!doctype html><html><body style="margin:0">
    <div style="padding:20px"><button id="b" style="width:300px;height:120px">click me</button></div>
    </body></html>`;
    
    const serve = (port, h) => new Promise((res) => {
      const s = http.createServer(h); s.listen(port, "127.0.0.1", () => res(s));
    });
    
    const parentServer = await serve(PARENT_PORT, (_q, res) => {
      res.writeHead(200, { "content-type": "text/html" }); res.end(parentHtml);
    });
    let childServed = 0;
    const childServer = await serve(CHILD_PORT, (_q, res) => {
      setTimeout(() => {
        childServed++;
        res.writeHead(200, { "content-type": "text/html" }); res.end(childHtml);
      }, CHILD_DELAY_MS);
    });
    
    const browser = await localBrowser.launch({
      headless: true,
      args: [
        `--host-resolver-rules=MAP parent.test 127.0.0.1:${PARENT_PORT},MAP child.test 127.0.0.1:${CHILD_PORT}`,
        "--site-per-process",
      ],
    });
    await Stagehand.create({ browser });
    
    const rows = [];
    const run = async (phase, name, fn) => {
      const t0 = Date.now();
      try { await fn(); rows.push([phase, "PASS", Date.now() - t0, name, ""]); }
      catch (e) { rows.push([phase, "FAIL", Date.now() - t0, name, String(e?.message ?? e)]); }
    };
    
    const [page] = await browser.context.pages();
    await page.goto("http://parent.test/", { waitUntil: "domcontentloaded" });
    
    const suite = async (phase) => {
      await run(phase, "css '#outside' click (main frame)", () => page.locator("#outside").click());
      await run(phase, "css 'iframe' click (main frame)", () => page.locator("iframe").click());
      await run(phase, "xpath .../iframe[1] click (descends)", () => page.locator(XPATH_IFRAME).click());
      await run(phase, "xpath .../iframe[1]/...button click (descends)", () => page.locator(XPATH_INNER).click());
    };
    
    await suite("LOADING");
    while (childServed === 0) await sleep(200);
    await sleep(1500);
    await suite("LOADED ");
    
    await browser.close();
    parentServer.close(); childServer.close();
    for (const [phase, status, ms, name, detail] of rows) {
      console.log(`[${phase}] ${status} ${String(ms + "ms").padStart(7)}  ${name}${detail ? `\n   ${detail}` : ""}`);
    }
    process.exit(0);
  4. added 3 commits that reference this issue on Sep 17, 2026
    f03fde9
    171f1c7
    687aae0
  5. added a commit that references this issue on Sep 23, 2026
    f3543d3
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