diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index 4ca59dc..1e4cb09 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -19,8 +19,8 @@ from .commons.constants import HANDLE_ALERT_METHOD, ERROR, NoneElement from .commons.locator import get_loc from .commons.tools import get_usable_path, clean_folder from .commons.web import set_browser_cookies -from .errors import ContextLossError, ElementLossError, AlertExistsError, CallMethodError, TabClosedError, \ - NoRectError, BrowserConnectError +from .errors import ContextLossError, ElementLossError, AlertExistsError, CDPError, TabClosedError, \ + NoRectError, BrowserConnectError, GetDocumentError from .network_listener import NetworkListener from .session_element import make_session_ele @@ -132,7 +132,8 @@ class ChromiumBase(BasePage): self._debug_recorder.add_data((perf_counter(), '信息', f'root_id:{self._root_id}')) break - except Exception: + except CDPError as e: + err = e if self._debug: print('重试获取document') if self._debug_recorder: @@ -141,7 +142,9 @@ class ChromiumBase(BasePage): sleep(.1) else: - raise RuntimeError('获取document失败。') + txt = f'请检查是否创建了过多页面对象同时操作浏览器。\n如无法解决,请把以下信息报告作者。\n{err._info}\n' \ + f'报告网址:https://gitee.com/g1879/DrissionPage/issues' + raise GetDocumentError(txt) if self._debug: print('获取document结束') @@ -399,7 +402,7 @@ class ChromiumBase(BasePage): elif error in ('Node does not have a layout object', 'Could not compute box model.'): raise NoRectError elif r['type'] == 'call_method_error': - raise CallMethodError(f'\n错误:{r["error"]}\nmethod:{r["method"]}\nargs:{r["args"]}') + raise CDPError(f'\n错误:{r["error"]}\nmethod:{r["method"]}\nargs:{r["args"]}') else: raise RuntimeError(r) diff --git a/DrissionPage/chromium_driver.py b/DrissionPage/chromium_driver.py index 24dd416..9d053f3 100644 --- a/DrissionPage/chromium_driver.py +++ b/DrissionPage/chromium_driver.py @@ -11,7 +11,7 @@ from threading import Thread, Event from websocket import WebSocketTimeoutException, WebSocketException, WebSocketConnectionClosedException, \ create_connection -from .errors import CallMethodError +from .errors import CDPError class GenericAttr(object): @@ -183,7 +183,7 @@ class ChromiumDriver(object): self.start() # raise RuntimeError("不能在启动前调用方法。") if args: - raise CallMethodError("参数必须是key=value形式。") + raise CDPError("参数必须是key=value形式。") if self._stopped.is_set(): return {'error': 'tab closed', 'type': 'tab_closed'} diff --git a/DrissionPage/chromium_element.py b/DrissionPage/chromium_element.py index acf78e3..734477f 100644 --- a/DrissionPage/chromium_element.py +++ b/DrissionPage/chromium_element.py @@ -14,7 +14,7 @@ from .commons.keys import keys_to_typing, keyDescriptionForString, keyDefinition from .commons.locator import get_loc from .commons.web import make_absolute_link, get_ele_txt, format_html, is_js_func, location_in_viewport, offset_scroll from .errors import ContextLossError, ElementLossError, JavaScriptError, NoRectError, ElementNotFoundError, \ - CallMethodError, NoResourceError, CanNotClickError + CDPError, NoResourceError, CanNotClickError from .session_element import make_session_ele @@ -99,7 +99,7 @@ class ChromiumElement(DrissionElement): try: attrs = self.page.run_cdp('DOM.getAttributes', nodeId=self._node_id)['attributes'] return {attrs[i]: attrs[i + 1] for i in range(0, len(attrs), 2)} - except CallMethodError: # 文档根元素不能调用此方法 + except CDPError: # 文档根元素不能调用此方法 return {} @property @@ -464,7 +464,7 @@ class ChromiumElement(DrissionElement): try: result = self.page.run_cdp('Page.getResourceContent', frameId=frame, url=src) break - except CallMethodError: + except CDPError: sleep(.1) if not result: @@ -1424,7 +1424,7 @@ class ChromiumElementStates(object): lx, ly = self._ele.locations.click_point try: r = self._ele.page.run_cdp('DOM.getNodeForLocation', x=lx, y=ly) - except CallMethodError: + except CDPError: return False if r.get('backendNodeId') != self._ele.ids.backend_id: diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index 60a40c6..0f41258 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -18,7 +18,7 @@ from .commons.browser import connect_browser from .commons.tools import port_is_using, get_usable_path from .commons.web import set_session_cookies from .configs.chromium_options import ChromiumOptions -from .errors import CallMethodError, BrowserConnectError +from .errors import CDPError, BrowserConnectError from .session_page import DownloadSetter @@ -106,7 +106,7 @@ class ChromiumPage(ChromiumBase): self._main_tab = self.tab_id try: self.download_set.by_browser() - except CallMethodError: + except CDPError: pass self._process_id = None @@ -536,7 +536,7 @@ class ChromiumDownloadSetter(DownloadSetter): try: self._page.browser_driver.Browser.setDownloadBehavior(behavior='allowAndName', downloadPath=path, eventsEnabled=True) - except CallMethodError: + except CDPError: warn('\n您的浏览器版本太低,用新标签页下载文件可能崩溃,建议升级。') self._page.run_cdp('Page.setDownloadBehavior', behavior='allowAndName', downloadPath=path) @@ -549,7 +549,7 @@ class ChromiumDownloadSetter(DownloadSetter): downloadPath=self._page.download_path) self._page.browser_driver.Browser.downloadWillBegin = self._download_will_begin self._page.browser_driver.Browser.downloadProgress = self._download_progress - except CallMethodError: + except CDPError: self._page.driver.Page.setDownloadBehavior(behavior='allowAndName', downloadPath=self._page.download_path) self._page.driver.Page.downloadWillBegin = self._download_will_begin self._page.driver.Page.downloadProgress = self._download_progress @@ -562,7 +562,7 @@ class ChromiumDownloadSetter(DownloadSetter): self._page.browser_driver.Browser.setDownloadBehavior(behavior='deny', eventsEnabled=True) self._page.browser_driver.Browser.downloadWillBegin = self._download_by_DownloadKit # self._page.browser_driver.Browser.downloadProgress = None - except CallMethodError: + except CDPError: raise RuntimeError('您的浏览器版本太低,不支持此方法,请升级。') self._behavior = 'deny' diff --git a/DrissionPage/errors.py b/DrissionPage/errors.py index 3bcfca0..4eabfa6 100644 --- a/DrissionPage/errors.py +++ b/DrissionPage/errors.py @@ -24,7 +24,7 @@ class ElementLossError(BaseError): _info = '元素对象因刷新已失效。' -class CallMethodError(BaseError): +class CDPError(BaseError): _info = '方法调用错误。' @@ -54,3 +54,7 @@ class NoResourceError(BaseError): class CanNotClickError(BaseError): _info = '该元素无法滚动到视口或被遮挡,无法点击。' + + +class GetDocumentError(BaseError): + _info = '获取文档失败。' diff --git a/DrissionPage/network_listener.py b/DrissionPage/network_listener.py index 2e0d7ff..ccbf43e 100644 --- a/DrissionPage/network_listener.py +++ b/DrissionPage/network_listener.py @@ -8,7 +8,7 @@ from time import perf_counter, sleep from requests.structures import CaseInsensitiveDict -from .errors import CallMethodError +from .errors import CDPError class NetworkListener(object): @@ -182,7 +182,7 @@ class NetworkListener(object): r = self._page.run_cdp('Network.getResponseBody', requestId=request_id) body = r['body'] is_base64 = r['base64Encoded'] - except CallMethodError: + except CDPError: body = '' is_base64 = False diff --git a/DrissionPage/web_page.py b/DrissionPage/web_page.py index 2315ea7..f84dd19 100644 --- a/DrissionPage/web_page.py +++ b/DrissionPage/web_page.py @@ -16,7 +16,7 @@ from .chromium_tab import WebPageTab from .commons.web import set_session_cookies, set_browser_cookies from .configs.chromium_options import ChromiumOptions from .configs.session_options import SessionOptions -from .errors import CallMethodError +from .errors import CDPError from .session_page import SessionPage, SessionPageSetter @@ -526,7 +526,7 @@ class WebPageDownloadSetter(ChromiumDownloadSetter): try: self._page.browser_driver.Browser.setDownloadBehavior(behavior=self._behavior, downloadPath=path, eventsEnabled=True) - except CallMethodError: + except CDPError: warn('\n您的浏览器版本太低,用新标签页下载文件可能崩溃,建议升级。') self._page.run_cdp('Page.setDownloadBehavior', behavior=self._behavior, downloadPath=path) @@ -541,7 +541,7 @@ class WebPageDownloadSetter(ChromiumDownloadSetter): self._page.browser_driver.Browser.downloadWillBegin = self._download_will_begin self._page.browser_driver.Browser.downloadProgress = self._download_progress - except CallMethodError: + except CDPError: warn('\n您的浏览器版本太低,用新标签页下载文件可能崩溃,建议升级。') self._page.driver.Page.setDownloadBehavior(behavior='allowAndName', downloadPath=self._page.download_path) self._page.driver.Page.downloadWillBegin = self._download_will_begin @@ -556,7 +556,7 @@ class WebPageDownloadSetter(ChromiumDownloadSetter): self._page.browser_driver.Browser.setDownloadBehavior(behavior='deny', eventsEnabled=True) self._page.browser_driver.Browser.downloadWillBegin = self._download_by_DownloadKit # self._page.browser_driver.Browser.downloadProgress = None - except CallMethodError: + except CDPError: raise RuntimeError('您的浏览器版本太低,不支持此方法,请升级。') self._behavior = 'deny'