|
30 | 30 | from selenium.webdriver.support.wait import WebDriverWait, TimeoutException, NoSuchElementException |
31 | 31 | from selenium.webdriver.support import expected_conditions as EC |
32 | 32 | from selenium.webdriver.common.print_page_options import PrintOptions |
33 | | -from weakref import ReferenceType, ref |
| 33 | +from weakref import ref |
34 | 34 |
|
35 | 35 | from . import config, cv2find, compat |
36 | 36 | from .browsers import BROWSER_CONFIGS, Browser, PageLoadStrategy |
|
45 | 45 | logger = logging.getLogger(__name__) |
46 | 46 |
|
47 | 47 |
|
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: |
50 | 57 | try: |
51 | | - bot().stop_browser() |
52 | | - temp_dir = bot()._botcity_temp_dir |
53 | | - if not temp_dir: |
54 | | - return None |
55 | 58 | shutil.rmtree(temp_dir, ignore_errors=True) |
56 | 59 | except Exception: |
57 | 60 | pass |
@@ -97,8 +100,6 @@ def __init__(self, headless=False): |
97 | 100 | self._download_folder_path = os.getcwd() |
98 | 101 | self._botcity_temp_dir = None |
99 | 102 |
|
100 | | - atexit.register(_cleanup, ref(self)) |
101 | | - |
102 | 103 | def __enter__(self): |
103 | 104 | pass |
104 | 105 |
|
@@ -306,6 +307,8 @@ def check_driver(): |
306 | 307 | self._others_configurations() |
307 | 308 | self.set_screen_resolution() |
308 | 309 |
|
| 310 | + atexit.register(_cleanup, ref(self._driver), self.options._botcity_temp_dir) |
| 311 | + |
309 | 312 | def _instantiate_driver(self, driver_class, func_def_options): |
310 | 313 | """ |
311 | 314 | It is necessary to create this function because we isolated the instantiation of the driver, |
|
0 commit comments