diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index f9e7d2c..03d5940 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -33,7 +33,7 @@ class ChromiumBase(BasePage): :param timeout: 超时时间 """ self._is_loading = None - self._root_id = None + self._root_id = None # object id self._debug = False self._debug_recorder = None self._tab_obj = None @@ -572,6 +572,26 @@ class ChromiumBase(BasePage): if ele: self.run_cdp('DOM.removeNode', nodeId=ele.ids.node_id) + def get_frame(self, loc_ind_ele): + """获取页面中一个frame对象,可传入定位符、iframe序号、ChromiumFrame对象,序号从1开始 + :param loc_ind_ele: 定位符、iframe序号、ChromiumFrame对象 + :return: ChromiumFrame对象 + """ + if isinstance(loc_ind_ele, (str, tuple)): + ele = self._ele(loc_ind_ele) + if ele and not str(type(ele)).endswith(".ChromiumFrame'>"): + raise RuntimeError('该定位符不是指向frame元素。') + return ele + elif isinstance(loc_ind_ele, int): + if loc_ind_ele < 1: + raise ValueError('序号必须大于0。') + xpath = f'x:(//*[name()="frame" or name()="iframe"])[{loc_ind_ele}]' + return self._ele(xpath) + elif str(type(loc_ind_ele)).endswith(".ChromiumFrame'>"): + return loc_ind_ele + else: + raise TypeError('必须传入定位符、iframe序号、ChromiumFrame对象其中之一。') + def get_session_storage(self, item=None): """获取sessionStorage信息,不设置item则获取全部 :param item: 要获取的项,不设置则返回全部 diff --git a/DrissionPage/chromium_base.pyi b/DrissionPage/chromium_base.pyi index 44cb06a..7d3fc11 100644 --- a/DrissionPage/chromium_base.pyi +++ b/DrissionPage/chromium_base.pyi @@ -150,8 +150,8 @@ class ChromiumBase(BasePage): def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[Union[SessionElement, str]]: ... def _find_elements(self, - loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame], - timeout: float = None, single: bool = True, relative: bool = False, raise_err:bool=None) \ + loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame], + timeout: float = None, single: bool = True, relative: bool = False, raise_err: bool = None) \ -> Union[ChromiumElement, ChromiumFrame, NoneElement, List[Union[ChromiumElement, ChromiumFrame]]]: ... def refresh(self, ignore_cache: bool = False) -> None: ... @@ -166,6 +166,8 @@ class ChromiumBase(BasePage): def remove_ele(self, loc_or_ele: Union[ChromiumElement, ChromiumFrame, str, Tuple[str, str]]) -> None: ... + def get_frame(self, loc_ind_ele: Union[str, int, ChromiumFrame]) -> ChromiumFrame: ... + def run_cdp(self, cmd: str, **cmd_args) -> dict: ... def run_cdp_loaded(self, cmd: str, **cmd_args) -> dict: ...