修复Page关闭后new_tab()报错问题;微调Driver错误处理

This commit is contained in:
g1879 2023-12-28 17:23:47 +08:00
parent a75cb3a0b3
commit 63266cce76
5 changed files with 12 additions and 13 deletions

View File

@ -77,7 +77,7 @@ class Driver(object):
except (OSError, WebSocketConnectionClosedException):
self.method_results.pop(ws_id, None)
return {'error': {'message': 'page closed'}}
return {'error': {'message': 'page closed'}, 'type': 'page_closed'}
while not self._stopped.is_set():
try:
@ -87,15 +87,17 @@ class Driver(object):
except Empty:
if self.alert_flag and message['method'].startswith(('Input.', 'Runtime.')):
return {'error': {'message': 'alert exists.'}}
return {'error': {'message': 'alert exists.'}, 'type': 'alert_exists'}
if timeout is not None and perf_counter() > end_time:
self.method_results.pop(ws_id, None)
return {'error': {'message': 'alert exists.'}} \
if self.alert_flag else {'error': {'message': 'timeout'}}
return {'error': {'message': 'alert exists.'}, 'type': 'alert_exists'} \
if self.alert_flag else {'error': {'message': 'timeout'}, 'type': 'timeout'}
continue
return {'error': 'page closed', 'type': 'page_closed'}
def _recv_loop(self):
"""接收浏览器信息的守护线程方法"""
while not self._stopped.is_set():
@ -161,11 +163,9 @@ class Driver(object):
timeout = kwargs.pop('_timeout', 15)
result = self._send({'method': _method, 'params': kwargs}, timeout=timeout)
if result is None:
return {'error': {'message': 'page closed'}}
elif 'result' not in result and 'error' in result:
if 'result' not in result and 'error' in result:
return {'error': result['error']['message'], 'type': result.get('type', 'call_method_error'),
'method': _method, 'args': kwargs}
'method': _method, 'args': kwargs, 'timeout': timeout}
else:
return result['result']

View File

@ -1407,7 +1407,7 @@ def run_js(page_or_ele, script, as_expr=False, timeout=None, args=None):
arguments=[convert_argument(arg) for arg in args], returnByValue=False,
awaitPromise=True, userGesture=True, _timeout=timeout, _ignore=AlertExistsError)
except TimeoutError:
raise TimeoutError('执行js超时')
raise TimeoutError(f'执行js超时{timeout}秒)')
except ContextLostError:
if is_page:
raise ContextLostError('页面已被刷新,请尝试等待页面加载完成再执行操作。')

View File

@ -219,7 +219,7 @@ def wait_until(page, condition, timeout=10, poll=0.1, raise_err=True):
break
if raise_err:
raise TimeoutError('等待超时')
raise TimeoutError(f'等待超时{timeout}秒)')
else:
return False

View File

@ -438,8 +438,7 @@ class ChromiumBase(BasePage):
def _js_ready_state(self):
"""返回js获取的ready state信息"""
try:
return self.run_cdp('Runtime.evaluate', expression='document.readyState;',
_timeout=3)['result']['value']
return self.run_cdp('Runtime.evaluate', expression='document.readyState;', _timeout=3)['result']['value']
except ContextLostError:
return None
except TimeoutError:

View File

@ -207,7 +207,7 @@ class ChromiumPage(ChromiumBase):
if bid:
kwargs['browserContextId'] = bid
return self.run_cdp('Target.createTarget', **kwargs)['targetId']
return self.browser.run_cdp('Target.createTarget', **kwargs)['targetId']
def close_tabs(self, tabs_or_ids=None, others=False):
"""关闭传入的标签页,默认关闭当前页。可传入多个