From 12fbe157617b0451055708f9eec802c013c6eb84 Mon Sep 17 00:00:00 2001 From: g1879 Date: Tue, 21 Dec 2021 22:29:51 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E6=94=B9=E8=BF=9B=E7=9B=B8?= =?UTF-8?q?=E5=AF=B9=E5=AE=9A=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/base.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/DrissionPage/base.py b/DrissionPage/base.py index c4056ed..7bf0ac9 100644 --- a/DrissionPage/base.py +++ b/DrissionPage/base.py @@ -173,30 +173,34 @@ class DrissionElement(BaseElement): def _get_brothers(self, index: int = None, filter_loc: Union[tuple, str] = '', - direction: str = 'following') -> List['DrissionElement']: - """按要求返回兄弟元素或节点组成的列表 \n + direction: str = 'following', + brother: bool = True, + timeout: float = .5) -> List['DrissionElement']: + """按要求返回兄弟元素或节点组成的列表 \n :param index: 获取第几个 :param filter_loc: 用于筛选元素的查询语法 :param direction: 'following' 或 'preceding',查找的方向 + :param brother: 查找范围,在同级查找还是整个dom前后查找 + :param timeout: 查找等待时间 :return: DriverElement对象或字符串 """ if index is not None and index < 1: raise ValueError('index必须大于等于1。') - timeout = 0 if direction == 'preceding' else .5 + brother = '-sibling' if brother else '' # 仅根据位置取一个 if index and not filter_loc: - xpath = f'xpath:./{direction}-sibling::*[{index}]' + xpath = f'xpath:./{direction}{brother}::*[{index}]' # 根据筛选获取所有 else: - loc = get_loc(filter_loc, True) + loc = get_loc(filter_loc, True) # 把定位符转换为xpath if loc[0] == 'css selector': raise ValueError('此css selector语法不受支持,请换成xpath。') loc = loc[1].lstrip('./') - xpath = f'xpath:./{direction}-sibling::{loc}' + xpath = f'xpath:./{direction}{brother}::{loc}' nodes = self._ele(xpath, timeout=timeout, single=False) nodes = [e for e in nodes if not (isinstance(e, str) and sub('[ \n\t\r]', '', e) == '')]