@@ -334,6 +334,7 @@ def load_selenium(args, wire=False):
334334 resolved = get_browser_profile_path (browser_name , profile )
335335 if resolved :
336336 args .user_data_dir = resolved
337+ args .profile_directory = profile
337338 _profile = profile
338339 log .info ("Using browser profile: %s" , resolved )
339340 if browser_name == "firefox" and not getattr (args , "chrome" , False ):
@@ -378,6 +379,7 @@ def load_selenium(args, wire=False):
378379 if xvfb is False :
379380 options .add_argument ("--headless" )
380381
382+ backup_profile_prefs (args )
381383 args .driver = webdriver .Firefox (service = service , options = options )
382384
383385 if not args .user_data_dir :
@@ -405,15 +407,15 @@ def load_selenium(args, wire=False):
405407 options .add_argument (f"--user-data-dir={ args .user_data_dir } " )
406408 if _profile :
407409 options .add_argument (f"--profile-directory={ _profile } " )
408-
409- options .add_experimental_option (
410- "prefs" ,
411- {
412- "credentials_enable_service" : False ,
413- "profile.password_manager_enabled" : False ,
414- "profile.default_content_setting_values.notifications" : 2 ,
415- },
416- )
410+ else :
411+ options .add_experimental_option (
412+ "prefs" ,
413+ {
414+ "credentials_enable_service" : False ,
415+ "profile.password_manager_enabled" : False ,
416+ "profile.default_content_setting_values.notifications" : 2 ,
417+ },
418+ )
417419 options .add_argument ("--disable-notifications" )
418420 options .add_argument ("--mute-audio" )
419421 if xvfb is False :
@@ -432,11 +434,48 @@ def load_selenium(args, wire=False):
432434 else :
433435 log .exception ("Could not install chrome extension. Missing file %s" , addon_path )
434436
437+ backup_profile_prefs (args )
435438 args .driver = webdriver .Chrome (options = options )
436439
437440
441+ firefox_profile_prefs = ("user.js" , "prefs.js" )
442+
443+
444+ def selenium_profile_prefs (args ):
445+ if getattr (args , "chrome" , False ):
446+ # chromedriver writes experimental prefs + exit state into these;
447+ # actual tab session data lives in <profile>/Sessions and is untouched
448+ profile_subdir = getattr (args , "profile_directory" , None ) or "Default"
449+ return ("Local State" , str (Path (profile_subdir ) / "Preferences" ))
450+ return firefox_profile_prefs
451+
452+
453+ def backup_profile_prefs (args ):
454+ if not getattr (args , "user_data_dir" , None ):
455+ return
456+ profile_dir = Path (args .user_data_dir )
457+ args .profile_prefs_backup = {
458+ name : (profile_dir / name ).read_bytes () if (profile_dir / name ).exists () else None
459+ for name in selenium_profile_prefs (args )
460+ }
461+
462+
463+ def restore_profile_prefs (args ):
464+ backup = getattr (args , "profile_prefs_backup" , None )
465+ if not backup :
466+ return
467+ profile_dir = Path (args .user_data_dir )
468+ for name , content in backup .items ():
469+ target = profile_dir / name
470+ if content is None :
471+ target .unlink (missing_ok = True )
472+ else :
473+ target .write_bytes (content )
474+
475+
438476def quit_selenium (args ):
439477 args .driver .quit ()
478+ restore_profile_prefs (args )
440479 if args .verbose < consts .LOG_DEBUG and not getattr (args , "manual" , False ):
441480 with suppress (Exception ):
442481 args .driver_display .stop ()
0 commit comments