Find an element by a selector. Parameters: element: The tree to search. selector: A function that returns True if the element matches. first: If True, return the first element found. If False, raise an error if multiple ele
(
element: VdomJson,
selector: Selector,
*,
first: bool = False,
)
| 35 | |
| 36 | |
| 37 | def find_element( |
| 38 | element: VdomJson, |
| 39 | selector: Selector, |
| 40 | *, |
| 41 | first: bool = False, |
| 42 | ) -> tuple[VdomJson, ElementInfo]: |
| 43 | """Find an element by a selector. |
| 44 | |
| 45 | Parameters: |
| 46 | element: |
| 47 | The tree to search. |
| 48 | selector: |
| 49 | A function that returns True if the element matches. |
| 50 | first: |
| 51 | If True, return the first element found. If False, raise an error if |
| 52 | multiple elements are found. |
| 53 | |
| 54 | Returns: |
| 55 | Element info, or None if not found. |
| 56 | """ |
| 57 | find_iter = find_elements(element, selector) |
| 58 | found = next(find_iter, None) |
| 59 | if found is None: |
| 60 | raise ValueError("Element not found") |
| 61 | if not first: |
| 62 | try: |
| 63 | next(find_iter) |
| 64 | raise ValueError("Multiple elements found") |
| 65 | except StopIteration: |
| 66 | pass |
| 67 | return found |
| 68 | |
| 69 | |
| 70 | def find_elements( |