From 6961fa07ff0fb8804909adceece7599b970aad8b Mon Sep 17 00:00:00 2001 From: g1879 Date: Fri, 11 Oct 2024 16:54:58 +0800 Subject: [PATCH] =?UTF-8?q?Page=E5=AF=B9=E8=B1=A1=E5=8A=A0=E4=B8=8A?= =?UTF-8?q?=E5=87=A0=E7=A7=8D=E6=B5=8F=E8=A7=88=E5=99=A8=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_base/chromium.py | 2 +- DrissionPage/_functions/tools.py | 4 ++-- DrissionPage/_functions/tools.pyi | 4 +++- DrissionPage/_pages/chromium_base.py | 6 +++--- DrissionPage/_units/states.py | 12 ++++++++++++ DrissionPage/_units/states.pyi | 15 +++++++++++++++ 6 files changed, 36 insertions(+), 7 deletions(-) diff --git a/DrissionPage/_base/chromium.py b/DrissionPage/_base/chromium.py index b741369..35217cd 100644 --- a/DrissionPage/_base/chromium.py +++ b/DrissionPage/_base/chromium.py @@ -373,7 +373,7 @@ class Chromium(object): def _run_cdp(self, cmd, **cmd_args): ignore = cmd_args.pop('_ignore', None) r = self._driver.run(cmd, **cmd_args) - return r if __ERROR__ not in r else raise_error(r, ignore) + return r if __ERROR__ not in r else raise_error(r, self, ignore) def _get_driver(self, tab_id, owner=None): d = self._drivers.pop(tab_id, None) diff --git a/DrissionPage/_functions/tools.py b/DrissionPage/_functions/tools.py index e99a62c..7c56840 100644 --- a/DrissionPage/_functions/tools.py +++ b/DrissionPage/_functions/tools.py @@ -157,7 +157,7 @@ def configs_to_here(save_name=None): om.save(save_name) -def raise_error(result, ignore=None, user=False): +def raise_error(result, browser, ignore=None, user=False): error = result['error'] if error in ('Cannot find context with specified id', 'Inspected target navigated or closed', 'No frame with given id found'): @@ -183,7 +183,7 @@ def raise_error(result, ignore=None, user=False): elif error == 'Given expression does not evaluate to a function': r = JavaScriptError(f'传入的js无法解析成函数:\n{result["args"]["functionDeclaration"]}') elif error.endswith("' wasn't found"): - r = RuntimeError(f'没有找到对应功能,方法错误或你的浏览器太旧。\n方法:{result["method"]}\n参数:{result["args"]}') + r = RuntimeError(f'没有找到对应功能,方法错误或你的浏览器太旧。\n浏览器版本:{browser.version}\n方法:{result["method"]}') elif result['type'] == 'timeout': from DrissionPage import __version__ txt = f'\n错误:{result["error"]}\n方法:{result["method"]}\n参数:{result["args"]}\n' \ diff --git a/DrissionPage/_functions/tools.pyi b/DrissionPage/_functions/tools.pyi index 2623bd8..a5d00e5 100644 --- a/DrissionPage/_functions/tools.pyi +++ b/DrissionPage/_functions/tools.pyi @@ -10,6 +10,7 @@ from pathlib import Path from threading import Lock from typing import Union, Tuple +from .._base.chromium import Chromium from .._pages.chromium_base import ChromiumBase @@ -98,9 +99,10 @@ def configs_to_here(save_name: Union[Path, str] = None) -> None: ... -def raise_error(result: dict, ignore=None, user: bool = False) -> None: +def raise_error(result: dict, browser: Chromium, ignore=None, user: bool = False) -> None: """抛出error对应报错 :param result: 包含error的dict + :param browser: 浏览器对象 :param ignore: 要忽略的错误 :param user: 是否用户调用的 :return: None diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 881a0bd..685caa1 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -370,17 +370,17 @@ class ChromiumBase(BasePage): def run_cdp(self, cmd, **cmd_args): r = self.driver.run(cmd, **cmd_args) - return r if __ERROR__ not in r else raise_error(r, user=True) + return r if __ERROR__ not in r else raise_error(r, self.browser, user=True) def run_cdp_loaded(self, cmd, **cmd_args): self.wait.doc_loaded() r = self.driver.run(cmd, **cmd_args) - return r if __ERROR__ not in r else raise_error(r, user=True) + return r if __ERROR__ not in r else raise_error(r, self.browser, user=True) def _run_cdp(self, cmd, **cmd_args): ignore = cmd_args.pop('_ignore', None) r = self.driver.run(cmd, **cmd_args) - return r if __ERROR__ not in r else raise_error(r, ignore) + return r if __ERROR__ not in r else raise_error(r, self.browser, ignore) def _run_cdp_loaded(self, cmd, **cmd_args): self.wait.doc_loaded() diff --git a/DrissionPage/_units/states.py b/DrissionPage/_units/states.py index 456fa40..1810a10 100644 --- a/DrissionPage/_units/states.py +++ b/DrissionPage/_units/states.py @@ -139,6 +139,18 @@ class PageStates(object): def has_alert(self): return self._owner._has_alert + @property + def is_headless(self): + return self._owner.browser.states.is_headless + + @property + def is_existed(self): + return self._owner.browser.states.is_existed + + @property + def is_incognito(self): + return self._owner.browser.states.is_incognito + class FrameStates(object): def __init__(self, frame): diff --git a/DrissionPage/_units/states.pyi b/DrissionPage/_units/states.pyi index f31463c..6aee4a7 100644 --- a/DrissionPage/_units/states.pyi +++ b/DrissionPage/_units/states.pyi @@ -153,6 +153,21 @@ class PageStates(object): """返回当前页面是否存在弹窗""" ... + @property + def is_headless(self) -> bool: + """返回浏览器是否无头模式""" + ... + + @property + def is_existed(self) -> bool: + """返回浏览器是否接管的""" + ... + + @property + def is_incognito(self) -> bool: + """返回浏览器是否无痕模式""" + ... + class FrameStates(object): _frame: ChromiumFrame = ...