@@ -46,9 +46,8 @@ def deconstruct_browser():
46
46
logger .debug (
47
47
"Problem removing data dir %s\n "
48
48
"Consider checking whether it's there "
49
- "and remove it by hand\n error: %s" ,
50
- _ .config .user_data_dir ,
51
- e ,
49
+ "and remove it by hand\n error: %s"
50
+ % (_ .config .user_data_dir , e )
52
51
)
53
52
break
54
53
time .sleep (0.15 )
@@ -190,7 +189,7 @@ async def wait(self, time: Union[float, int] = 1) -> Browser:
190
189
191
190
sleep = wait
192
191
"""Alias for wait"""
193
- def _handle_target_update (
192
+ async def _handle_target_update (
194
193
self ,
195
194
event : Union [
196
195
cdp .target .TargetInfoChanged ,
@@ -224,21 +223,21 @@ def _handle_target_update(
224
223
current_tab .target = target_info
225
224
elif isinstance (event , cdp .target .TargetCreated ):
226
225
target_info : cdp .target .TargetInfo = event .target_info
227
- from .tab import Tab
228
-
229
- new_target = Tab (
230
- (
231
- f"ws://{ self .config .host } :{ self .config .port } "
232
- f"/devtools/{ target_info .type_ or 'page' } "
233
- f"/{ target_info .target_id } "
234
- ),
235
- target = target_info ,
236
- browser = self ,
237
- )
238
- self .targets .append (new_target )
239
- logger .debug (
240
- "Target #%d created => %s" , len (self .targets ), new_target
226
+ websocket_url = (
227
+ f"ws://{ self .config .host } :{ self .config .port } "
228
+ f"/devtools/{ target_info .type_ or 'page' } "
229
+ f"/{ target_info .target_id } "
241
230
)
231
+ async with tab .Tab (
232
+ websocket_url = websocket_url ,
233
+ target = target_info ,
234
+ browser = self
235
+ ) as new_target :
236
+ self .targets .append (new_target )
237
+ logger .debug (
238
+ "Target #%d created => %s"
239
+ % (len (self .targets ), new_target )
240
+ )
242
241
elif isinstance (event , cdp .target .TargetDestroyed ):
243
242
current_tab = next (
244
243
filter (
@@ -287,62 +286,42 @@ async def get(
287
286
connection : tab .Tab = next (
288
287
filter (lambda item : item .type_ == "page" , self .targets )
289
288
)
290
- if hasattr (sb_config , "_cdp_locale" ) and sb_config ._cdp_locale :
291
- await connection .send (cdp .page .navigate ("about:blank" ))
292
- if (
293
- hasattr (sb_config , "_cdp_user_agent" )
294
- and sb_config ._cdp_user_agent
295
- ):
296
- pass
297
- elif (
298
- hasattr (sb_config , "_cdp_platform" )
299
- and sb_config ._cdp_platform
300
- ):
301
- pass
302
- else :
303
- await connection .set_locale (sb_config ._cdp_locale )
304
- if hasattr (sb_config , "_cdp_timezone" ) and sb_config ._cdp_timezone :
305
- await connection .send (cdp .page .navigate ("about:blank" ))
306
- await connection .set_timezone (sb_config ._cdp_timezone )
289
+ _cdp_timezone = None
290
+ _cdp_user_agent = ""
291
+ _cdp_locale = None
292
+ _cdp_platform = None
293
+ _cdp_geolocation = None
294
+ if (
295
+ hasattr (sb_config , "_cdp_timezone" ) and sb_config ._cdp_timezone
296
+ ):
297
+ _cdp_timezone = sb_config ._cdp_timezone
307
298
if (
308
299
hasattr (sb_config , "_cdp_user_agent" )
309
300
and sb_config ._cdp_user_agent
310
301
):
311
- await connection .send (cdp .page .navigate ("about:blank" ))
312
- if hasattr (sb_config , "_cdp_locale" ) and sb_config ._cdp_locale :
313
- _cdp_platform = None
314
- if (
315
- hasattr (sb_config , "_cdp_platform" )
316
- and sb_config ._cdp_platform
317
- ):
318
- _cdp_platform = sb_config ._cdp_platform
319
- await connection .set_user_agent (
320
- sb_config ._cdp_user_agent ,
321
- sb_config ._cdp_locale ,
322
- _cdp_platform ,
323
- )
324
- else :
325
- await connection .set_user_agent (sb_config ._cdp_user_agent )
326
- elif (
327
- hasattr (sb_config , "_cdp_platform" ) and sb_config ._cdp_platform
328
- ):
329
- await connection .send (cdp .page .navigate ("about:blank" ))
330
- if hasattr (sb_config , "_cdp_locale" ) and sb_config ._cdp_locale :
331
- _cdp_platform = sb_config ._cdp_platform
332
- await connection .set_user_agent (
333
- accept_language = sb_config ._cdp_locale ,
334
- platform = _cdp_platform ,
335
- )
336
- else :
337
- await connection .set_user_agent (
338
- platform = sb_config ._cdp_platform
339
- )
302
+ _cdp_user_agent = sb_config ._cdp_user_agent
303
+ if hasattr (sb_config , "_cdp_locale" ) and sb_config ._cdp_locale :
304
+ _cdp_locale = sb_config ._cdp_locale
305
+ if hasattr (sb_config , "_cdp_platform" ) and sb_config ._cdp_platform :
306
+ _cdp_platform = sb_config ._cdp_platform
340
307
if (
341
308
hasattr (sb_config , "_cdp_geolocation" )
342
309
and sb_config ._cdp_geolocation
343
310
):
311
+ _cdp_geolocation = sb_config ._cdp_geolocation
312
+ if _cdp_timezone :
313
+ await connection .send (cdp .page .navigate ("about:blank" ))
314
+ await connection .set_timezone (_cdp_timezone )
315
+ if _cdp_user_agent or _cdp_locale or _cdp_platform :
316
+ await connection .send (cdp .page .navigate ("about:blank" ))
317
+ await connection .set_user_agent (
318
+ user_agent = _cdp_user_agent ,
319
+ accept_language = _cdp_locale ,
320
+ platform = _cdp_platform ,
321
+ )
322
+ if _cdp_geolocation :
344
323
await connection .send (cdp .page .navigate ("about:blank" ))
345
- await connection .set_geolocation (sb_config . _cdp_geolocation )
324
+ await connection .set_geolocation (_cdp_geolocation )
346
325
# Use the tab to navigate to new url
347
326
frame_id , loader_id , * _ = await connection .send (
348
327
cdp .page .navigate (url )
@@ -376,8 +355,8 @@ async def start(self=None) -> Browser:
376
355
self .config .port = util .free_port ()
377
356
if not connect_existing :
378
357
logger .debug (
379
- "BROWSER EXECUTABLE PATH: %s" ,
380
- self .config .browser_executable_path ,
358
+ "BROWSER EXECUTABLE PATH: %s"
359
+ % self .config .browser_executable_path ,
381
360
)
382
361
if not pathlib .Path (self .config .browser_executable_path ).exists ():
383
362
raise FileNotFoundError (
@@ -782,10 +761,8 @@ async def save(self, file: PathLike = ".session.dat", pattern: str = ".*"):
782
761
for cookie in cookies :
783
762
for match in pattern .finditer (str (cookie .__dict__ )):
784
763
logger .debug (
785
- "Saved cookie for matching pattern '%s' => (%s: %s)" ,
786
- pattern .pattern ,
787
- cookie .name ,
788
- cookie .value ,
764
+ "Saved cookie for matching pattern '%s' => (%s: %s)"
765
+ % (pattern .pattern , cookie .name , cookie .value )
789
766
)
790
767
included_cookies .append (cookie )
791
768
break
@@ -822,10 +799,8 @@ async def load(self, file: PathLike = ".session.dat", pattern: str = ".*"):
822
799
for match in pattern .finditer (str (cookie .__dict__ )):
823
800
included_cookies .append (cookie )
824
801
logger .debug (
825
- "Loaded cookie for matching pattern '%s' => (%s: %s)" ,
826
- pattern .pattern ,
827
- cookie .name ,
828
- cookie .value ,
802
+ "Loaded cookie for matching pattern '%s' => (%s: %s)"
803
+ % (pattern .pattern , cookie .name , cookie .value )
829
804
)
830
805
break
831
806
await connection .send (cdp .network .set_cookies (included_cookies ))
0 commit comments