Conversation
…ts context 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. Fixes browserbase#2982
🦋 Changeset detectedLatest commit: 681946f The changes in this PR will be included in the next version bump. This PR includes changesets to release 18 packages
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 |
Contributor
|
This PR is from an external contributor and must be approved by a stagehand team member with write access before CI can run. |
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2982.
Why
page.waitForSelector()is a singleRuntime.evaluate(awaitPromise) pinned to the execution context that was current when it was issued. The typical caller issues it right after thelocator.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 itExecution context was destroyed).Frame.evaluateInLocatorWorldretries onlyCannot find context with specified id, so the wait failed.With a local form target that answers after 100 ms this fails deterministically on Chrome 153; with a fast target it fails about 2 runs in 5. The standalone repro is in #2982.
What changed
packages/extension/understudy/page.ts—waitForSelectorloops: it re-resolves its target frame (the root wrapper is replaced on root swaps), 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.The retry stays scoped to this wait, which has no side effects.
Frame.evaluateis unchanged, since replaying an arbitrary evaluate after it ran can duplicate side effects (#2751).packages/extension/tests/page-wait-for-selector-navigation.test.ts— fake-session unit test: the outgoing world's evaluate rejects the way Chrome does while the new document's contexts arrive; the wait resolves from the new world, carrying the remaining timeout; an exhausted budget and an unrelated error are not retried.Test plan
vitest run packages/extension/tests packages/extension/understudy: 375 passed (the 3extension-buildtests need avite buildI did not run locally)tsc --noEmit -p packages/extension/tsconfig.json,oxfmt,oxlintv3branch (packages/core/lib/v3/understudy/page.ts) passes a real-browser spec against Chrome 153 that fails 3/3 without it; backport PR followsSummary by cubic
page.waitForSelector()now survives the navigation it is waiting through. When the navigation commit rejects the pending evaluate with "Inspected target navigated or closed" (or "Execution context was destroyed"), the wait is re-issued against the new document with the remaining timeout instead of failing. Fixes #2982.Behavior
Frame.evaluateis unchanged, since replaying arbitrary evaluates can duplicate side effects.Testing
Written for commit 681946f. Summary will update on new commits.