Skip to content

Commit

Permalink
improved find() and find_all() to recurse through iframes
Browse files Browse the repository at this point in the history
  • Loading branch information
ultrafunkamsterdam committed Feb 21, 2024
1 parent 54560b4 commit a0c269e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
1 change: 1 addition & 0 deletions nodriver/core/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ async def listener_loop(self):
event = cdp.util.parse_json_event(message)
# self.history.append(event)
event_tx = EventTransaction(event)
# getattr(globals(), event.__class__.__module__)
if not self.connection.mapper:
self.connection.__count__ = itertools.count(0)
event_tx.id = next(self.connection.__count__)
Expand Down
44 changes: 29 additions & 15 deletions nodriver/core/tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Tab(Connection):
find and returns all elements by css selector match.
when no match is found, it will retry for <timeout> seconds (default: 10), so
this is also suitable to use as wait condition.
await :py:obj:`Tab`
---------------------------
Expand Down Expand Up @@ -461,7 +461,9 @@ async def find_elements_by_text(
doc = await self.send(cdp.dom.get_document(-1, True))
search_id, nresult = await self.send(cdp.dom.perform_search(text, True))
if nresult:
node_ids = await self.send(cdp.dom.get_search_results(search_id, 0, nresult))
node_ids = await self.send(
cdp.dom.get_search_results(search_id, 0, nresult)
)
else:
node_ids = []

Expand Down Expand Up @@ -501,17 +503,23 @@ async def find_elements_by_text(
# let's also search through the iframes
iframes = util.filter_recurse_all(doc, lambda node: node.node_name == "IFRAME")
if iframes:
iframes_elems = [element.create(iframe, self, iframe.content_document) for iframe in iframes]
iframes_elems = [
element.create(iframe, self, iframe.content_document)
for iframe in iframes
]
for iframe_elem in iframes_elems:
iframe_text_nodes = util.filter_recurse_all(
iframe_elem.content_document,
lambda node: node.node_type == 3 # noqa
and text.lower() in node.node_value.lower(),
)
if iframe_text_nodes:
iframe_text_elems = [element.create(text_node, self, iframe_elem.tree) for text_node in
iframe_text_nodes]
results.extend(text_node.parent for text_node in iframe_text_elems)
if iframe_elem.content_document:
iframe_text_nodes = util.filter_recurse_all(
iframe_elem,
lambda node: node.node_type == 3 # noqa
and text.lower() in node.node_value.lower(),
)
if iframe_text_nodes:
iframe_text_elems = [
element.create(text_node, self, iframe_elem.tree)
for text_node in iframe_text_nodes
]
results.extend(text_node.parent for text_node in iframe_text_elems)
await self.send(cdp.dom.disable())
return results or []

Expand Down Expand Up @@ -574,15 +582,21 @@ async def find_element_by_text(
# let's also search through the iframes
iframes = util.filter_recurse_all(doc, lambda node: node.node_name == "IFRAME")
if iframes:
iframes_elems = [element.create(iframe, self, iframe.content_document) for iframe in iframes]
iframes_elems = [
element.create(iframe, self, iframe.content_document)
for iframe in iframes
]
for iframe_elem in iframes_elems:
iframe_text_nodes = util.filter_recurse_all(
iframe_elem.content_document,
iframe_elem,
lambda node: node.node_type == 3 # noqa
and text.lower() in node.node_value.lower(),
)
if iframe_text_nodes:
iframe_text_elems = [element.create(text_node, self, iframe_elem.tree) for text_node in iframe_text_nodes]
iframe_text_elems = [
element.create(text_node, self, iframe_elem.tree)
for text_node in iframe_text_nodes
]
results.extend(text_node.parent for text_node in iframe_text_elems)
try:
if not results:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "nodriver" # Required
version = "0.23rc0" # Required
version = "0.24rc1" # Required
description = """
* Official successor of Undetected Chromedriver
* Can be made to work for for all chromium based browsers.
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.23rc0
0.24rc1

0 comments on commit a0c269e

Please sign in to comment.