A collection of Element instances with `list`-like operations. Supports iteration, indexing, slicing, and finding descendants. For bulk operations, iterate over the collection explicitly or use the `update_all` method. ```python # Get a collection of elements. items =
| 1034 | |
| 1035 | |
| 1036 | class ElementCollection: |
| 1037 | """ |
| 1038 | A collection of Element instances with `list`-like operations. |
| 1039 | |
| 1040 | Supports iteration, indexing, slicing, and finding descendants. |
| 1041 | For bulk operations, iterate over the collection explicitly or use |
| 1042 | the `update_all` method. |
| 1043 | |
| 1044 | ```python |
| 1045 | # Get a collection of elements. |
| 1046 | items = web.page.find(".item") |
| 1047 | |
| 1048 | # Access by index. |
| 1049 | first = items[0] |
| 1050 | last = items[-1] |
| 1051 | |
| 1052 | # Slice the collection. |
| 1053 | subset = items[1:3] |
| 1054 | |
| 1055 | # Look up a specific element by id (returns None if not found). |
| 1056 | specific = items["item-id"] |
| 1057 | |
| 1058 | # Iterate over elements. |
| 1059 | for item in items: |
| 1060 | item.innerHTML = "Updated" |
| 1061 | item.classes.add("processed") |
| 1062 | |
| 1063 | # Bulk update all contained elements. |
| 1064 | items.update_all(innerHTML="Hello", className="updated") |
| 1065 | |
| 1066 | # Find matches within the collection. |
| 1067 | buttons = items.find("button") |
| 1068 | |
| 1069 | # Get the count. |
| 1070 | count = len(items) |
| 1071 | ``` |
| 1072 | """ |
| 1073 | |
| 1074 | @classmethod |
| 1075 | def wrap_dom_elements(cls, dom_elements): |
| 1076 | """ |
| 1077 | Wrap an iterable of DOM elements in an `ElementCollection`. |
| 1078 | """ |
| 1079 | return cls( |
| 1080 | [Element.wrap_dom_element(dom_element) for dom_element in dom_elements] |
| 1081 | ) |
| 1082 | |
| 1083 | def __init__(self, elements): |
| 1084 | self._elements = elements |
| 1085 | |
| 1086 | def __eq__(self, obj): |
| 1087 | """ |
| 1088 | Check equality by comparing elements. |
| 1089 | """ |
| 1090 | return isinstance(obj, ElementCollection) and obj._elements == self._elements |
| 1091 | |
| 1092 | def __getitem__(self, key): |
| 1093 | """ |
no outgoing calls
no test coverage detected