Skip to content

Commit f372151

Browse files
FIX: Ensuring the temp folder is deleted at the end
1 parent 38b7e24 commit f372151

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

botcity/web/bot.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from selenium.webdriver.support.wait import WebDriverWait, TimeoutException, NoSuchElementException
3131
from selenium.webdriver.support import expected_conditions as EC
3232
from selenium.webdriver.common.print_page_options import PrintOptions
33-
from weakref import ReferenceType, ref
33+
from weakref import ref
3434

3535
from . import config, cv2find, compat
3636
from .browsers import BROWSER_CONFIGS, Browser, PageLoadStrategy
@@ -45,13 +45,16 @@
4545
logger = logging.getLogger(__name__)
4646

4747

48-
def _cleanup(bot: ReferenceType[WebBot]):
49-
if bot() is not None:
48+
def _cleanup(driver, temp_dir):
49+
if driver() is not None:
50+
try:
51+
if driver().service.is_connectable():
52+
driver().quit()
53+
except Exception:
54+
pass
55+
56+
if temp_dir:
5057
try:
51-
bot().stop_browser()
52-
temp_dir = bot()._botcity_temp_dir
53-
if not temp_dir:
54-
return None
5558
shutil.rmtree(temp_dir, ignore_errors=True)
5659
except Exception:
5760
pass
@@ -97,8 +100,6 @@ def __init__(self, headless=False):
97100
self._download_folder_path = os.getcwd()
98101
self._botcity_temp_dir = None
99102

100-
atexit.register(_cleanup, ref(self))
101-
102103
def __enter__(self):
103104
pass
104105

@@ -306,6 +307,8 @@ def check_driver():
306307
self._others_configurations()
307308
self.set_screen_resolution()
308309

310+
atexit.register(_cleanup, ref(self._driver), self.options._botcity_temp_dir)
311+
309312
def _instantiate_driver(self, driver_class, func_def_options):
310313
"""
311314
It is necessary to create this function because we isolated the instantiation of the driver,

0 commit comments

Comments
 (0)