15
15
# specific language governing permissions and limitations
16
16
# under the License.
17
17
18
- from typing import Optional , Union
18
+ from typing import Optional , Union , Callable
19
19
20
20
from selenium .webdriver .common .bidi .common import command_builder
21
21
@@ -67,10 +67,10 @@ def from_json(cls, json: dict) -> "NavigationInfo":
67
67
NavigationInfo: A new instance of NavigationInfo.
68
68
"""
69
69
return cls (
70
- context = json .get ("context" ),
70
+ context = str ( json .get ("context" ) ),
71
71
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" ) ),
74
74
)
75
75
76
76
@@ -109,11 +109,11 @@ def from_json(cls, json: dict) -> "BrowsingContextInfo":
109
109
"""
110
110
children = None
111
111
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" , [] )]
113
113
114
114
return cls (
115
- context = json .get ("context" ),
116
- url = json .get ("url" ),
115
+ context = str ( json .get ("context" ) ),
116
+ url = str ( json .get ("url" ) ),
117
117
children = children ,
118
118
parent = json .get ("parent" ),
119
119
user_context = json .get ("userContext" ),
@@ -149,11 +149,11 @@ def from_json(cls, json: dict) -> "DownloadWillBeginParams":
149
149
DownloadWillBeginParams: A new instance of DownloadWillBeginParams.
150
150
"""
151
151
return cls (
152
- context = json .get ("context" ),
152
+ context = str ( json .get ("context" ) ),
153
153
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" ) ),
157
157
)
158
158
159
159
@@ -187,10 +187,10 @@ def from_json(cls, json: dict) -> "UserPromptOpenedParams":
187
187
UserPromptOpenedParams: A new instance of UserPromptOpenedParams.
188
188
"""
189
189
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" ) ),
194
194
default_value = json .get ("defaultValue" ),
195
195
)
196
196
@@ -223,9 +223,9 @@ def from_json(cls, json: dict) -> "UserPromptClosedParams":
223
223
UserPromptClosedParams: A new instance of UserPromptClosedParams.
224
224
"""
225
225
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" ) ),
229
229
user_text = json .get ("userText" ),
230
230
)
231
231
@@ -254,8 +254,8 @@ def from_json(cls, json: dict) -> "HistoryUpdatedParams":
254
254
HistoryUpdatedParams: A new instance of HistoryUpdatedParams.
255
255
"""
256
256
return cls (
257
- context = json .get ("context" ),
258
- url = json .get ("url" ),
257
+ context = str ( json .get ("context" ) ),
258
+ url = str ( json .get ("url" ) ),
259
259
)
260
260
261
261
@@ -278,7 +278,7 @@ def from_json(cls, json: dict) -> "BrowsingContextEvent":
278
278
-------
279
279
BrowsingContextEvent: A new instance of BrowsingContextEvent.
280
280
"""
281
- return cls (event_class = json .get ("event_class" ), ** json )
281
+ return cls (event_class = str ( json .get ("event_class" ) ), ** json )
282
282
283
283
284
284
class BrowsingContext :
@@ -341,9 +341,9 @@ def capture_screenshot(
341
341
"""
342
342
params = {"context" : context , "origin" : origin }
343
343
if format is not None :
344
- params ["format" ] = format
344
+ params ["format" ] = str ( format )
345
345
if clip is not None :
346
- params ["clip" ] = clip
346
+ params ["clip" ] = str ( clip )
347
347
348
348
result = self .conn .execute (command_builder ("browsingContext.captureScreenshot" , params ))
349
349
return result ["data" ]
@@ -387,7 +387,7 @@ def create(
387
387
if reference_context is not None :
388
388
params ["referenceContext" ] = reference_context
389
389
if background is not None :
390
- params ["background" ] = background
390
+ params ["background" ] = str ( background )
391
391
if user_context is not None :
392
392
params ["userContext" ] = user_context
393
393
@@ -415,7 +415,7 @@ def get_tree(
415
415
if max_depth is not None :
416
416
params ["maxDepth" ] = max_depth
417
417
if root is not None :
418
- params ["root" ] = root
418
+ params ["root" ] = int ( root or 0 )
419
419
420
420
result = self .conn .execute (command_builder ("browsingContext.getTree" , params ))
421
421
return [BrowsingContextInfo .from_json (context ) for context in result ["contexts" ]]
@@ -436,7 +436,7 @@ def handle_user_prompt(
436
436
"""
437
437
params = {"context" : context }
438
438
if accept is not None :
439
- params ["accept" ] = accept
439
+ params ["accept" ] = str ( accept )
440
440
if user_text is not None :
441
441
params ["userText" ] = user_text
442
442
@@ -466,7 +466,7 @@ def locate_nodes(
466
466
"""
467
467
params = {"context" : context , "locator" : locator }
468
468
if max_node_count is not None :
469
- params ["maxNodeCount" ] = max_node_count
469
+ params ["maxNodeCount" ] = [ int ( max_node_count or 0 )]
470
470
if serialization_options is not None :
471
471
params ["serializationOptions" ] = serialization_options
472
472
if start_nodes is not None :
@@ -566,7 +566,7 @@ def reload(
566
566
"""
567
567
params = {"context" : context }
568
568
if ignore_cache is not None :
569
- params ["ignoreCache" ] = ignore_cache
569
+ params ["ignoreCache" ] = str ( ignore_cache )
570
570
if wait is not None :
571
571
params ["wait" ] = wait
572
572
@@ -597,11 +597,11 @@ def set_viewport(
597
597
if context is not None :
598
598
params ["context" ] = context
599
599
if viewport is not None :
600
- params ["viewport" ] = viewport
600
+ params ["viewport" ] = str ( viewport )
601
601
if device_pixel_ratio is not None :
602
- params ["devicePixelRatio" ] = device_pixel_ratio
602
+ params ["devicePixelRatio" ] = str ( device_pixel_ratio )
603
603
if user_contexts is not None :
604
- params ["userContexts" ] = user_contexts
604
+ params ["userContexts" ] = str ( user_contexts )
605
605
606
606
self .conn .execute (command_builder ("browsingContext.setViewport" , params ))
607
607
@@ -621,7 +621,7 @@ def traverse_history(self, context: str, delta: int) -> dict:
621
621
result = self .conn .execute (command_builder ("browsingContext.traverseHistory" , params ))
622
622
return result
623
623
624
- def _on_event (self , event_name : str , callback : callable ) -> int :
624
+ def _on_event (self , event_name : str , callback : Callable ) -> int :
625
625
"""Set a callback function to subscribe to a browsing context event.
626
626
627
627
Parameters:
@@ -665,7 +665,7 @@ def _callback(event_data):
665
665
666
666
return callback_id
667
667
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 :
669
669
"""Add an event handler to the browsing context.
670
670
671
671
Parameters:
@@ -710,7 +710,7 @@ def remove_event_handler(self, event: str, callback_id: int) -> None:
710
710
except KeyError :
711
711
raise Exception (f"Event { event } not found" )
712
712
713
- event = BrowsingContextEvent (event_name )
713
+ event = str ( BrowsingContextEvent (event_name ) )
714
714
715
715
self .conn .remove_callback (event , callback_id )
716
716
self .subscriptions [event_name ].remove (callback_id )
0 commit comments