From f0dfe6ff3ca5fcc2ce262f7922ef739f14146bab Mon Sep 17 00:00:00 2001 From: g1879 Date: Tue, 27 Jun 2023 18:00:35 +0800 Subject: [PATCH] =?UTF-8?q?parent()=E5=A2=9E=E5=8A=A0index=E5=8F=82?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E5=8F=AF=E6=8C=89=E5=BA=8F=E5=8F=B7=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=AE=9A=E4=BD=8D=E7=AC=A6=E8=8E=B7=E5=8F=96=E5=88=B0?= =?UTF-8?q?=E7=9A=84=E7=88=B6=E5=85=83=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/base.py | 5 +++-- DrissionPage/base.pyi | 2 +- DrissionPage/chromium_element.py | 10 ++++++---- DrissionPage/chromium_element.pyi | 4 ++-- DrissionPage/session_element.py | 5 +++-- DrissionPage/session_element.pyi | 2 +- 6 files changed, 16 insertions(+), 12 deletions(-) diff --git a/DrissionPage/base.py b/DrissionPage/base.py index cd3ae8d..21692cb 100644 --- a/DrissionPage/base.py +++ b/DrissionPage/base.py @@ -122,9 +122,10 @@ class DrissionElement(BaseElement): return [format_html(x.strip(' ').rstrip('\n')) for x in texts if x and sub('[\r\n\t ]', '', x) != ''] - def parent(self, level_or_loc=1): + def parent(self, level_or_loc=1, index=1): """返回上面某一级父元素,可指定层数或用查询语法定位 :param level_or_loc: 第几级父元素,或定位符 + :param index: 当level_or_loc传入定位符,使用此参数选择第几个结果 :return: 上级元素对象 """ if isinstance(level_or_loc, int): @@ -136,7 +137,7 @@ class DrissionElement(BaseElement): if loc[0] == 'css selector': raise ValueError('此css selector语法不受支持,请换成xpath。') - loc = f'xpath:./ancestor::{loc[1].lstrip(". / ")}' + loc = f'xpath:./ancestor::{loc[1].lstrip(". / ")}[{index}]' else: raise TypeError('level_or_loc参数只能是tuple、int或str。') diff --git a/DrissionPage/base.pyi b/DrissionPage/base.pyi index 2b94665..3d9ec12 100644 --- a/DrissionPage/base.pyi +++ b/DrissionPage/base.pyi @@ -80,7 +80,7 @@ class DrissionElement(BaseElement): def texts(self, text_node_only: bool = False) -> list: ... - def parent(self, level_or_loc: Union[tuple, str, int] = 1) -> Union[DrissionElement, None]: ... + def parent(self, level_or_loc: Union[tuple, str, int] = 1, index: int = 1) -> Union[DrissionElement, None]: ... def child(self, index: int = 1, filter_loc: Union[tuple, str] = '', diff --git a/DrissionPage/chromium_element.py b/DrissionPage/chromium_element.py index 3be5f88..bf75627 100644 --- a/DrissionPage/chromium_element.py +++ b/DrissionPage/chromium_element.py @@ -203,12 +203,13 @@ class ChromiumElement(DrissionElement): return self._select - def parent(self, level_or_loc=1): + def parent(self, level_or_loc=1, index=1): """返回上面某一级父元素,可指定层数或用查询语法定位 :param level_or_loc: 第几级父元素,或定位符 + :param index: 当level_or_loc传入定位符,使用此参数选择第几个结果 :return: 上级元素对象 """ - return super().parent(level_or_loc) + return super().parent(level_or_loc, index) def child(self, filter_loc='', index=1, timeout=0, ele_only=True): """返回当前元素的一个符合条件的直接子元素,可用查询语法筛选,可指定返回筛选结果的第几个 @@ -808,9 +809,10 @@ class ChromiumShadowRoot(BaseElement): from threading import Thread Thread(target=run_js, args=(self, script, as_expr, self.page.timeouts.script, args)).start() - def parent(self, level_or_loc=1): + def parent(self, level_or_loc=1, index=1): """返回上面某一级父元素,可指定层数或用查询语法定位 :param level_or_loc: 第几级父元素,或定位符 + :param index: 当level_or_loc传入定位符,使用此参数选择第几个结果 :return: ChromiumElement对象 """ if isinstance(level_or_loc, int): @@ -822,7 +824,7 @@ class ChromiumShadowRoot(BaseElement): if loc[0] == 'css selector': raise ValueError('此css selector语法不受支持,请换成xpath。') - loc = f'xpath:./ancestor-or-self::{loc[1].lstrip(". / ")}' + loc = f'xpath:./ancestor-or-self::{loc[1].lstrip(". / ")}[{index}]' else: raise TypeError('level_or_loc参数只能是tuple、int或str。') diff --git a/DrissionPage/chromium_element.pyi b/DrissionPage/chromium_element.pyi index 68b83a5..c074c20 100644 --- a/DrissionPage/chromium_element.pyi +++ b/DrissionPage/chromium_element.pyi @@ -94,7 +94,7 @@ class ChromiumElement(DrissionElement): @property def click(self) -> Click: ... - def parent(self, level_or_loc: Union[tuple, str, int] = 1) -> Union[ChromiumElement, None]: ... + def parent(self, level_or_loc: Union[tuple, str, int] = 1, index: int = 1) -> Union[ChromiumElement, None]: ... def child(self, filter_loc: Union[tuple, str] = '', index: int = 1, @@ -273,7 +273,7 @@ class ChromiumShadowRoot(BaseElement): def run_async_js(self, script: str, *args: Any, as_expr: bool = False) -> None: ... - def parent(self, level_or_loc: Union[str, int] = 1) -> ChromiumElement: ... + def parent(self, level_or_loc: Union[str, int] = 1, index: int = 1) -> ChromiumElement: ... def child(self, filter_loc: Union[tuple, str] = '', index: int = 1) -> Union[ChromiumElement, str, None]: ... diff --git a/DrissionPage/session_element.py b/DrissionPage/session_element.py index c8a6f46..5142222 100644 --- a/DrissionPage/session_element.py +++ b/DrissionPage/session_element.py @@ -75,12 +75,13 @@ class SessionElement(DrissionElement): """返回未格式化处理的元素内文本""" return str(self._inner_ele.text_content()) - def parent(self, level_or_loc=1): + def parent(self, level_or_loc=1, index=1): """返回上面某一级父元素,可指定层数或用查询语法定位 :param level_or_loc: 第几级父元素,或定位符 + :param index: 当level_or_loc传入定位符,使用此参数选择第几个结果 :return: 上级元素对象 """ - return super().parent(level_or_loc) + return super().parent(level_or_loc, index) def child(self, filter_loc='', index=1, timeout=None, ele_only=True): """返回当前元素的一个符合条件的直接子元素,可用查询语法筛选,可指定返回筛选结果的第几个 diff --git a/DrissionPage/session_element.pyi b/DrissionPage/session_element.pyi index eda02e0..2959c57 100644 --- a/DrissionPage/session_element.pyi +++ b/DrissionPage/session_element.pyi @@ -48,7 +48,7 @@ class SessionElement(DrissionElement): @property def raw_text(self) -> str: ... - def parent(self, level_or_loc: Union[tuple, str, int] = 1) -> Union['SessionElement', None]: ... + def parent(self, level_or_loc: Union[tuple, str, int] = 1, index: int = 1) -> Union['SessionElement', None]: ... def child(self, filter_loc: Union[tuple, str] = '', index: int = 1,