Skip to content

Commit 5cc7e7d

Browse files
authored
fix(core): retry query_selector on stale document ID (#235)
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 #234
1 parent 52e8964 commit 5cc7e7d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12+
- Fix race condition in `Tab.query_selector` and `Tab.query_selector_all` on stale document. @Avejack
13+
1214
### Added
1315

1416
### Changed

zendriver/core/tab.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,13 @@ async def query_selector_all(
469469
setattr(_node, "__last", True)
470470
return await self.query_selector_all(selector, _node)
471471
else:
472+
if e.message is not None and "could not find node" in e.message.lower():
473+
# The document node is stale; refetch and retry once
474+
doc = await self.send(cdp.dom.get_document(-1, True))
475+
# Prevent double-retry by marking this node as 'last attempt'
476+
setattr(doc, "__last", True)
477+
return await self.query_selector_all(selector, doc)
478+
472479
await self.disable_dom_agent()
473480
raise
474481
if not node_ids:
@@ -531,7 +538,11 @@ async def query_selector(
531538
and "could not find node" in e.message.lower()
532539
and doc
533540
):
534-
return None
541+
# The document node is stale; refetch and retry once
542+
doc = await self.send(cdp.dom.get_document(-1, True))
543+
# Prevent double-retry by marking this node as 'last attempt'
544+
setattr(doc, "__last", True)
545+
return await self.query_selector(selector, doc)
535546
else:
536547
await self.disable_dom_agent()
537548
raise

0 commit comments

Comments
 (0)