From 6f5020f955f306f55c7c256e8cce15d746fb5a0c Mon Sep 17 00:00:00 2001 From: g1879 Date: Fri, 29 Dec 2023 19:20:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=9B=91=E5=90=AC=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E9=97=AE=E9=A2=98=EF=BC=9BPageClosedError=E6=94=B9?= =?UTF-8?q?=E4=B8=BAPageDisconnectedError?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_base/browser.py | 6 +++--- DrissionPage/_base/driver.py | 10 +++++----- DrissionPage/_functions/tools.py | 8 ++++---- DrissionPage/_pages/chromium_base.py | 4 ++-- DrissionPage/_pages/chromium_frame.py | 4 ++-- DrissionPage/_units/listener.py | 4 +--- DrissionPage/_units/states.py | 6 +++--- DrissionPage/errors.py | 4 ++-- 8 files changed, 22 insertions(+), 24 deletions(-) diff --git a/DrissionPage/_base/browser.py b/DrissionPage/_base/browser.py index 6556480..2d12911 100644 --- a/DrissionPage/_base/browser.py +++ b/DrissionPage/_base/browser.py @@ -8,7 +8,7 @@ from time import sleep, perf_counter from .driver import BrowserDriver, Driver from .._functions.tools import stop_process_on_port, raise_error from .._units.downloader import DownloadManager -from ..errors import PageClosedError +from ..errors import PageDisconnectedError __ERROR__ = 'error' @@ -143,7 +143,7 @@ class Browser(object): :param tab_id: 标签页id :return: None """ - self.run_cdp('Target.closeTarget', targetId=tab_id, _ignore=PageClosedError) + self.run_cdp('Target.closeTarget', targetId=tab_id, _ignore=PageDisconnectedError) def activate_tab(self, tab_id): """使标签页变为活动状态 @@ -168,7 +168,7 @@ class Browser(object): try: self.run_cdp('Browser.close') self.driver.stop() - except PageClosedError: + except PageDisconnectedError: self.driver.stop() return diff --git a/DrissionPage/_base/driver.py b/DrissionPage/_base/driver.py index d932805..0b63f80 100644 --- a/DrissionPage/_base/driver.py +++ b/DrissionPage/_base/driver.py @@ -12,7 +12,7 @@ from requests import get from websocket import (WebSocketTimeoutException, WebSocketConnectionClosedException, create_connection, WebSocketException) -from ..errors import PageClosedError +from ..errors import PageDisconnectedError class Driver(object): @@ -77,7 +77,7 @@ class Driver(object): except (OSError, WebSocketConnectionClosedException): self.method_results.pop(ws_id, None) - return {'error': {'message': 'page closed'}, 'type': 'page_closed'} + return {'error': {'message': 'connection disconnected'}, 'type': 'connection_error'} while not self._stopped.is_set(): try: @@ -96,7 +96,7 @@ class Driver(object): continue - return {'error': 'page closed', 'type': 'page_closed'} + return {'error': {'message': 'connection disconnected'}, 'type': 'connection_error'} def _recv_loop(self): """接收浏览器信息的守护线程方法""" @@ -159,7 +159,7 @@ class Driver(object): :return: 执行结果 """ if self._stopped.is_set(): - return {'error': 'page closed', 'type': 'page_closed'} + return {'error': 'connection disconnected', 'type': 'connection_error'} timeout = kwargs.pop('_timeout', 15) result = self._send({'method': _method, 'params': kwargs}, timeout=timeout) @@ -258,5 +258,5 @@ class BrowserDriver(Driver): def run_function(function, kwargs): try: function(**kwargs) - except PageClosedError: + except PageDisconnectedError: pass diff --git a/DrissionPage/_functions/tools.py b/DrissionPage/_functions/tools.py index 9df03db..edfc8ec 100644 --- a/DrissionPage/_functions/tools.py +++ b/DrissionPage/_functions/tools.py @@ -12,8 +12,8 @@ from time import perf_counter, sleep from psutil import process_iter, AccessDenied, NoSuchProcess, ZombieProcess from .._configs.options_manage import OptionsManager -from ..errors import (ContextLostError, ElementLostError, CDPError, PageClosedError, NoRectError, AlertExistsError, - WrongURLError, StorageError, CookieFormatError) +from ..errors import (ContextLostError, ElementLostError, CDPError, PageDisconnectedError, NoRectError, + AlertExistsError, WrongURLError, StorageError, CookieFormatError) def get_usable_path(path, is_file=True, parents=True): @@ -267,8 +267,8 @@ def raise_error(result, ignore=None): 'No node with given id found', 'Node with given id does not belong to the document', 'No node found for given backend id'): r = ElementLostError() - elif error in ('page closed', 'No target with given id found'): - r = PageClosedError() + elif error in ('connection disconnected', 'No target with given id found'): + r = PageDisconnectedError() elif error == 'timeout': r = TimeoutError(f'超时。\n错误:{result["error"]}\nmethod:{result["method"]}\nargs:{result["args"]}\n' f'出现这个错误可能意味着程序有bug,请把错误信息和重现方法告知作者,谢谢。\n' diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 5ec4c7f..9ca83ed 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -27,7 +27,7 @@ from .._units.scroller import PageScroller from .._units.setter import ChromiumBaseSetter from .._units.states import PageStates from .._units.waiter import BaseWaiter -from ..errors import ContextLostError, CDPError, PageClosedError, ElementNotFoundError +from ..errors import ContextLostError, CDPError, PageDisconnectedError, ElementNotFoundError __ERROR__ = 'error' @@ -672,7 +672,7 @@ class ChromiumBase(BasePage): print('停止页面加载') try: self.run_cdp('Page.stopLoading') - except (PageClosedError, CDPError): + except (PageDisconnectedError, CDPError): pass end_time = perf_counter() + self.timeouts.page_load while self._ready_state != 'complete' and perf_counter() < end_time: diff --git a/DrissionPage/_pages/chromium_frame.py b/DrissionPage/_pages/chromium_frame.py index 44bc702..9abe951 100644 --- a/DrissionPage/_pages/chromium_frame.py +++ b/DrissionPage/_pages/chromium_frame.py @@ -15,7 +15,7 @@ from .._units.scroller import FrameScroller from .._units.setter import ChromiumFrameSetter from .._units.states import FrameStates from .._units.waiter import FrameWaiter -from ..errors import ContextLostError, ElementLostError, PageClosedError, JavaScriptError +from ..errors import ContextLostError, ElementLostError, PageDisconnectedError, JavaScriptError class ChromiumFrame(ChromiumBase): @@ -119,7 +119,7 @@ class ChromiumFrame(ChromiumBase): else: return - except (ElementLostError, PageClosedError): + except (ElementLostError, PageDisconnectedError): return if self._is_inner_frame(): diff --git a/DrissionPage/_units/listener.py b/DrissionPage/_units/listener.py index d576480..d18759f 100644 --- a/DrissionPage/_units/listener.py +++ b/DrissionPage/_units/listener.py @@ -295,9 +295,7 @@ class Listener(object): packet = self._request_ids.get(rid) if packet: r = self._driver.run('Network.getResponseBody', requestId=rid) - if 'error' in r: - return - elif 'body' in r: + if 'body' in r: packet._raw_body = r['body'] packet._base64_body = r['base64Encoded'] else: diff --git a/DrissionPage/_units/states.py b/DrissionPage/_units/states.py index 1ae731f..2abbcf5 100644 --- a/DrissionPage/_units/states.py +++ b/DrissionPage/_units/states.py @@ -4,7 +4,7 @@ @Contact : g1879@qq.com """ from .._functions.web import location_in_viewport -from ..errors import CDPError, NoRectError, PageClosedError, ElementLostError +from ..errors import CDPError, NoRectError, PageDisconnectedError, ElementLostError class ElementStates(object): @@ -120,7 +120,7 @@ class PageStates(object): try: self._page.run_cdp('Page.getLayoutMetrics') return True - except PageClosedError: + except PageDisconnectedError: return False @property @@ -152,7 +152,7 @@ class FrameStates(object): try: node = self._frame._target_page.run_cdp('DOM.describeNode', backendNodeId=self._frame._frame_ele._backend_id)['node'] - except (ElementLostError, PageClosedError): + except (ElementLostError, PageDisconnectedError): return False return 'frameId' in node diff --git a/DrissionPage/errors.py b/DrissionPage/errors.py index 5e8677a..155453e 100644 --- a/DrissionPage/errors.py +++ b/DrissionPage/errors.py @@ -45,8 +45,8 @@ class CDPError(BaseError): _info = '方法调用错误。' -class PageClosedError(BaseError): - _info = '页面已关闭。' +class PageDisconnectedError(BaseError): + _info = '与页面的连接已断开。' class JavaScriptError(BaseError):