Skip to content

wait for dom settle with CDP network events #768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 24, 2025

Conversation

seanmcguire12
Copy link
Member

@seanmcguire12 seanmcguire12 commented May 19, 2025

why

  • the original browser side mutation observer implementation would cause this error: page.evaluate: Execution context was destroyed, most likely because of a navigation
    • the mutation observer had a timeout of 2 seconds, and if a navigation occurred during these two seconds, the page.evaluate would throw an error
  • we can avoid this by monitoring network events on the node side instead of relying on browser side scripts

what changed

  • removed browser side mutation observer logic in favour of listening to network events with CDP

test plan

  • evals

Copy link

changeset-bot bot commented May 19, 2025

🦋 Changeset detected

Latest commit: 32b5df1

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

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

@seanmcguire12 seanmcguire12 added act These changes pertain to the act function extract These changes pertain to the extract function observe These changes pertain to the observe function targeted-extract These changes pertain to targeted extract labels May 19, 2025
@seanmcguire12 seanmcguire12 marked this pull request as ready for review May 23, 2025 23:58
@seanmcguire12 seanmcguire12 changed the title [WIP] wait for dom settle with CDP network events wait for dom settle with CDP network events May 23, 2025
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

This PR replaces browser-side mutation observers with CDP network event monitoring for DOM settling detection, addressing execution context errors during navigation.

  • Implements new _waitForSettledDom in lib/StagehandPage.ts using CDP to track network requests and determine DOM readiness
  • Adds intelligent handling of stalled iframe document requests with a 2-second timeout
  • Introduces a 500ms "quiet window" requirement after network activity stops before considering DOM settled
  • Removes waitForDomSettle from browser-side code (global.d.ts, utils.ts, process.ts)
  • Adds devtools-protocol dependency for CDP network event monitoring

5 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines +467 to +468
const hasDoc = !!(await this.page.title().catch(() => false));
if (!hasDoc) await this.page.waitForLoadState("domcontentloaded");
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Check if page.title() throws for reasons other than missing document - may need more specific error handling

* If no new request appears before the timer fires, the promise
* resolves → **DOM is considered settled**.
* 7. A global guard (`timeoutMs` or `stagehand.domSettleTimeoutMs`,
* default ≈ 30 s) ensures we always resolve; if it fires we log how many
Copy link
Collaborator

Choose a reason for hiding this comment

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

note: lower the default timeout

@seanmcguire12 seanmcguire12 merged commit 58b06eb into main May 24, 2025
13 of 14 checks passed
@github-actions github-actions bot mentioned this pull request May 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
act These changes pertain to the act function extract These changes pertain to the extract function observe These changes pertain to the observe function targeted-extract These changes pertain to targeted extract
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants