diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index 64f0cc6..ac0fe6d 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -93,7 +93,11 @@ class ChromiumBase(BasePage): if self._debug_recorder: self._debug_recorder.add_data((perf_counter(), '获取document', '开始')) - self._wait_loaded() + try: # 处理标签页关闭的情况 + self._wait_loaded() + except ConnectionError: + return + while True: try: root_id = self._tab_obj.DOM.getDocument()['root']['nodeId'] @@ -251,7 +255,10 @@ class ChromiumBase(BasePage): @property def ready_state(self): """返回当前页面加载状态,'loading' 'interactive' 'complete'""" - return self._tab_obj.Runtime.evaluate(expression='document.readyState;')['result']['value'] + try: + return self._tab_obj.Runtime.evaluate(expression='document.readyState;')['result']['value'] + except KeyError: + raise ConnectionError('标签页或连接已关闭。') @property def size(self): diff --git a/DrissionPage/chromium_driver.py b/DrissionPage/chromium_driver.py index 45c9b45..e2e1a72 100644 --- a/DrissionPage/chromium_driver.py +++ b/DrissionPage/chromium_driver.py @@ -146,8 +146,9 @@ class ChromiumDriver(object): if event['method'] in self.event_handlers: try: self.event_handlers[event['method']](**event['params']) - except Exception as e: - raise RuntimeError(f"回调函数 {event['method']} 错误:{e}") + except Exception: + pass + # raise RuntimeError(f"回调函数 {event['method']} 错误:{e}") self.event_queue.task_done() @@ -169,10 +170,12 @@ class ChromiumDriver(object): raise CallMethodException("参数必须是key=value形式。") if self._stopped.is_set(): - raise RuntimeError("Driver已经停止。") + return {'tab_closed': True} timeout = kwargs.pop("_timeout", None) result = self._send({"method": _method, "params": kwargs}, timeout=timeout) + if result is None: + return {'tab_closed': True} if 'result' not in result and 'error' in result: raise CallMethodException(f"\n调用方法:{_method}\n参数:{kwargs}\n错误:{result['error']['message']}") @@ -204,6 +207,9 @@ class ChromiumDriver(object): self._stopped.set() if self._ws: self._ws.close() + self.event_handlers = {} + self.method_results = {} + self.event_queue = Queue() return True def set_listener(self, event, callback): diff --git a/setup.py b/setup.py index 75818d4..4823d9a 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh: setup( name="DrissionPage", - version="3.1.3", + version="3.1.4", author="g1879", author_email="g1879@qq.com", description="A module that integrates selenium and requests session, encapsulates common page operations.",