From 79e48cd9890da27ce5c7e9a6bd0c411bb6d961ee Mon Sep 17 00:00:00 2001 From: kayqueGovetri Date: Mon, 20 Nov 2023 20:35:20 -0300 Subject: [PATCH 1/2] FIX: Implement atexit to stop browser --- botcity/web/bot.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/botcity/web/bot.py b/botcity/web/bot.py index 0ddbf4a..c6db860 100644 --- a/botcity/web/bot.py +++ b/botcity/web/bot.py @@ -1,3 +1,6 @@ +from __future__ import annotations + +import atexit import base64 import functools import glob @@ -27,6 +30,7 @@ from selenium.webdriver.support.wait import WebDriverWait, TimeoutException, NoSuchElementException from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.print_page_options import PrintOptions +from weakref import ReferenceType, ref from . import config, cv2find, compat from .browsers import BROWSER_CONFIGS, Browser, PageLoadStrategy @@ -41,6 +45,14 @@ logger = logging.getLogger(__name__) +def _cleanup(bot: ReferenceType[WebBot]): + if bot() is not None: + try: + bot().stop_browser() + except: + pass + + class WebBot(BaseBot): KEYS = Keys DEFAULT_DIMENSIONS = (1600, 900) @@ -80,6 +92,8 @@ def __init__(self, headless=False): self._download_folder_path = os.getcwd() + atexit.register(_cleanup, ref(self)) + def __enter__(self): pass From d1aa70733daf2ec1070235141b4b18c6da41d741 Mon Sep 17 00:00:00 2001 From: kayqueGovetri Date: Tue, 21 Nov 2023 13:30:52 -0300 Subject: [PATCH 2/2] STYLE: Implement Exception in try/except --- botcity/web/bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/botcity/web/bot.py b/botcity/web/bot.py index c6db860..9b018d5 100644 --- a/botcity/web/bot.py +++ b/botcity/web/bot.py @@ -49,7 +49,7 @@ def _cleanup(bot: ReferenceType[WebBot]): if bot() is not None: try: bot().stop_browser() - except: + except Exception: pass