Skip to content

Commit e2d30d8

Browse files
committed
STYLE: Implement Exception in try/except
1 parent 107c10b commit e2d30d8

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

botcity/web/bot.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import annotations
2+
3+
import atexit
14
import base64
25
import functools
36
import glob
@@ -25,6 +28,7 @@
2528
from selenium.webdriver.remote.webelement import WebElement
2629
from selenium.webdriver.support.wait import WebDriverWait, TimeoutException, NoSuchElementException
2730
from selenium.webdriver.support import expected_conditions as EC
31+
from weakref import ReferenceType, ref
2832

2933
from . import config, cv2find
3034
from .browsers import BROWSER_CONFIGS, Browser, PageLoadStrategy
@@ -39,6 +43,14 @@
3943
logger = logging.getLogger(__name__)
4044

4145

46+
def _cleanup(bot: ReferenceType[WebBot]):
47+
if bot() is not None:
48+
try:
49+
bot().stop_browser()
50+
except Exception:
51+
pass
52+
53+
4254
class WebBot(BaseBot):
4355
KEYS = Keys
4456
DEFAULT_DIMENSIONS = (1600, 900)
@@ -77,6 +89,8 @@ def __init__(self, headless=False):
7789

7890
self._download_folder_path = os.getcwd()
7991

92+
atexit.register(_cleanup, ref(self))
93+
8094
def __enter__(self):
8195
pass
8296

0 commit comments

Comments
 (0)