Skip to content

Commit 19c556c

Browse files
committed
feat(browser utils): added save_working_profile_directory to ease saving the profile before closing the browser
1 parent d1cb46c commit 19c556c

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

iarp_utils/browser/utils.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import os
12
import re
23
import subprocess
4+
import shutil
5+
import time
36

47
from ..system import OSTypes
58

@@ -93,3 +96,32 @@ def _process_version_commands(name, cmds, pattern=r'\d+\.\d+\.\d+\.\d+|\d+\.\d+\
9396
if not version:
9497
raise ValueError(f'Could not process version for {name} commands output: {cmds}')
9598
return version.group(0)
99+
100+
101+
def save_working_profile_directory(driver, profile_storage_path):
102+
""" Saves the currently in-use browser profile directory to a custom location.
103+
104+
Args:
105+
driver: The driver running the
106+
profile_storage_path: Where to save the profiles contents.
107+
"""
108+
109+
driver.browser.execute_script("window.close()")
110+
111+
time.sleep(0.5)
112+
113+
# Copy the profile directory (must be done BEFORE driver.quit()!)
114+
current_profile_path = driver.active_driver._get_active_profile_data_directory()
115+
116+
assert os.path.isdir(current_profile_path)
117+
118+
try:
119+
shutil.rmtree(profile_storage_path)
120+
except FileNotFoundError:
121+
pass
122+
123+
shutil.copytree(
124+
src=current_profile_path,
125+
dst=profile_storage_path,
126+
ignore_dangling_symlinks=True
127+
)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='iarp_utils',
5-
version='1.2.1',
5+
version='1.3.0',
66
description='A personal collection of common python utilities used in various projects',
77
url='https://bitbucket.org/iarp/iarp-python-utils/',
88
author='IARP',

0 commit comments

Comments
 (0)