|
15 | 15 | # specific language governing permissions and limitations |
16 | 16 | # under the License. |
17 | 17 |
|
| 18 | +import warnings |
18 | 19 | from typing import Any, Callable, Optional, Union |
19 | 20 |
|
20 | 21 | from selenium.webdriver.common.bidi.common import command_builder |
@@ -110,7 +111,12 @@ def from_json(cls, json: dict) -> "BrowsingContextInfo": |
110 | 111 | children = None |
111 | 112 | raw_children = json.get("children") |
112 | 113 | if raw_children is not None and isinstance(raw_children, list): |
113 | | - children = [BrowsingContextInfo.from_json(child) for child in raw_children if isinstance(child, dict)] |
| 114 | + children = [] |
| 115 | + for child in raw_children: |
| 116 | + if isinstance(child, dict): |
| 117 | + children.append(BrowsingContextInfo.from_json(child)) |
| 118 | + else: |
| 119 | + warnings.warn(f"Unexpected child type in browsing context: {type(child)}") |
114 | 120 |
|
115 | 121 | return cls( |
116 | 122 | context=str(json.get("context", "")), |
@@ -187,12 +193,32 @@ def from_json(cls, json: dict) -> "UserPromptOpenedParams": |
187 | 193 | ------- |
188 | 194 | UserPromptOpenedParams: A new instance of UserPromptOpenedParams. |
189 | 195 | """ |
| 196 | + context = json.get("context") |
| 197 | + if context is None or not isinstance(context, str): |
| 198 | + raise ValueError("context is required and must be a string") |
| 199 | + |
| 200 | + handler = json.get("handler") |
| 201 | + if handler is None or not isinstance(handler, str): |
| 202 | + raise ValueError("handler is required and must be a string") |
| 203 | + |
| 204 | + message = json.get("message") |
| 205 | + if message is None or not isinstance(message, str): |
| 206 | + raise ValueError("message is required and must be a string") |
| 207 | + |
| 208 | + type_value = json.get("type") |
| 209 | + if type_value is None or not isinstance(type_value, str): |
| 210 | + raise ValueError("type is required and must be a string") |
| 211 | + |
| 212 | + default_value = json.get("defaultValue") |
| 213 | + if default_value is not None and not isinstance(default_value, str): |
| 214 | + raise ValueError("defaultValue must be a string if provided") |
| 215 | + |
190 | 216 | return cls( |
191 | | - context=str(json.get("context", "")), |
192 | | - handler=str(json.get("handler", "")), |
193 | | - message=str(json.get("message", "")), |
194 | | - type=str(json.get("type", "")), |
195 | | - default_value=str(json.get("defaultValue", "")), |
| 217 | + context=context, |
| 218 | + handler=handler, |
| 219 | + message=message, |
| 220 | + type=type_value, |
| 221 | + default_value=default_value, |
196 | 222 | ) |
197 | 223 |
|
198 | 224 |
|
@@ -714,13 +740,15 @@ def remove_event_handler(self, event: str, callback_id: int) -> None: |
714 | 740 | event_obj = BrowsingContextEvent(event_name) |
715 | 741 |
|
716 | 742 | self.conn.remove_callback(event_obj, callback_id) |
717 | | - if event_name in self.subscriptions and callback_id in self.subscriptions[event_name]: |
718 | | - self.subscriptions[event_name].remove(callback_id) |
719 | | - if len(self.subscriptions[event_name]) == 0: |
720 | | - params = {"events": [event_name]} |
721 | | - session = Session(self.conn) |
722 | | - self.conn.execute(session.unsubscribe(**params)) |
723 | | - del self.subscriptions[event_name] |
| 743 | + if event_name in self.subscriptions: |
| 744 | + callbacks = self.subscriptions[event_name] |
| 745 | + if callback_id in callbacks: |
| 746 | + callbacks.remove(callback_id) |
| 747 | + if not callbacks: |
| 748 | + params = {"events": [event_name]} |
| 749 | + session = Session(self.conn) |
| 750 | + self.conn.execute(session.unsubscribe(**params)) |
| 751 | + del self.subscriptions[event_name] |
724 | 752 |
|
725 | 753 | def clear_event_handlers(self) -> None: |
726 | 754 | """Clear all event handlers from the browsing context.""" |
|
0 commit comments