3.1.4修复标签页关闭时页面发起请求导致的问题

This commit is contained in:
g1879 2023-02-08 02:13:27 +08:00
parent 43d0a742d1
commit b03e919989
3 changed files with 19 additions and 6 deletions

View File

@ -93,7 +93,11 @@ class ChromiumBase(BasePage):
if self._debug_recorder: if self._debug_recorder:
self._debug_recorder.add_data((perf_counter(), '获取document', '开始')) self._debug_recorder.add_data((perf_counter(), '获取document', '开始'))
self._wait_loaded() try: # 处理标签页关闭的情况
self._wait_loaded()
except ConnectionError:
return
while True: while True:
try: try:
root_id = self._tab_obj.DOM.getDocument()['root']['nodeId'] root_id = self._tab_obj.DOM.getDocument()['root']['nodeId']
@ -251,7 +255,10 @@ class ChromiumBase(BasePage):
@property @property
def ready_state(self): def ready_state(self):
"""返回当前页面加载状态,'loading' 'interactive' 'complete'""" """返回当前页面加载状态,'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 @property
def size(self): def size(self):

View File

@ -146,8 +146,9 @@ class ChromiumDriver(object):
if event['method'] in self.event_handlers: if event['method'] in self.event_handlers:
try: try:
self.event_handlers[event['method']](**event['params']) self.event_handlers[event['method']](**event['params'])
except Exception as e: except Exception:
raise RuntimeError(f"回调函数 {event['method']} 错误:{e}") pass
# raise RuntimeError(f"回调函数 {event['method']} 错误:{e}")
self.event_queue.task_done() self.event_queue.task_done()
@ -169,10 +170,12 @@ class ChromiumDriver(object):
raise CallMethodException("参数必须是key=value形式。") raise CallMethodException("参数必须是key=value形式。")
if self._stopped.is_set(): if self._stopped.is_set():
raise RuntimeError("Driver已经停止。") return {'tab_closed': True}
timeout = kwargs.pop("_timeout", None) timeout = kwargs.pop("_timeout", None)
result = self._send({"method": _method, "params": kwargs}, timeout=timeout) 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: if 'result' not in result and 'error' in result:
raise CallMethodException(f"\n调用方法:{_method}\n参数:{kwargs}\n错误:{result['error']['message']}") raise CallMethodException(f"\n调用方法:{_method}\n参数:{kwargs}\n错误:{result['error']['message']}")
@ -204,6 +207,9 @@ class ChromiumDriver(object):
self._stopped.set() self._stopped.set()
if self._ws: if self._ws:
self._ws.close() self._ws.close()
self.event_handlers = {}
self.method_results = {}
self.event_queue = Queue()
return True return True
def set_listener(self, event, callback): def set_listener(self, event, callback):

View File

@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
setup( setup(
name="DrissionPage", name="DrissionPage",
version="3.1.3", version="3.1.4",
author="g1879", author="g1879",
author_email="g1879@qq.com", author_email="g1879@qq.com",
description="A module that integrates selenium and requests session, encapsulates common page operations.", description="A module that integrates selenium and requests session, encapsulates common page operations.",