Skip to content

Conversation

@Avejack
Copy link
Contributor

@Avejack Avejack commented Nov 29, 2025

Description

This PR fixes a race condition where Tab.query_selector and Tab.query_selector_all would fail if the document node ID became invalid immediately after being fetched.

  • In query_selector_all, the method now catches the specific ProtocolException when the document is stale, re-fetches the document, and retries.
  • In query_selector, the method no longer returns None (masking the error) when the document is stale; instead, it re-fetches and retries.

Both implementations piggyback on the existing retry logic (passing the new doc as _node). We explicitly set __last=True on the new document node to ensure we retry exactly once and prevent infinite recursion or double-retries.

Related Issue

Closes #234

Pre-merge Checklist

  • I have described my change in the section above.
  • I have ran the ./scripts/format.sh and ./scripts/lint.sh scripts. My code is properly formatted and has no linting errors.
  • I have ran uv run pytest and ensured all tests pass.
  • I have added my change to CHANGELOG.md under the [Unreleased] section.

Handle `ProtocolException: Could not find node with given id` in
`query_selector` and `query_selector_all` when operating on the default
document. This ensures the method re-fetches the document and retries
exactly once instead of raising or incorrectly returning None.

Closes cdpdriver#234
@Avejack Avejack requested a review from a team as a code owner November 29, 2025 10:06
@codecov
Copy link

codecov bot commented Nov 29, 2025

Codecov Report

❌ Patch coverage is 0% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
zendriver/core/tab.py 0.00% 7 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Member

@stephanlensky stephanlensky left a comment

Choose a reason for hiding this comment

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

Thank you for the fix!

@stephanlensky stephanlensky merged commit 5cc7e7d into cdpdriver:main Nov 29, 2025
4 of 5 checks passed
@Avejack Avejack deleted the fix/stale-document-query-selector branch November 29, 2025 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix race condition in query_selector methods on stale document

2 participants