Skip to content

fix: wait for the locator world when a deep XPath crosses an iframe - #2342

Merged
seanmcguire12 merged 2 commits into
browserbase:evals/trippyogi-fix-iframe-main-world-loadingfrom
trippyogi:fix/iframe-main-world-loading-2324
Sep 17, 2026
Merged

seanmcguire12 merged 2 commits into
browserbase:evals/trippyogi-fix-iframe-main-world-loadingfrom
trippyogi:fix/iframe-main-world-loading-2324

Conversation

@trippyogi

@trippyogi trippyogi commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2324.

Rebuilt against current v4 main. The original implementation in this PR targeted packages/core/lib/v3/understudy/, which no longer exists, and waited on the main world. v4 fails on the Stagehand extension/locator world instead, so this branch was rewritten rather than rebased.

Why

Clicking through a still-loading iframe fails deterministically on v4:

Stagehand extension world not ready for frame <id>; checked contexts: none

The same click succeeds immediately once the child document commits. The reproduction in #2324 sharpens the original report in two ways: the trigger is frame descent rather than the target element, and it is not cross-origin specific — a same-site, in-process delayed iframe fails identically.

That surfaces two distinct defects:

Case Selector Expected Before
A. Selector ends at the iframe /html/body/iframe[1] Stay in the parent document and target the <iframe> element resolveDeepXPathTarget flushed the trailing iframe into a FrameLocator, entered the child with xpath=/, and needed a child execution context it never required
B. Selector crosses the iframe /html/body/iframe[1]/html/body/button Descend, then wait for the child's locator world ensureChildFrameReady waited ~1200ms best-effort for the main world and swallowed failures, so evaluation reached the child before the extension world existed

A plain CSS iframe click already worked while loading, which is the semantic target for case A.

What changed

packages/extension/understudy/deepLocator.ts — an iframe step becomes a frame hop only when further selector steps follow it. A trailing iframe[n] stays an element target in the parent frame. Hop planning moved into planDeepXPathTarget() so the semantics are unit-testable without CDP.

packages/extension/understudy/frameLocator.ts — ensureChildFrameReady() now blocks on executionContexts.waitForLocatorWorld() instead of the main world:

  • one bounded 15s deadline, applied only when crossing a frame;
  • session ownership is re-resolved per attempt so OOPIF adoption cannot pin the wait to a stale session, and re-verified after a successful wait;
  • readiness failure throws instead of returning best-effort;
  • the per-child try/catch in resolveFrame() was narrowed to the DOM.getFrameOwner lookup, so a readiness failure surfaces its real message rather than the generic Unable to obtain a content frame for selector: ....

executionContextRegistry.ts is untouched, and the generic 1s waitForLocatorWorld call sites are unchanged — after a gated transition that budget is only a short recovery window for context churn, not the loading timeout.

Test plan

New unit coverage:

  • packages/extension/tests/deep-locator.test.ts — trailing, crossing, nested-trailing, and nested-crossing XPath semantics, plus frame parity with iframe.
  • packages/extension/tests/frame-locator.test.ts — readiness errors propagate out of resolveFrame(), and readiness retries against the new session when OOPIF ownership changes mid-attempt.

New browser regression, packages/sdk-ts/tests/integration/iframeLocatorReadiness.test.ts, registered in the local/frames-shadow group. The child document response is delayed while the <iframe> element is in the parent DOM immediately:

Case Same-process OOPIF (--site-per-process + host-resolver rules)
Trailing iframe XPath clicks without waiting for the child document same
Deep XPath into the child button waits for readiness, then the button's server-side counter increments waits across session adoption, then increments

Case B asserts a real side effect (the child button issues an HTTP request the fixture counts) rather than only elapsed time, so it works for OOPIF without cross-origin DOM inspection.

Results locally: extension unit suite passes, all 4 integration cases pass, oxfmt and oxlint clean, and typecheck reports no errors in the touched files.

Changeset

Extension patch plus the SDKs that embed it; no protocol bump, since there is no wire or API change.

@changeset-bot

