1515# specific language governing permissions and limitations
1616# under the License.
1717
18- from typing import Optional , Union
18+ from typing import Any , Callable , Optional , Union
1919
2020from selenium .webdriver .common .bidi .common import command_builder
2121
@@ -67,10 +67,10 @@ def from_json(cls, json: dict) -> "NavigationInfo":
6767 NavigationInfo: A new instance of NavigationInfo.
6868 """
6969 return cls (
70- context = json .get ("context" ),
70+ context = str ( json .get ("context" , "" ) ),
7171 navigation = json .get ("navigation" ),
72- timestamp = json .get ("timestamp" ),
73- url = json .get ("url" ),
72+ timestamp = int ( json .get ("timestamp" , 0 ) ),
73+ url = str ( json .get ("url" , "" ) ),
7474 )
7575
7676
@@ -108,12 +108,13 @@ def from_json(cls, json: dict) -> "BrowsingContextInfo":
108108 BrowsingContextInfo: A new instance of BrowsingContextInfo.
109109 """
110110 children = None
111- if json .get ("children" ) is not None :
112- children = [BrowsingContextInfo .from_json (child ) for child in json .get ("children" )]
111+ raw_children = json .get ("children" )
112+ if raw_children is not None and isinstance (raw_children , list ):
113+ children = [BrowsingContextInfo .from_json (child ) for child in raw_children ]
113114
114115 return cls (
115- context = json .get ("context" ),
116- url = json .get ("url" ),
116+ context = str ( json .get ("context" , "" ) ),
117+ url = str ( json .get ("url" , "" ) ),
117118 children = children ,
118119 parent = json .get ("parent" ),
119120 user_context = json .get ("userContext" ),
@@ -149,11 +150,11 @@ def from_json(cls, json: dict) -> "DownloadWillBeginParams":
149150 DownloadWillBeginParams: A new instance of DownloadWillBeginParams.
150151 """
151152 return cls (
152- context = json .get ("context" ),
153+ context = str ( json .get ("context" , "" ) ),
153154 navigation = json .get ("navigation" ),
154- timestamp = json .get ("timestamp" ),
155- url = json .get ("url" ),
156- suggested_filename = json .get ("suggestedFilename" ),
155+ timestamp = int ( json .get ("timestamp" , 0 ) ),
156+ url = str ( json .get ("url" , "" ) ),
157+ suggested_filename = str ( json .get ("suggestedFilename" , "" ) ),
157158 )
158159
159160
@@ -187,11 +188,11 @@ def from_json(cls, json: dict) -> "UserPromptOpenedParams":
187188 UserPromptOpenedParams: A new instance of UserPromptOpenedParams.
188189 """
189190 return cls (
190- context = json .get ("context" ),
191- handler = json .get ("handler" ),
192- message = json .get ("message" ),
193- type = json .get ("type" ),
194- default_value = json .get ("defaultValue" ),
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" , "" ) ),
195196 )
196197
197198
@@ -223,10 +224,10 @@ def from_json(cls, json: dict) -> "UserPromptClosedParams":
223224 UserPromptClosedParams: A new instance of UserPromptClosedParams.
224225 """
225226 return cls (
226- context = json .get ("context" ),
227- accepted = json .get ("accepted" ),
228- type = json .get ("type" ),
229- user_text = json .get ("userText" ),
227+ context = str ( json .get ("context" , "" ) ),
228+ accepted = bool ( json .get ("accepted" , False ) ),
229+ type = str ( json .get ("type" , "" ) ),
230+ user_text = str ( json .get ("userText" , "" ) ),
230231 )
231232
232233
@@ -254,8 +255,8 @@ def from_json(cls, json: dict) -> "HistoryUpdatedParams":
254255 HistoryUpdatedParams: A new instance of HistoryUpdatedParams.
255256 """
256257 return cls (
257- context = json .get ("context" ),
258- url = json .get ("url" ),
258+ context = str ( json .get ("context" , "" ) ),
259+ url = str ( json .get ("url" , "" ) ),
259260 )
260261
261262
@@ -278,7 +279,7 @@ def from_json(cls, json: dict) -> "BrowsingContextEvent":
278279 -------
279280 BrowsingContextEvent: A new instance of BrowsingContextEvent.
280281 """
281- return cls (event_class = json .get ("event_class" ), ** json )
282+ return cls (event_class = str ( json .get ("event_class" , "" ) ), ** json )
282283
283284
284285class BrowsingContext :
@@ -339,7 +340,7 @@ def capture_screenshot(
339340 -------
340341 str: The Base64-encoded screenshot.
341342 """
342- params = {"context" : context , "origin" : origin }
343+ params : dict [ str , Any ] = {"context" : context , "origin" : origin }
343344 if format is not None :
344345 params ["format" ] = format
345346 if clip is not None :
@@ -383,7 +384,7 @@ def create(
383384 -------
384385 str: The browsing context ID of the created navigable.
385386 """
386- params = {"type" : type }
387+ params : dict [ str , Any ] = {"type" : type }
387388 if reference_context is not None :
388389 params ["referenceContext" ] = reference_context
389390 if background is not None :
@@ -411,7 +412,7 @@ def get_tree(
411412 -------
412413 List[BrowsingContextInfo]: A list of browsing context information.
413414 """
414- params = {}
415+ params : dict [ str , Any ] = {}
415416 if max_depth is not None :
416417 params ["maxDepth" ] = max_depth
417418 if root is not None :
@@ -434,7 +435,7 @@ def handle_user_prompt(
434435 accept: Whether to accept the prompt.
435436 user_text: The text to enter in the prompt.
436437 """
437- params = {"context" : context }
438+ params : dict [ str , Any ] = {"context" : context }
438439 if accept is not None :
439440 params ["accept" ] = accept
440441 if user_text is not None :
@@ -464,7 +465,7 @@ def locate_nodes(
464465 -------
465466 List[Dict]: A list of nodes.
466467 """
467- params = {"context" : context , "locator" : locator }
468+ params : dict [ str , Any ] = {"context" : context , "locator" : locator }
468469 if max_node_count is not None :
469470 params ["maxNodeCount" ] = max_node_count
470471 if serialization_options is not None :
@@ -564,7 +565,7 @@ def reload(
564565 -------
565566 Dict: A dictionary containing the navigation result.
566567 """
567- params = {"context" : context }
568+ params : dict [ str , Any ] = {"context" : context }
568569 if ignore_cache is not None :
569570 params ["ignoreCache" ] = ignore_cache
570571 if wait is not None :
@@ -593,7 +594,7 @@ def set_viewport(
593594 ------
594595 Exception: If the browsing context is not a top-level traversable.
595596 """
596- params = {}
597+ params : dict [ str , Any ] = {}
597598 if context is not None :
598599 params ["context" ] = context
599600 if viewport is not None :
@@ -621,7 +622,7 @@ def traverse_history(self, context: str, delta: int) -> dict:
621622 result = self .conn .execute (command_builder ("browsingContext.traverseHistory" , params ))
622623 return result
623624
624- def _on_event (self , event_name : str , callback : callable ) -> int :
625+ def _on_event (self , event_name : str , callback : Callable ) -> int :
625626 """Set a callback function to subscribe to a browsing context event.
626627
627628 Parameters:
@@ -665,7 +666,7 @@ def _callback(event_data):
665666
666667 return callback_id
667668
668- def add_event_handler (self , event : str , callback : callable , contexts : Optional [list [str ]] = None ) -> int :
669+ def add_event_handler (self , event : str , callback : Callable , contexts : Optional [list [str ]] = None ) -> int :
669670 """Add an event handler to the browsing context.
670671
671672 Parameters:
@@ -710,9 +711,9 @@ def remove_event_handler(self, event: str, callback_id: int) -> None:
710711 except KeyError :
711712 raise Exception (f"Event { event } not found" )
712713
713- event = BrowsingContextEvent (event_name )
714+ event_obj = BrowsingContextEvent (event_name )
714715
715- self .conn .remove_callback (event , callback_id )
716+ self .conn .remove_callback (event_obj , callback_id )
716717 self .subscriptions [event_name ].remove (callback_id )
717718 if len (self .subscriptions [event_name ]) == 0 :
718719 params = {"events" : [event_name ]}
0 commit comments