1515# specific language governing permissions and limitations
1616# under the License.
1717
18- from typing import Optional , Union
18+ from typing import Optional , Union , Callable
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" ) or 0 ),
73+ url = str ( json .get ("url" ) ),
7474 )
7575
7676
@@ -109,11 +109,11 @@ def from_json(cls, json: dict) -> "BrowsingContextInfo":
109109 """
110110 children = None
111111 if json .get ("children" ) is not None :
112- children = [BrowsingContextInfo .from_json (child ) for child in json .get ("children" )]
112+ children = [BrowsingContextInfo .from_json (child ) for child in json .get ("children" , [] )]
113113
114114 return cls (
115- context = json .get ("context" ),
116- url = json .get ("url" ),
115+ context = str ( json .get ("context" ) ),
116+ url = str ( json .get ("url" ) ),
117117 children = children ,
118118 parent = json .get ("parent" ),
119119 user_context = json .get ("userContext" ),
@@ -149,11 +149,11 @@ def from_json(cls, json: dict) -> "DownloadWillBeginParams":
149149 DownloadWillBeginParams: A new instance of DownloadWillBeginParams.
150150 """
151151 return cls (
152- context = json .get ("context" ),
152+ context = str ( json .get ("context" ) ),
153153 navigation = json .get ("navigation" ),
154- timestamp = json .get ("timestamp" ),
155- url = json .get ("url" ),
156- suggested_filename = json .get ("suggestedFilename" ),
154+ timestamp = int ( json .get ("timestamp" ) or 0 ),
155+ url = str ( json .get ("url" ) ),
156+ suggested_filename = str ( json .get ("suggestedFilename" ) ),
157157 )
158158
159159
@@ -187,10 +187,10 @@ def from_json(cls, json: dict) -> "UserPromptOpenedParams":
187187 UserPromptOpenedParams: A new instance of UserPromptOpenedParams.
188188 """
189189 return cls (
190- context = json .get ("context" ),
191- handler = json .get ("handler" ),
192- message = json .get ("message" ),
193- type = json .get ("type" ),
190+ context = str ( json .get ("context" ) ),
191+ handler = str ( json .get ("handler" ) ),
192+ message = str ( json .get ("message" ) ),
193+ type = str ( json .get ("type" ) ),
194194 default_value = json .get ("defaultValue" ),
195195 )
196196
@@ -223,9 +223,9 @@ def from_json(cls, json: dict) -> "UserPromptClosedParams":
223223 UserPromptClosedParams: A new instance of UserPromptClosedParams.
224224 """
225225 return cls (
226- context = json .get ("context" ),
227- accepted = json .get ("accepted" ),
228- type = json .get ("type" ),
226+ context = str ( json .get ("context" ) ),
227+ accepted = bool ( json .get ("accepted" ) ),
228+ type = str ( json .get ("type" ) ),
229229 user_text = json .get ("userText" ),
230230 )
231231
@@ -254,8 +254,8 @@ def from_json(cls, json: dict) -> "HistoryUpdatedParams":
254254 HistoryUpdatedParams: A new instance of HistoryUpdatedParams.
255255 """
256256 return cls (
257- context = json .get ("context" ),
258- url = json .get ("url" ),
257+ context = str ( json .get ("context" ) ),
258+ url = str ( json .get ("url" ) ),
259259 )
260260
261261
@@ -278,7 +278,7 @@ def from_json(cls, json: dict) -> "BrowsingContextEvent":
278278 -------
279279 BrowsingContextEvent: A new instance of BrowsingContextEvent.
280280 """
281- return cls (event_class = json .get ("event_class" ), ** json )
281+ return cls (event_class = str ( json .get ("event_class" ) ), ** json )
282282
283283
284284class BrowsingContext :
@@ -341,9 +341,9 @@ def capture_screenshot(
341341 """
342342 params = {"context" : context , "origin" : origin }
343343 if format is not None :
344- params ["format" ] = format
344+ params ["format" ] = str ( format )
345345 if clip is not None :
346- params ["clip" ] = clip
346+ params ["clip" ] = str ( clip )
347347
348348 result = self .conn .execute (command_builder ("browsingContext.captureScreenshot" , params ))
349349 return result ["data" ]
@@ -387,7 +387,7 @@ def create(
387387 if reference_context is not None :
388388 params ["referenceContext" ] = reference_context
389389 if background is not None :
390- params ["background" ] = background
390+ params ["background" ] = str ( background )
391391 if user_context is not None :
392392 params ["userContext" ] = user_context
393393
@@ -415,7 +415,7 @@ def get_tree(
415415 if max_depth is not None :
416416 params ["maxDepth" ] = max_depth
417417 if root is not None :
418- params ["root" ] = root
418+ params ["root" ] = int ( root or 0 )
419419
420420 result = self .conn .execute (command_builder ("browsingContext.getTree" , params ))
421421 return [BrowsingContextInfo .from_json (context ) for context in result ["contexts" ]]
@@ -436,7 +436,7 @@ def handle_user_prompt(
436436 """
437437 params = {"context" : context }
438438 if accept is not None :
439- params ["accept" ] = accept
439+ params ["accept" ] = str ( accept )
440440 if user_text is not None :
441441 params ["userText" ] = user_text
442442
@@ -466,7 +466,7 @@ def locate_nodes(
466466 """
467467 params = {"context" : context , "locator" : locator }
468468 if max_node_count is not None :
469- params ["maxNodeCount" ] = max_node_count
469+ params ["maxNodeCount" ] = [ int ( max_node_count or 0 )]
470470 if serialization_options is not None :
471471 params ["serializationOptions" ] = serialization_options
472472 if start_nodes is not None :
@@ -566,7 +566,7 @@ def reload(
566566 """
567567 params = {"context" : context }
568568 if ignore_cache is not None :
569- params ["ignoreCache" ] = ignore_cache
569+ params ["ignoreCache" ] = str ( ignore_cache )
570570 if wait is not None :
571571 params ["wait" ] = wait
572572
@@ -597,11 +597,11 @@ def set_viewport(
597597 if context is not None :
598598 params ["context" ] = context
599599 if viewport is not None :
600- params ["viewport" ] = viewport
600+ params ["viewport" ] = str ( viewport )
601601 if device_pixel_ratio is not None :
602- params ["devicePixelRatio" ] = device_pixel_ratio
602+ params ["devicePixelRatio" ] = str ( device_pixel_ratio )
603603 if user_contexts is not None :
604- params ["userContexts" ] = user_contexts
604+ params ["userContexts" ] = str ( user_contexts )
605605
606606 self .conn .execute (command_builder ("browsingContext.setViewport" , params ))
607607
@@ -621,7 +621,7 @@ def traverse_history(self, context: str, delta: int) -> dict:
621621 result = self .conn .execute (command_builder ("browsingContext.traverseHistory" , params ))
622622 return result
623623
624- def _on_event (self , event_name : str , callback : callable ) -> int :
624+ def _on_event (self , event_name : str , callback : Callable ) -> int :
625625 """Set a callback function to subscribe to a browsing context event.
626626
627627 Parameters:
@@ -665,7 +665,7 @@ def _callback(event_data):
665665
666666 return callback_id
667667
668- def add_event_handler (self , event : str , callback : callable , contexts : Optional [list [str ]] = None ) -> int :
668+ def add_event_handler (self , event : str , callback : Callable , contexts : Optional [list [str ]] = None ) -> int :
669669 """Add an event handler to the browsing context.
670670
671671 Parameters:
@@ -710,7 +710,7 @@ def remove_event_handler(self, event: str, callback_id: int) -> None:
710710 except KeyError :
711711 raise Exception (f"Event { event } not found" )
712712
713- event = BrowsingContextEvent (event_name )
713+ event = str ( BrowsingContextEvent (event_name ) )
714714
715715 self .conn .remove_callback (event , callback_id )
716716 self .subscriptions [event_name ].remove (callback_id )
0 commit comments