changeset-bot Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c4e799f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 11 packages
Name Type
@browserbasehq/stagehand-extension Patch
@browserbasehq/stagehand Patch
@browserbasehq/stagehand-python Patch
@browserbasehq/stagehand-go Patch
@browserbasehq/stagehand-integrations Patch
@browserbasehq/stagehand-integrations-example-eve-facade Patch
@browserbasehq/stagehand-integrations-example-pi-facade Patch
@browserbasehq/stagehand-integrations-example-claude-code-facade Patch
@browserbasehq/stagehand-integrations-example-codex-facade Patch
@browserbasehq/stagehand-integrations-example-mastra-facade Patch
@browserbasehq/stagehand-integrations-example-vercel-ai-facade Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run.
Approving the latest commit mirrors it into an internal PR owned by the approver.
If new commits are pushed later, the internal PR stays open but is marked stale until someone approves the latest external commit and refreshes it.

@github-actions github-actions Bot added external-contributor Tracks PRs mirrored from external contributor forks. external-contributor:awaiting-approval Waiting for a stagehand team member to approve the latest external commit. labels Jul 9, 2026

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 7 files

Confidence score: 5/5

  • Safe to merge after the addressed issues were fixed.
Architecture diagram
sequenceDiagram
    participant Client as "Test / User"
    participant FrameLocator as FrameLocator
    participant Page as Page
    participant CDPSession as "Child CDPSession"
    participant ExecutionContextRegistry as ExecutionContextRegistry

    Note over Client,ExecutionContextRegistry: NEW: Event-driven wait for cross-origin iframe main world

    Client->>FrameLocator: deepLocator().click()
    FrameLocator->>FrameLocator: ensureChildFrameReady(frameId)

    loop Retry on OOPIF session change
        FrameLocator->>Page: getSessionForFrame(frameId)
        Page-->>FrameLocator: session (current owner)

        FrameLocator->>CDPSession: Runtime.enable (best-effort)
        FrameLocator->>CDPSession: Page.setLifecycleEventsEnabled (best-effort)

        FrameLocator->>ExecutionContextRegistry: waitForMainWorld(session, frameId, remaining)
        
        alt Main world already cached
            ExecutionContextRegistry-->>FrameLocator: executionContextId (immediate)
        else Event received before timeout
            par Runtime.executionContextCreated fires
                CDPSession-->>ExecutionContextRegistry: event {auxData.frameId, isDefault, context.id}
            and Page.lifecycleEvent fires
                CDPSession-->>ExecutionContextRegistry: event {frameId, name}
            end
            ExecutionContextRegistry-->>FrameLocator: executionContextId
        else Timeout expires (DEFAULT_MAIN_WORLD_TIMEOUT_MS = 15s via shared constant)
            ExecutionContextRegistry->>ExecutionContextRegistry: final cache check
            alt Found
                ExecutionContextRegistry-->>FrameLocator: executionContextId
            else Still missing
                ExecutionContextRegistry-->>FrameLocator: reject (main world not ready)
                FrameLocator->>FrameLocator: catch() -> wait 50ms, retry loop
            end
        end
    end

    FrameLocator-->>Client: resolved
Loading

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Keep trailing iframe XPaths in the parent frame, and block genuine frame transitions on extension/locator-world readiness with OOPIF session rechecks.
Keep OOPIF readiness tied to the current owning session and preserve the actionable locator-world error after matching a child frame.
@trippyogi
trippyogi force-pushed the fix/iframe-main-world-loading-2324 branch from a56940f to c4e799f Compare August 15, 2026 20:59
@trippyogi trippyogi changed the title fix: wait for main world in cross-origin iframes instead of 1s timeout fix: wait for the locator world when a deep XPath crosses an iframe Aug 15, 2026
@seanmcguire12
seanmcguire12 changed the base branch from main to evals/trippyogi-fix-iframe-main-world-loading September 17, 2026 23:02
@seanmcguire12
seanmcguire12 merged commit f03fde9 into browserbase:evals/trippyogi-fix-iframe-main-world-loading Sep 17, 2026
3 checks passed
seanmcguire12 pushed a commit that referenced this pull request Sep 21, 2026
…2342)

