Skip to content

Commit 75c43f2

Browse files
FIX: Ensuring the temp folder is deleted at the end
1 parent f41e1bb commit 75c43f2

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

botcity/web/bot.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,16 @@
4343
logger = logging.getLogger(__name__)
4444

4545

46-
def _cleanup(bot: ReferenceType[WebBot]):
47-
if bot() is not None:
46+
def _cleanup(driver, temp_dir):
47+
if driver() is not None:
48+
try:
49+
if driver().service.is_connectable():
50+
driver().quit()
51+
except Exception:
52+
pass
53+
54+
if temp_dir:
4855
try:
49-
bot().stop_browser()
50-
temp_dir = bot()._botcity_temp_dir
51-
if not temp_dir:
52-
return None
5356
shutil.rmtree(temp_dir, ignore_errors=True)
5457
except Exception:
5558
pass
@@ -94,8 +97,6 @@ def __init__(self, headless=False):
9497
self._download_folder_path = os.getcwd()
9598
self._botcity_temp_dir = None
9699

97-
atexit.register(_cleanup, ref(self))
98-
99100
def __enter__(self):
100101
pass
101102

@@ -279,10 +280,11 @@ def check_driver():
279280
self.capabilities = cap
280281
driver_path = self.driver_path or check_driver()
281282
self.driver_path = driver_path
282-
283283
self._driver = driver_class(options=opt, desired_capabilities=cap, executable_path=driver_path)
284284
self.set_screen_resolution()
285285

286+
atexit.register(_cleanup, ref(self._driver), self.options._botcity_temp_dir)
287+
286288
def stop_browser(self):
287289
"""
288290
Stops the Chrome browser and clean up the User Data Directory.

0 commit comments

Comments
 (0)