diff --git a/DrissionPage/_base/driver.py b/DrissionPage/_base/driver.py index 2412b6f..d932805 100644 --- a/DrissionPage/_base/driver.py +++ b/DrissionPage/_base/driver.py @@ -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'] diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index 8eca461..2a0a4f0 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -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('页面已被刷新,请尝试等待页面加载完成再执行操作。') diff --git a/DrissionPage/_functions/tools.py b/DrissionPage/_functions/tools.py index 4db38b5..9df03db 100644 --- a/DrissionPage/_functions/tools.py +++ b/DrissionPage/_functions/tools.py @@ -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 diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index ea9a04d..5ec4c7f 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -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: diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index dd3ee7a..3f04953 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -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): """关闭传入的标签页,默认关闭当前页。可传入多个