Fixes #2324.

> Rebuilt against current v4 `main`. The original implementation in this
PR targeted `packages/core/lib/v3/understudy/`, which no longer exists,
and waited on the **main world**. v4 fails on the **Stagehand
extension/locator world** instead, so this branch was rewritten rather
than rebased.

## Why

Clicking through a still-loading iframe fails deterministically on v4:

```
Stagehand extension world not ready for frame <id>; checked contexts: none
```

The same click succeeds immediately once the child document commits. The
reproduction in #2324 sharpens the original report in two ways: the
trigger is **frame descent** rather than the target element, and it is
**not cross-origin specific** — a same-site, in-process delayed iframe
fails identically.

That surfaces two distinct defects:

| Case | Selector | Expected | Before |
| --- | --- | --- | --- |
| A. Selector ends at the iframe | `/html/body/iframe[1]` | Stay in the
parent document and target the `<iframe>` element |
`resolveDeepXPathTarget` flushed the trailing iframe into a
`FrameLocator`, entered the child with `xpath=/`, and needed a child
execution context it never required |
| B. Selector crosses the iframe |
`/html/body/iframe[1]/html/body/button` | Descend, then wait for the
child's locator world | `ensureChildFrameReady` waited ~1200ms
best-effort for the **main** world and swallowed failures, so evaluation
reached the child before the extension world existed |

A plain CSS `iframe` click already worked while loading, which is the
semantic target for case A.

## What changed

**`packages/extension/understudy/deepLocator.ts`** — an iframe step
becomes a frame hop only when further selector steps follow it. A
trailing `iframe[n]` stays an element target in the parent frame. Hop
planning moved into `planDeepXPathTarget()` so the semantics are
unit-testable without CDP.

**`packages/extension/understudy/frameLocator.ts`** —
`ensureChildFrameReady()` now blocks on
`executionContexts.waitForLocatorWorld()` instead of the main world:

- one bounded `15s` deadline, applied only when crossing a frame;
- session ownership is re-resolved per attempt so OOPIF adoption cannot
pin the wait to a stale session, and re-verified after a successful
wait;
- readiness failure throws instead of returning best-effort;
- the per-child `try/catch` in `resolveFrame()` was narrowed to the
`DOM.getFrameOwner` lookup, so a readiness failure surfaces its real
message rather than the generic `Unable to obtain a content frame for
selector: ...`.

`executionContextRegistry.ts` is untouched, and the generic `1s`
`waitForLocatorWorld` call sites are unchanged — after a gated
transition that budget is only a short recovery window for context
churn, not the loading timeout.

## Test plan

New unit coverage:

- `packages/extension/tests/deep-locator.test.ts` — trailing, crossing,
nested-trailing, and nested-crossing XPath semantics, plus `frame`
parity with `iframe`.
- `packages/extension/tests/frame-locator.test.ts` — readiness errors
propagate out of `resolveFrame()`, and readiness retries against the new
session when OOPIF ownership changes mid-attempt.

New browser regression,
`packages/sdk-ts/tests/integration/iframeLocatorReadiness.test.ts`,
registered in the `local/frames-shadow` group. The child document
response is delayed while the `<iframe>` element is in the parent DOM
immediately:

| Case | Same-process | OOPIF (`--site-per-process` + host-resolver
rules) |
| --- | --- | --- |
| Trailing iframe XPath | clicks without waiting for the child document
| same |
| Deep XPath into the child button | waits for readiness, then the
button's server-side counter increments | waits across session adoption,
then increments |

Case B asserts a real side effect (the child button issues an HTTP
request the fixture counts) rather than only elapsed time, so it works
for OOPIF without cross-origin DOM inspection.

Results locally: extension unit suite passes, all 4 integration cases
pass, `oxfmt` and `oxlint` clean, and typecheck reports no errors in the
touched files.

## Changeset

Extension patch plus the SDKs that embed it; no protocol bump, since
there is no wire or API change.
seanmcguire12 pushed a commit that referenced this pull request Sep 23, 2026
…2342)

Fixes #2324.

