Equivalent of JavaScript "document.querySelectorAll". This is considered one of the main methods to use in this package. It returns all matching :py:obj:`element.Element` objects. :param selector: css selector. (first time? => https://www.w3schools.com/
(
self,
selector: str,
_node: Optional[Union[cdp.dom.Node, "element.Element"]] = None,
)
| 379 | return await self.get(url=url) |
| 380 | |
| 381 | async def query_selector_all( |
| 382 | self, |
| 383 | selector: str, |
| 384 | _node: Optional[Union[cdp.dom.Node, "element.Element"]] = None, |
| 385 | ): |
| 386 | """ |
| 387 | Equivalent of JavaScript "document.querySelectorAll". |
| 388 | This is considered one of the main methods to use in this package. |
| 389 | It returns all matching :py:obj:`element.Element` objects. |
| 390 | :param selector: css selector. |
| 391 | (first time? => https://www.w3schools.com/cssref/css_selectors.php ) |
| 392 | :type selector: str |
| 393 | :param _node: internal use |
| 394 | """ |
| 395 | if not _node: |
| 396 | doc: cdp.dom.Node = await self.send(cdp.dom.get_document(-1, True)) |
| 397 | else: |
| 398 | doc = _node |
| 399 | if _node.node_name == "IFRAME": |
| 400 | doc = _node.content_document |
| 401 | node_ids = [] |
| 402 | try: |
| 403 | node_ids = await self.send( |
| 404 | cdp.dom.query_selector_all(doc.node_id, selector) |
| 405 | ) |
| 406 | except ProtocolException as e: |
| 407 | if _node is not None: |
| 408 | if "could not find node" in e.message.lower(): |
| 409 | if getattr(_node, "__last", None): |
| 410 | del _node.__last |
| 411 | return [] |
| 412 | # If the supplied node is not found, |
| 413 | # then the DOM has changed since acquiring the element. |
| 414 | # Therefore, we need to update our node, and try again. |
| 415 | await _node.update() |
| 416 | _node.__last = ( |
| 417 | True # Make sure this isn't turned into infinite loop. |
| 418 | ) |
| 419 | return await self.query_selector_all(selector, _node) |
| 420 | else: |
| 421 | await self.send(cdp.dom.disable()) |
| 422 | raise |
| 423 | if not node_ids: |
| 424 | return [] |
| 425 | items = [] |
| 426 | for nid in node_ids: |
| 427 | node = util.filter_recurse(doc, lambda n: n.node_id == nid) |
| 428 | # Pass along the retrieved document tree to improve performance. |
| 429 | if not node: |
| 430 | continue |
| 431 | elem = element.create(node, self, doc) |
| 432 | items.append(elem) |
| 433 | return items |
| 434 | |
| 435 | async def query_selector( |
| 436 | self, |
no test coverage detected