@@ -46,9 +46,8 @@ def deconstruct_browser():
4646 logger .debug (
4747 "Problem removing data dir %s\n "
4848 "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 )
5251 )
5352 break
5453 time .sleep (0.15 )
@@ -190,7 +189,7 @@ async def wait(self, time: Union[float, int] = 1) -> Browser:
190189
191190 sleep = wait
192191 """Alias for wait"""
193- def _handle_target_update (
192+ async def _handle_target_update (
194193 self ,
195194 event : Union [
196195 cdp .target .TargetInfoChanged ,
@@ -224,21 +223,21 @@ def _handle_target_update(
224223 current_tab .target = target_info
225224 elif isinstance (event , cdp .target .TargetCreated ):
226225 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 } "
241230 )
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+ )
242241 elif isinstance (event , cdp .target .TargetDestroyed ):
243242 current_tab = next (
244243 filter (
@@ -287,62 +286,42 @@ async def get(
287286 connection : tab .Tab = next (
288287 filter (lambda item : item .type_ == "page" , self .targets )
289288 )
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
307298 if (
308299 hasattr (sb_config , "_cdp_user_agent" )
309300 and sb_config ._cdp_user_agent
310301 ):
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
340307 if (
341308 hasattr (sb_config , "_cdp_geolocation" )
342309 and sb_config ._cdp_geolocation
343310 ):
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 :
344323 await connection .send (cdp .page .navigate ("about:blank" ))
345- await connection .set_geolocation (sb_config . _cdp_geolocation )
324+ await connection .set_geolocation (_cdp_geolocation )
346325 # Use the tab to navigate to new url
347326 frame_id , loader_id , * _ = await connection .send (
348327 cdp .page .navigate (url )
@@ -376,8 +355,8 @@ async def start(self=None) -> Browser:
376355 self .config .port = util .free_port ()
377356 if not connect_existing :
378357 logger .debug (
379- "BROWSER EXECUTABLE PATH: %s" ,
380- self .config .browser_executable_path ,
358+ "BROWSER EXECUTABLE PATH: %s"
359+ % self .config .browser_executable_path ,
381360 )
382361 if not pathlib .Path (self .config .browser_executable_path ).exists ():
383362 raise FileNotFoundError (
@@ -782,10 +761,8 @@ async def save(self, file: PathLike = ".session.dat", pattern: str = ".*"):
782761 for cookie in cookies :
783762 for match in pattern .finditer (str (cookie .__dict__ )):
784763 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 )
789766 )
790767 included_cookies .append (cookie )
791768 break
@@ -822,10 +799,8 @@ async def load(self, file: PathLike = ".session.dat", pattern: str = ".*"):
822799 for match in pattern .finditer (str (cookie .__dict__ )):
823800 included_cookies .append (cookie )
824801 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 )
829804 )
830805 break
831806 await connection .send (cdp .network .set_cookies (included_cookies ))
0 commit comments