> Rebuilt against current v4 `main`. The original implementation in this
PR targeted `packages/core/lib/v3/understudy/`, which no longer exists,
and waited on the **main world**. v4 fails on the **Stagehand
extension/locator world** instead, so this branch was rewritten rather
than rebased.

## Why

Clicking through a still-loading iframe fails deterministically on v4:

```
Stagehand extension world not ready for frame <id>; checked contexts: none
```

The same click succeeds immediately once the child document commits. The
reproduction in #2324 sharpens the original report in two ways: the
trigger is **frame descent** rather than the target element, and it is
**not cross-origin specific** — a same-site, in-process delayed iframe
fails identically.

That surfaces two distinct defects:

| Case | Selector | Expected | Before |
| --- | --- | --- | --- |
| A. Selector ends at the iframe | `/html/body/iframe[1]` | Stay in the
parent document and target the `<iframe>` element |
`resolveDeepXPathTarget` flushed the trailing iframe into a
`FrameLocator`, entered the child with `xpath=/`, and needed a child
execution context it never required |
| B. Selector crosses the iframe |
`/html/body/iframe[1]/html/body/button` | Descend, then wait for the
child's locator world | `ensureChildFrameReady` waited ~1200ms
best-effort for the **main** world and swallowed failures, so evaluation
reached the child before the extension world existed |

A plain CSS `iframe` click already worked while loading, which is the
semantic target for case A.

## What changed

**`packages/extension/understudy/deepLocator.ts`** — an iframe step
becomes a frame hop only when further selector steps follow it. A
trailing `iframe[n]` stays an element target in the parent frame. Hop
planning moved into `planDeepXPathTarget()` so the semantics are
unit-testable without CDP.

**`packages/extension/understudy/frameLocator.ts`** —
`ensureChildFrameReady()` now blocks on
`executionContexts.waitForLocatorWorld()` instead of the main world:

- one bounded `15s` deadline, applied only when crossing a frame;
- session ownership is re-resolved per attempt so OOPIF adoption cannot
pin the wait to a stale session, and re-verified after a successful
wait;
- readiness failure throws instead of returning best-effort;
- the per-child `try/catch` in `resolveFrame()` was narrowed to the
`DOM.getFrameOwner` lookup, so a readiness failure surfaces its real
message rather than the generic `Unable to obtain a content frame for
selector: ...`.

`executionContextRegistry.ts` is untouched, and the generic `1s`
`waitForLocatorWorld` call sites are unchanged — after a gated
transition that budget is only a short recovery window for context
churn, not the loading timeout.

## Test plan

New unit coverage:

- `packages/extension/tests/deep-locator.test.ts` — trailing, crossing,
nested-trailing, and nested-crossing XPath semantics, plus `frame`
parity with `iframe`.
- `packages/extension/tests/frame-locator.test.ts` — readiness errors
propagate out of `resolveFrame()`, and readiness retries against the new
session when OOPIF ownership changes mid-attempt.

New browser regression,
`packages/sdk-ts/tests/integration/iframeLocatorReadiness.test.ts`,
registered in the `local/frames-shadow` group. The child document
response is delayed while the `<iframe>` element is in the parent DOM
immediately:

| Case | Same-process | OOPIF (`--site-per-process` + host-resolver
rules) |
| --- | --- | --- |
| Trailing iframe XPath | clicks without waiting for the child document
| same |
| Deep XPath into the child button | waits for readiness, then the
button's server-side counter increments | waits across session adoption,
then increments |

Case B asserts a real side effect (the child button issues an HTTP
request the fixture counts) rather than only elapsed time, so it works
for OOPIF without cross-origin DOM inspection.

Results locally: extension unit suite passes, all 4 integration cases
pass, `oxfmt` and `oxlint` clean, and typecheck reports no errors in the
touched files.

## Changeset

Extension patch plus the SDKs that embed it; no protocol bump, since
there is no wire or API change.
@trippyogi
trippyogi deleted the fix/iframe-main-world-loading-2324 branch October 2, 2026 23:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external-contributor:awaiting-approval Waiting for a stagehand team member to approve the latest external commit. external-contributor Tracks PRs mirrored from external contributor forks.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants