Page对象加上几种浏览器状态

This commit is contained in:
g1879 2024-10-11 16:54:58 +08:00
parent 8d1d3b554d
commit 6961fa07ff
6 changed files with 36 additions and 7 deletions

View File

@ -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)

View File

@ -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' \

View File

@ -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

View File

@ -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()

View File

@ -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):

View File

@ -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 = ...