Skip to content

[V3]: re-issue waitForSelector when a navigation destroys its context - #2984

Open
arrufat wants to merge 1 commit into
browserbase:v3from
arrufat:fix/wait-for-selector-across-navigation-v3
Open

arrufat wants to merge 1 commit into
browserbase:v3from
arrufat:fix/wait-for-selector-across-navigation-v3

Conversation

@arrufat

@arrufat arrufat commented Sep 18, 2026

Copy link
Copy Markdown

Backport of #2983 to v3. Fixes #2982 for the 3.x line.

Why

page.waitForSelector() is a single Runtime.evaluate (awaitPromise) pinned to the execution context that was current when it was issued. The typical caller issues it right after the locator.click() that submits a form, so the context is the document about to be navigated away from. When the commit lands after the evaluate was accepted, Chrome rejects it with -32000 Inspected target navigated or closed (V8 words it Execution context was destroyed). Frame.evaluate retries only Cannot find context with specified id, so the wait failed.

What changed

packages/core/lib/v3/understudy/page.tswaitForSelector loops: it re-resolves its target frame, rebuilds the invocation with the time left, and re-issues the wait when the previous evaluate was rejected with one of the navigation-teardown messages. Anything else, and an exhausted budget, still throw the original error. Frame.evaluate is unchanged, since replaying an arbitrary evaluate after it ran can duplicate side effects.

packages/core/tests/integration/wait-for-selector-across-navigation.spec.ts — real-browser spec: a local form target answers after 150 ms, click() then waitForSelector('#query').

Test plan

  • STAGEHAND_BROWSER_TARGET=local pnpm run test:e2e -- .../wait-for-selector-across-navigation.spec.js against Chrome 153: fails 3/3 runs without the change (Rejected to value: [Error: -32000 Inspected target navigated or closed]), passes with it
  • eslint, prettier, tsc -p packages/core/tsconfig.json --noEmit

Summary by cubic

Fixes page.waitForSelector() in the v3 line so it survives a click-triggered navigation instead of failing with Inspected target navigated or closed (or Execution context was destroyed) when the pending evaluate's execution context is torn down. The wait is re-issued against the new document with the remaining timeout; other errors and an exhausted timeout still throw.

Written for commit 0374fac. Summary will update on new commits.

Review in cubic

The wait is a single Runtime.evaluate pinned to the execution context that
was current when it was issued, typically the document a click is about to
navigate away from. When the commit lands after that, Chrome rejects the
pending evaluate with "Inspected target navigated or closed" (V8 words it
"Execution context was destroyed") and the wait failed, since only
"Cannot find context with specified id" was retried.

The wait has no side effects, so Page.waitForSelector now re-resolves its
target frame and re-issues the wait against the new document with the time
left. Other evaluate failures and an exhausted budget still throw.

Backport of browserbase#2983. Fixes browserbase#2982 on v3.
@changeset-bot

changeset-bot Bot commented Sep 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0374fac

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

This PR includes changesets to release 3 packages
Name Type
@browserbasehq/stagehand Patch
@browserbasehq/stagehand-evals Patch
@browserbasehq/stagehand-server-v3 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

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 Sep 18, 2026

This branch has not been deployed

No deployments
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.

1 participant