Skip to content

Commit 7ab885f

Browse files
authored
Merge pull request #99 from kayqueGovetri/FIX/implement-atexit-browser
FIX: Implement atexit to stop browser
2 parents b592e15 + d1aa707 commit 7ab885f

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
@@ -27,6 +30,7 @@
2730
from selenium.webdriver.support.wait import WebDriverWait, TimeoutException, NoSuchElementException
2831
from selenium.webdriver.support import expected_conditions as EC
2932
from selenium.webdriver.common.print_page_options import PrintOptions
33+
from weakref import ReferenceType, ref
3034

3135
from . import config, cv2find, compat
3236
from .browsers import BROWSER_CONFIGS, Browser, PageLoadStrategy
@@ -41,6 +45,14 @@
4145
logger = logging.getLogger(__name__)
4246

4347

48+
def _cleanup(bot: ReferenceType[WebBot]):
49+
if bot() is not None:
50+
try:
51+
bot().stop_browser()
52+
except Exception:
53+
pass
54+
55+
4456
class WebBot(BaseBot):
4557
KEYS = Keys
4658
DEFAULT_DIMENSIONS = (1600, 900)
@@ -80,6 +92,8 @@ def __init__(self, headless=False):
8092

8193
self._download_folder_path = os.getcwd()
8294

95+
atexit.register(_cleanup, ref(self))
96+
8397
def __enter__(self):
8498
pass
8599

0 commit comments

Comments
 (0)