From b03e919989483c9c8ee33fb8937f5ab9b94e3816 Mon Sep 17 00:00:00 2001 From: g1879 Date: Wed, 8 Feb 2023 02:13:27 +0800 Subject: [PATCH] =?UTF-8?q?3.1.4=E4=BF=AE=E5=A4=8D=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E9=A1=B5=E5=85=B3=E9=97=AD=E6=97=B6=E9=A1=B5=E9=9D=A2=E5=8F=91?= =?UTF-8?q?=E8=B5=B7=E8=AF=B7=E6=B1=82=E5=AF=BC=E8=87=B4=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_base.py | 11 +++++++++-- DrissionPage/chromium_driver.py | 12 +++++++++--- setup.py | 2 +- 3 files changed, 19 insertions(+), 6 deletions(-) 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.",