From 9e4b39be55da27364eef726ffd5d9434bba247d3 Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 22 Jan 2024 17:13:10 +0800 Subject: [PATCH] =?UTF-8?q?4.0.3.5=E7=BB=9F=E4=B8=80=E5=90=84locator?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=90=8D=E7=A7=B0=EF=BC=9Bselect.by=5Floc()?= =?UTF-8?q?=E6=94=B9=E6=88=90by.locator()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/__init__.py | 2 +- DrissionPage/_base/base.py | 149 +++++++------ DrissionPage/_base/base.pyi | 47 +++-- DrissionPage/_elements/chromium_element.py | 222 ++++++++++---------- DrissionPage/_elements/chromium_element.pyi | 65 +++--- DrissionPage/_elements/session_element.py | 96 ++++----- DrissionPage/_elements/session_element.pyi | 32 +-- DrissionPage/_pages/chromium_base.py | 56 ++--- DrissionPage/_pages/chromium_base.pyi | 14 +- DrissionPage/_pages/chromium_frame.py | 66 +++--- DrissionPage/_pages/chromium_frame.pyi | 20 +- DrissionPage/_pages/chromium_tab.py | 48 ++--- DrissionPage/_pages/chromium_tab.pyi | 12 +- DrissionPage/_pages/session_page.py | 37 ++-- DrissionPage/_pages/session_page.pyi | 12 +- DrissionPage/_pages/web_page.py | 48 ++--- DrissionPage/_pages/web_page.pyi | 12 +- DrissionPage/_units/selector.py | 12 +- DrissionPage/_units/selector.pyi | 4 +- DrissionPage/_units/waiter.py | 6 +- DrissionPage/_units/waiter.pyi | 2 +- 21 files changed, 483 insertions(+), 479 deletions(-) diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index a038814..b64e503 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -14,4 +14,4 @@ from ._configs.chromium_options import ChromiumOptions from ._configs.session_options import SessionOptions __all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__'] -__version__ = '4.0.3.4' +__version__ = '4.0.3.5' diff --git a/DrissionPage/_base/base.py b/DrissionPage/_base/base.py index 9ecf2b1..0156fbd 100644 --- a/DrissionPage/_base/base.py +++ b/DrissionPage/_base/base.py @@ -20,31 +20,31 @@ from ..errors import ElementNotFoundError class BaseParser(object): """所有页面、元素类的基类""" - def __call__(self, loc_or_str): - return self.ele(loc_or_str) + def __call__(self, locator): + return self.ele(locator) - def ele(self, loc_or_ele, index=1, timeout=None): - return self._ele(loc_or_ele, timeout, index=index, method='ele()') + def ele(self, locator, index=1, timeout=None): + return self._ele(locator, timeout, index=index, method='ele()') - def eles(self, loc_or_str, timeout=None): - return self._ele(loc_or_str, timeout, index=None) + def eles(self, locator, timeout=None): + return self._ele(locator, timeout, index=None) # ----------------以下属性或方法待后代实现---------------- @property def html(self): return '' - def s_ele(self, loc_or_ele): + def s_ele(self, locator): pass - def s_eles(self, loc_or_str): + def s_eles(self, locator): pass - def _ele(self, loc_or_ele, timeout=None, index=1, raise_err=None, method=None): + def _ele(self, locator, timeout=None, index=1, raise_err=None, method=None): pass @abstractmethod - def _find_elements(self, loc_or_ele, timeout=None, index=1, raise_err=None): + def _find_elements(self, locator, timeout=None, index=1, raise_err=None): pass @@ -69,9 +69,9 @@ class BaseElement(BaseParser): def nexts(self): pass - def _ele(self, loc_or_str, timeout=None, index=1, relative=False, raise_err=None, method=None): + def _ele(self, locator, timeout=None, index=1, relative=False, raise_err=None, method=None): """调用获取元素的方法 - :param loc_or_str: 定位符 + :param locator: 定位符 :param timeout: 超时时间(秒) :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :param relative: 是否相对定位 @@ -79,24 +79,23 @@ class BaseElement(BaseParser): :param method: 调用的方法名 :return: 元素对象或它们组成的列表 """ - r = self._find_elements(loc_or_str, timeout=timeout, index=index, relative=relative, raise_err=raise_err) + r = self._find_elements(locator, timeout=timeout, index=index, relative=relative, raise_err=raise_err) if r or isinstance(r, list): return r if Settings.raise_when_ele_not_found or raise_err is True: - raise ElementNotFoundError(None, method, {'loc_or_str': loc_or_str, 'index': index}) + raise ElementNotFoundError(None, method, {'locator': locator, 'index': index}) r.method = method - r.args = {'loc_or_str': loc_or_str, 'index': index} + r.args = {'locator': locator, 'index': index} return r @abstractmethod - def _find_elements(self, loc_or_str, timeout=None, index=1, relative=False, raise_err=None): + def _find_elements(self, locator, timeout=None, index=1, relative=False, raise_err=None): pass class DrissionElement(BaseElement): - """ChromiumElement 和 SessionElement的基类 - 但不是ShadowRoot的基类""" + """ChromiumElement 和 SessionElement的基类,但不是ShadowRoot的基类""" @property def link(self): @@ -152,21 +151,21 @@ class DrissionElement(BaseElement): return self._ele(loc, timeout=0, relative=True, raise_err=False, method='parent()') - def child(self, filter_loc='', index=1, timeout=None, ele_only=True): + def child(self, locator='', index=1, timeout=None, ele_only=True): """返回直接子元素元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 第几个查询结果,1开始 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 直接子元素或节点文本组成的列表 """ - if isinstance(filter_loc, int): - index = filter_loc - filter_loc = '' - if not filter_loc: + if isinstance(locator, int): + index = locator + locator = '' + if not locator: loc = '*' if ele_only else 'node()' else: - loc = get_loc(filter_loc, True) # 把定位符转换为xpath + loc = get_loc(locator, True) # 把定位符转换为xpath if loc[0] == 'css selector': raise ValueError('此css selector语法不受支持,请换成xpath。') loc = loc[1].lstrip('./') @@ -176,62 +175,62 @@ class DrissionElement(BaseElement): return node if Settings.raise_when_ele_not_found: - raise ElementNotFoundError(None, 'child()', {'filter_loc': filter_loc, 'index': index, + raise ElementNotFoundError(None, 'child()', {'locator': locator, 'index': index, 'ele_only': ele_only}) else: - return NoneElement(self.page, 'child()', {'filter_loc': filter_loc, 'index': index, 'ele_only': ele_only}) + return NoneElement(self.page, 'child()', {'locator': locator, 'index': index, 'ele_only': ele_only}) - def prev(self, filter_loc='', index=1, timeout=None, ele_only=True): + def prev(self, locator='', index=1, timeout=None, ele_only=True): """返回前面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 前面第几个查询结果,1开始 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 兄弟元素 """ - return self._get_relative('prev()', 'preceding', True, filter_loc, index, timeout, ele_only) + return self._get_relative('prev()', 'preceding', True, locator, index, timeout, ele_only) - def next(self, filter_loc='', index=1, timeout=None, ele_only=True): + def next(self, locator='', index=1, timeout=None, ele_only=True): """返回后面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 后面第几个查询结果,1开始 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 兄弟元素 """ - return self._get_relative('next()', 'following', True, filter_loc, index, timeout, ele_only) + return self._get_relative('next()', 'following', True, locator, index, timeout, ele_only) - def before(self, filter_loc='', index=1, timeout=None, ele_only=True): + def before(self, locator='', index=1, timeout=None, ele_only=True): """返回前面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 前面第几个查询结果,1开始 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素前面的某个元素或节点 """ - return self._get_relative('before()', 'preceding', False, filter_loc, index, timeout, ele_only) + return self._get_relative('before()', 'preceding', False, locator, index, timeout, ele_only) - def after(self, filter_loc='', index=1, timeout=None, ele_only=True): + def after(self, locator='', index=1, timeout=None, ele_only=True): """返回后面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 后面第几个查询结果,1开始 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素后面的某个元素或节点 """ - return self._get_relative('after()', 'following', False, filter_loc, index, timeout, ele_only) + return self._get_relative('after()', 'following', False, locator, index, timeout, ele_only) - def children(self, filter_loc='', timeout=None, ele_only=True): + def children(self, locator='', timeout=None, ele_only=True): """返回直接子元素元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 直接子元素或节点文本组成的列表 """ - if not filter_loc: + if not locator: loc = '*' if ele_only else 'node()' else: - loc = get_loc(filter_loc, True) # 把定位符转换为xpath + loc = get_loc(locator, True) # 把定位符转换为xpath if loc[0] == 'css selector': raise ValueError('此css selector语法不受支持,请换成xpath。') loc = loc[1].lstrip('./') @@ -240,69 +239,69 @@ class DrissionElement(BaseElement): nodes = self._ele(loc, timeout=timeout, index=None, relative=True) return [e for e in nodes if not (isinstance(e, str) and sub('[ \n\t\r]', '', e) == '')] - def prevs(self, filter_loc='', timeout=None, ele_only=True): + def prevs(self, locator='', timeout=None, ele_only=True): """返回前面全部兄弟元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 兄弟元素或节点文本组成的列表 """ - return self._get_relatives(filter_loc=filter_loc, direction='preceding', timeout=timeout, ele_only=ele_only) + return self._get_relatives(locator=locator, direction='preceding', timeout=timeout, ele_only=ele_only) - def nexts(self, filter_loc='', timeout=None, ele_only=True): + def nexts(self, locator='', timeout=None, ele_only=True): """返回后面全部兄弟元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 兄弟元素或节点文本组成的列表 """ - return self._get_relatives(filter_loc=filter_loc, direction='following', timeout=timeout, ele_only=ele_only) + return self._get_relatives(locator=locator, direction='following', timeout=timeout, ele_only=ele_only) - def befores(self, filter_loc='', timeout=None, ele_only=True): + def befores(self, locator='', timeout=None, ele_only=True): """返回后面全部兄弟元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素前面的元素或节点组成的列表 """ - return self._get_relatives(filter_loc=filter_loc, direction='preceding', + return self._get_relatives(locator=locator, direction='preceding', brother=False, timeout=timeout, ele_only=ele_only) - def afters(self, filter_loc='', timeout=None, ele_only=True): + def afters(self, locator='', timeout=None, ele_only=True): """返回前面全部兄弟元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素后面的元素或节点组成的列表 """ - return self._get_relatives(filter_loc=filter_loc, direction='following', + return self._get_relatives(locator=locator, direction='following', brother=False, timeout=timeout, ele_only=ele_only) - def _get_relative(self, func, direction, brother, filter_loc='', index=1, timeout=None, ele_only=True): + def _get_relative(self, func, direction, brother, locator='', index=1, timeout=None, ele_only=True): """获取一个亲戚元素或节点,可用查询语法筛选,可指定返回筛选结果的第几个 :param func: 方法名称 :param direction: 方向,'following' 或 'preceding' - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 前面第几个查询结果,1开始 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素前面的某个元素或节点 """ - if isinstance(filter_loc, int): - index = filter_loc - filter_loc = '' - node = self._get_relatives(index, filter_loc, direction, brother, timeout, ele_only) + if isinstance(locator, int): + index = locator + locator = '' + node = self._get_relatives(index, locator, direction, brother, timeout, ele_only) if node: return node if Settings.raise_when_ele_not_found: - raise ElementNotFoundError(None, func, {'filter_loc': filter_loc, 'index': index, 'ele_only': ele_only}) + raise ElementNotFoundError(None, func, {'locator': locator, 'index': index, 'ele_only': ele_only}) else: - return NoneElement(self.page, func, {'filter_loc': filter_loc, 'index': index, 'ele_only': ele_only}) + return NoneElement(self.page, func, {'locator': locator, 'index': index, 'ele_only': ele_only}) - def _get_relatives(self, index=None, filter_loc='', direction='following', brother=True, timeout=.5, ele_only=True): + def _get_relatives(self, index=None, locator='', direction='following', brother=True, timeout=.5, ele_only=True): """按要求返回兄弟元素或节点组成的列表 :param index: 获取第几个,该参数不为None时只获取该编号的元素 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param direction: 'following' 或 'preceding',查找的方向 :param brother: 查找范围,在同级查找还是整个dom前后查找 :param timeout: 查找等待时间(秒) @@ -310,11 +309,11 @@ class DrissionElement(BaseElement): """ brother = '-sibling' if brother else '' - if not filter_loc: + if not locator: loc = '*' if ele_only else 'node()' else: - loc = get_loc(filter_loc, True) # 把定位符转换为xpath + loc = get_loc(locator, True) # 把定位符转换为xpath if loc[0] == 'css selector': raise ValueError('此css selector语法不受支持,请换成xpath。') loc = loc[1].lstrip('./') @@ -424,29 +423,29 @@ class BasePage(BaseParser): def get(self, url, show_errmsg=False, retry=None, interval=None): pass - def _ele(self, loc_or_ele, timeout=None, index=1, raise_err=None, method=None): + def _ele(self, locator, timeout=None, index=1, raise_err=None, method=None): """调用获取元素的方法 - :param loc_or_ele: 定位符 + :param locator: 定位符 :param timeout: 超时时间(秒) :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :param raise_err: 找不到时是否抛出异常 :param method: 调用的方法名 :return: 元素对象或它们组成的列表 """ - if not loc_or_ele: - raise ElementNotFoundError(None, method, {'loc_or_str': loc_or_ele}) + if not locator: + raise ElementNotFoundError(None, method, {'locator': locator}) - r = self._find_elements(loc_or_ele, timeout=timeout, index=index, raise_err=raise_err) + r = self._find_elements(locator, timeout=timeout, index=index, raise_err=raise_err) if r or isinstance(r, list): return r if Settings.raise_when_ele_not_found or raise_err is True: - raise ElementNotFoundError(None, method, {'loc_or_str': loc_or_ele, 'index': index}) + raise ElementNotFoundError(None, method, {'locator': locator, 'index': index}) r.method = method - r.args = {'loc_or_str': loc_or_ele, 'index': index} + r.args = {'locator': locator, 'index': index} return r @abstractmethod - def _find_elements(self, loc_or_ele, timeout=None, index=1, raise_err=None): + def _find_elements(self, locator, timeout=None, index=1, raise_err=None): pass diff --git a/DrissionPage/_base/base.pyi b/DrissionPage/_base/base.pyi index 1642529..2d4148d 100644 --- a/DrissionPage/_base/base.pyi +++ b/DrissionPage/_base/base.pyi @@ -16,25 +16,25 @@ from .._elements.none_element import NoneElement class BaseParser(object): _type: str - def __call__(self, loc_or_str: Union[Tuple[str, str], str], index: int = 1): ... + def __call__(self, locator: Union[Tuple[str, str], str], index: int = 1): ... def ele(self, - loc_or_ele: Union[Tuple[str, str], str, BaseElement], + locator: Union[Tuple[str, str], str, BaseElement], index: int = 1, timeout: float = None): ... - def eles(self, loc_or_str: Union[Tuple[str, str], str], timeout=None): ... + def eles(self, locator: Union[Tuple[str, str], str], timeout=None): ... # ----------------以下属性或方法待后代实现---------------- @property def html(self) -> str: ... - def s_ele(self, loc_or_ele: Union[Tuple[str, str], str, BaseElement], index: int = 1): ... + def s_ele(self, locator: Union[Tuple[str, str], str, BaseElement], index: int = 1): ... - def s_eles(self, loc_or_str: Union[Tuple[str, str], str]): ... + def s_eles(self, locator: Union[Tuple[str, str], str]): ... def _ele(self, - loc_or_ele, + locator, timeout: float = None, index: Optional[int] = 1, raise_err: bool = None, @@ -42,7 +42,7 @@ class BaseParser(object): @abstractmethod def _find_elements(self, - loc_or_ele, + locator, timeout: float = None, index: Optional[int] = 1, raise_err: bool = None): ... @@ -58,7 +58,7 @@ class BaseElement(BaseParser): def tag(self) -> str: ... def _ele(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], timeout: float = None, index: Optional[int] = 1, relative: bool = False, @@ -66,7 +66,8 @@ class BaseElement(BaseParser): method: str = None): ... @abstractmethod - def _find_elements(self, loc_or_str, + def _find_elements(self, + locator, timeout: float = None, index: Optional[int] = 1, relative: bool = False, @@ -108,57 +109,57 @@ class DrissionElement(BaseElement): index: int = 1) -> Union[DrissionElement, None]: ... def child(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[DrissionElement, str, NoneElement]: ... def prev(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[DrissionElement, str, NoneElement]: ... def next(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[DrissionElement, str, NoneElement]: ... def before(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[DrissionElement, str, NoneElement]: ... def after(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[DrissionElement, str, NoneElement]: ... def children(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[DrissionElement, str]]: ... def prevs(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[DrissionElement, str]]: ... def nexts(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[DrissionElement, str]]: ... def befores(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[DrissionElement, str]]: ... def afters(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[DrissionElement, str]]: ... @@ -166,14 +167,14 @@ class DrissionElement(BaseElement): func: str, direction: str, brother: bool, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> DrissionElement: ... def _get_relatives(self, index: int = None, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', direction: str = 'following', brother: bool = True, timeout: float = 0.5, @@ -245,7 +246,7 @@ class BasePage(BaseParser): def get(self, url: str, show_errmsg: bool = False, retry: int = None, interval: float = None): ... def _ele(self, - loc_or_ele, + locator, timeout: float = None, index: Optional[int] = 1, raise_err: bool = None, @@ -253,7 +254,7 @@ class BasePage(BaseParser): @abstractmethod def _find_elements(self, - loc_or_ele, + locator, timeout: float = None, index: Optional[int] = 1, raise_err: bool = None): ... diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index ca373e1..2b4e678 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -82,13 +82,13 @@ class ChromiumElement(DrissionElement): attrs = [f"{attr}='{attrs[attr]}'" for attr in attrs] return f'' - def __call__(self, loc_or_str, index=1, timeout=None): + def __call__(self, locator, index=1, timeout=None): """在内部查找元素 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 超时时间(秒) :return: ChromiumElement对象或属性、文本 """ - return self.ele(loc_or_str, index=index, timeout=timeout) + return self.ele(locator, index=index, timeout=timeout) def __eq__(self, other): return self._backend_id == getattr(other, '_backend_id', None) @@ -235,104 +235,104 @@ class ChromiumElement(DrissionElement): """ return super().parent(level_or_loc, index) - def child(self, filter_loc='', index=1, timeout=None, ele_only=True): + def child(self, locator='', index=1, timeout=None, ele_only=True): """返回当前元素的一个符合条件的直接子元素,可用查询语法筛选,可指定返回筛选结果的第几个 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 第几个查询结果,1开始 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 直接子元素或节点文本 """ - return super().child(filter_loc, index, timeout, ele_only=ele_only) + return super().child(locator, index, timeout, ele_only=ele_only) - def prev(self, filter_loc='', index=1, timeout=None, ele_only=True): + def prev(self, locator='', index=1, timeout=None, ele_only=True): """返回当前元素前面一个符合条件的同级元素,可用查询语法筛选,可指定返回筛选结果的第几个 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 前面第几个查询结果,1开始 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 兄弟元素或节点文本 """ - return super().prev(filter_loc, index, timeout, ele_only=ele_only) + return super().prev(locator, index, timeout, ele_only=ele_only) - def next(self, filter_loc='', index=1, timeout=None, ele_only=True): + def next(self, locator='', index=1, timeout=None, ele_only=True): """返回当前元素后面一个符合条件的同级元素,可用查询语法筛选,可指定返回筛选结果的第几个 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 第几个查询结果,1开始 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 兄弟元素或节点文本 """ - return super().next(filter_loc, index, timeout, ele_only=ele_only) + return super().next(locator, index, timeout, ele_only=ele_only) - def before(self, filter_loc='', index=1, timeout=None, ele_only=True): + def before(self, locator='', index=1, timeout=None, ele_only=True): """返回文档中当前元素前面符合条件的一个元素,可用查询语法筛选,可指定返回筛选结果的第几个 查找范围不限同级元素,而是整个DOM文档 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 前面第几个查询结果,1开始 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素前面的某个元素或节点 """ - return super().before(filter_loc, index, timeout, ele_only=ele_only) + return super().before(locator, index, timeout, ele_only=ele_only) - def after(self, filter_loc='', index=1, timeout=None, ele_only=True): + def after(self, locator='', index=1, timeout=None, ele_only=True): """返回文档中此当前元素后面符合条件的一个元素,可用查询语法筛选,可指定返回筛选结果的第几个 查找范围不限同级元素,而是整个DOM文档 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 第几个查询结果,1开始 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素后面的某个元素或节点 """ - return super().after(filter_loc, index, timeout, ele_only=ele_only) + return super().after(locator, index, timeout, ele_only=ele_only) - def children(self, filter_loc='', timeout=None, ele_only=True): + def children(self, locator='', timeout=None, ele_only=True): """返回当前元素符合条件的直接子元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 直接子元素或节点文本组成的列表 """ - return super().children(filter_loc, timeout, ele_only=ele_only) + return super().children(locator, timeout, ele_only=ele_only) - def prevs(self, filter_loc='', timeout=None, ele_only=True): + def prevs(self, locator='', timeout=None, ele_only=True): """返回当前元素前面符合条件的同级元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 兄弟元素或节点文本组成的列表 """ - return super().prevs(filter_loc, timeout, ele_only=ele_only) + return super().prevs(locator, timeout, ele_only=ele_only) - def nexts(self, filter_loc='', timeout=None, ele_only=True): + def nexts(self, locator='', timeout=None, ele_only=True): """返回当前元素后面符合条件的同级元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 兄弟元素或节点文本组成的列表 """ - return super().nexts(filter_loc, timeout, ele_only=ele_only) + return super().nexts(locator, timeout, ele_only=ele_only) - def befores(self, filter_loc='', timeout=None, ele_only=True): + def befores(self, locator='', timeout=None, ele_only=True): """返回文档中当前元素前面符合条件的元素或节点组成的列表,可用查询语法筛选 查找范围不限同级元素,而是整个DOM文档 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素前面的元素或节点组成的列表 """ - return super().befores(filter_loc, timeout, ele_only=ele_only) + return super().befores(locator, timeout, ele_only=ele_only) - def afters(self, filter_loc='', timeout=None, ele_only=True): + def afters(self, locator='', timeout=None, ele_only=True): """返回文档中当前元素后面符合条件的元素或节点组成的列表,可用查询语法筛选 查找范围不限同级元素,而是整个DOM文档 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素后面的元素或节点组成的列表 """ - return super().afters(filter_loc, timeout, ele_only=ele_only) + return super().afters(locator, timeout, ele_only=ele_only) def attr(self, attr): """返回一个attribute属性值 @@ -402,60 +402,60 @@ class ChromiumElement(DrissionElement): """ run_js(self, script, as_expr, 0, args) - def ele(self, loc_or_str, index=1, timeout=None): + def ele(self, locator, index=1, timeout=None): """返回当前元素下级符合条件的一个元素、属性或节点文本 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param index: 获取第几个元素,从1开始,可传入负数获取倒数第几个 :param timeout: 查找元素超时时间(秒),默认与元素所在页面等待时间一致 :return: ChromiumElement对象或属性、文本 """ - return self._ele(loc_or_str, timeout, index=index, method='ele()') + return self._ele(locator, timeout, index=index, method='ele()') - def eles(self, loc_or_str, timeout=None): + def eles(self, locator, timeout=None): """返回当前元素下级所有符合条件的子元素、属性或节点文本 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 查找元素超时时间(秒),默认与元素所在页面等待时间一致 :return: ChromiumElement对象或属性、文本组成的列表 """ - return self._ele(loc_or_str, timeout=timeout, index=None) + return self._ele(locator, timeout=timeout, index=None) - def s_ele(self, loc_or_str=None, index=1): + def s_ele(self, locator=None, index=1): """查找一个符合条件的元素,以SessionElement形式返回 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :return: SessionElement对象或属性、文本 """ if self.tag in __FRAME_ELEMENT__: - r = make_session_ele(self.inner_html, loc_or_str, index=index) + r = make_session_ele(self.inner_html, locator, index=index) else: - r = make_session_ele(self, loc_or_str, index=index) + r = make_session_ele(self, locator, index=index) if isinstance(r, NoneElement): if Settings.raise_when_ele_not_found: - raise ElementNotFoundError(None, 's_ele()', {'loc_or_str': loc_or_str}) + raise ElementNotFoundError(None, 's_ele()', {'locator': locator}) else: r.method = 's_ele()' - r.args = {'loc_or_str': loc_or_str} + r.args = {'locator': locator} return r - def s_eles(self, loc_or_str=None): + def s_eles(self, locator=None): """查找所有符合条件的元素,以SessionElement列表形式返回 - :param loc_or_str: 定位符 + :param locator: 定位符 :return: SessionElement或属性、文本组成的列表 """ if self.tag in __FRAME_ELEMENT__: - return make_session_ele(self.inner_html, loc_or_str, index=None) - return make_session_ele(self, loc_or_str, index=None) + return make_session_ele(self.inner_html, locator, index=None) + return make_session_ele(self, locator, index=None) - def _find_elements(self, loc_or_str, timeout=None, index=1, relative=False, raise_err=None): + def _find_elements(self, locator, timeout=None, index=1, relative=False, raise_err=None): """返回当前元素下级符合条件的子元素、属性或节点文本,默认返回第一个 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 查找元素超时时间(秒) :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 :param relative: WebPage用的表示是否相对定位的参数 :param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置 :return: ChromiumElement对象或文本、属性或其组成的列表 """ - return find_in_chromium_ele(self, loc_or_str, index, timeout, relative=relative) + return find_in_chromium_ele(self, locator, index, timeout, relative=relative) def style(self, style, pseudo_ele=''): """返回元素样式属性值,可获取伪元素属性值 @@ -789,15 +789,15 @@ class ShadowRoot(BaseElement): def __repr__(self): return f'' - def __call__(self, loc_or_str, index=1, timeout=None): + def __call__(self, locator, index=1, timeout=None): """在内部查找元素 例:ele2 = ele1('@id=ele_id') - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :param timeout: 超时时间(秒) :return: 元素对象或属性、文本 """ - return self.ele(loc_or_str, index=index, timeout=timeout) + return self.ele(locator, index=index, timeout=timeout) def __eq__(self, other): return self._backend_id == getattr(other, '_backend_id', None) @@ -868,16 +868,16 @@ class ShadowRoot(BaseElement): return self.parent_ele._ele(loc, timeout=0, relative=True, raise_err=False, method='parent()') - def child(self, filter_loc='', index=1): + def child(self, locator='', index=1): """返回直接子元素元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 第几个查询结果,1开始 :return: 直接子元素或节点文本组成的列表 """ - if not filter_loc: + if not locator: loc = '*' else: - loc = get_loc(filter_loc, True) # 把定位符转换为xpath + loc = get_loc(locator, True) # 把定位符转换为xpath if loc[0] == 'css selector': raise ValueError('此css selector语法不受支持,请换成xpath。') loc = loc[1].lstrip('./') @@ -888,17 +888,17 @@ class ShadowRoot(BaseElement): return ele if Settings.raise_when_ele_not_found: - raise ElementNotFoundError(None, 'child()', {'filter_loc': filter_loc, 'index': index}) + raise ElementNotFoundError(None, 'child()', {'locator': locator, 'index': index}) else: - return NoneElement(self.page, 'child()', {'filter_loc': filter_loc, 'index': index}) + return NoneElement(self.page, 'child()', {'locator': locator, 'index': index}) - def next(self, filter_loc='', index=1): + def next(self, locator='', index=1): """返回当前元素后面一个符合条件的同级元素,可用查询语法筛选,可指定返回筛选结果的第几个 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 第几个查询结果,1开始 :return: ChromiumElement对象 """ - loc = get_loc(filter_loc, True) + loc = get_loc(locator, True) if loc[0] == 'css selector': raise ValueError('此css selector语法不受支持,请换成xpath。') @@ -909,18 +909,18 @@ class ShadowRoot(BaseElement): return ele if Settings.raise_when_ele_not_found: - raise ElementNotFoundError(None, 'next()', {'filter_loc': filter_loc, 'index': index}) + raise ElementNotFoundError(None, 'next()', {'locator': locator, 'index': index}) else: - return NoneElement(self.page, 'next()', {'filter_loc': filter_loc, 'index': index}) + return NoneElement(self.page, 'next()', {'locator': locator, 'index': index}) - def before(self, filter_loc='', index=1): + def before(self, locator='', index=1): """返回文档中当前元素前面符合条件的一个元素,可用查询语法筛选,可指定返回筛选结果的第几个 查找范围不限同级元素,而是整个DOM文档 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 前面第几个查询结果,1开始 :return: 本元素前面的某个元素或节点 """ - loc = get_loc(filter_loc, True) + loc = get_loc(locator, True) if loc[0] == 'css selector': raise ValueError('此css selector语法不受支持,请换成xpath。') @@ -931,34 +931,34 @@ class ShadowRoot(BaseElement): return ele if Settings.raise_when_ele_not_found: - raise ElementNotFoundError(None, 'before()', {'filter_loc': filter_loc, 'index': index}) + raise ElementNotFoundError(None, 'before()', {'locator': locator, 'index': index}) else: - return NoneElement(self.page, 'before()', {'filter_loc': filter_loc, 'index': index}) + return NoneElement(self.page, 'before()', {'locator': locator, 'index': index}) - def after(self, filter_loc='', index=1): + def after(self, locator='', index=1): """返回文档中此当前元素后面符合条件的一个元素,可用查询语法筛选,可指定返回筛选结果的第几个 查找范围不限同级元素,而是整个DOM文档 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 后面第几个查询结果,1开始 :return: 本元素后面的某个元素或节点 """ - nodes = self.afters(filter_loc=filter_loc) + nodes = self.afters(locator=locator) if nodes: return nodes[index - 1] if Settings.raise_when_ele_not_found: - raise ElementNotFoundError(None, 'after()', {'filter_loc': filter_loc, 'index': index}) + raise ElementNotFoundError(None, 'after()', {'locator': locator, 'index': index}) else: - return NoneElement(self.page, 'after()', {'filter_loc': filter_loc, 'index': index}) + return NoneElement(self.page, 'after()', {'locator': locator, 'index': index}) - def children(self, filter_loc=''): + def children(self, locator=''): """返回当前元素符合条件的直接子元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :return: 直接子元素或节点文本组成的列表 """ - if not filter_loc: + if not locator: loc = '*' else: - loc = get_loc(filter_loc, True) # 把定位符转换为xpath + loc = get_loc(locator, True) # 把定位符转换为xpath if loc[0] == 'css selector': raise ValueError('此css selector语法不受支持,请换成xpath。') loc = loc[1].lstrip('./') @@ -966,12 +966,12 @@ class ShadowRoot(BaseElement): loc = f'xpath:./{loc}' return self._ele(loc, index=None, relative=True) - def nexts(self, filter_loc=''): + def nexts(self, locator=''): """返回当前元素后面符合条件的同级元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :return: ChromiumElement对象组成的列表 """ - loc = get_loc(filter_loc, True) + loc = get_loc(locator, True) if loc[0] == 'css selector': raise ValueError('此css selector语法不受支持,请换成xpath。') @@ -979,13 +979,13 @@ class ShadowRoot(BaseElement): xpath = f'xpath:./{loc}' return self.parent_ele._ele(xpath, index=None, relative=True) - def befores(self, filter_loc=''): + def befores(self, locator=''): """返回文档中当前元素前面符合条件的元素或节点组成的列表,可用查询语法筛选 查找范围不限同级元素,而是整个DOM文档 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :return: 本元素前面的元素或节点组成的列表 """ - loc = get_loc(filter_loc, True) + loc = get_loc(locator, True) if loc[0] == 'css selector': raise ValueError('此css selector语法不受支持,请换成xpath。') @@ -993,63 +993,63 @@ class ShadowRoot(BaseElement): xpath = f'xpath:./preceding::{loc}' return self.parent_ele._ele(xpath, index=None, relative=True) - def afters(self, filter_loc=''): + def afters(self, locator=''): """返回文档中当前元素后面符合条件的元素或节点组成的列表,可用查询语法筛选 查找范围不限同级元素,而是整个DOM文档 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :return: 本元素后面的元素或节点组成的列表 """ - eles1 = self.nexts(filter_loc) - loc = get_loc(filter_loc, True)[1].lstrip('./') + eles1 = self.nexts(locator) + loc = get_loc(locator, True)[1].lstrip('./') xpath = f'xpath:./following::{loc}' return eles1 + self.parent_ele._ele(xpath, index=None, relative=True) - def ele(self, loc_or_str, index=1, timeout=None): + def ele(self, locator, index=1, timeout=None): """返回当前元素下级符合条件的一个元素 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param index: 获取第几个元素,从1开始,可传入负数获取倒数第几个 :param timeout: 查找元素超时时间(秒),默认与元素所在页面等待时间一致 :return: ChromiumElement对象 """ - return self._ele(loc_or_str, timeout, index=index, method='ele()') + return self._ele(locator, timeout, index=index, method='ele()') - def eles(self, loc_or_str, timeout=None): + def eles(self, locator, timeout=None): """返回当前元素下级所有符合条件的子元素 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 查找元素超时时间(秒),默认与元素所在页面等待时间一致 :return: ChromiumElement对象组成的列表 """ - return self._ele(loc_or_str, timeout=timeout, index=None) + return self._ele(locator, timeout=timeout, index=None) - def s_ele(self, loc_or_str=None, index=1): + def s_ele(self, locator=None, index=1): """查找一个符合条件的元素以SessionElement形式返回,处理复杂页面时效率很高 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :return: SessionElement对象或属性、文本 """ - r = make_session_ele(self, loc_or_str, index=index) + r = make_session_ele(self, locator, index=index) if isinstance(r, NoneElement): r.method = 's_ele()' - r.args = {'loc_or_str': loc_or_str} + r.args = {'locator': locator} return r - def s_eles(self, loc_or_str): + def s_eles(self, locator): """查找所有符合条件的元素以SessionElement列表形式返回,处理复杂页面时效率很高 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :return: SessionElement对象 """ - return make_session_ele(self, loc_or_str, index=None) + return make_session_ele(self, locator, index=None) - def _find_elements(self, loc_or_str, timeout=None, index=1, relative=False, raise_err=None): + def _find_elements(self, locator, timeout=None, index=1, relative=False, raise_err=None): """返回当前元素下级符合条件的子元素、属性或节点文本,默认返回第一个 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 查找元素超时时间(秒) :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 :param relative: WebPage用的表示是否相对定位的参数 :param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置 :return: ChromiumElement对象或其组成的列表 """ - loc = get_loc(loc_or_str, css_mode=False) + loc = get_loc(locator, css_mode=False) if loc[0] == 'css selector' and str(loc[1]).startswith(':root'): loc = loc[0], loc[1][5:] @@ -1114,20 +1114,20 @@ class ShadowRoot(BaseElement): return r['backendNodeId'] -def find_in_chromium_ele(ele, loc, index=1, timeout=None, relative=True): +def find_in_chromium_ele(ele, locator, index=1, timeout=None, relative=True): """在chromium元素中查找 :param ele: ChromiumElement对象 - :param loc: 元素定位元组 + :param locator: 元素定位元组 :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 :param timeout: 查找元素超时时间(秒) :param relative: WebPage用于标记是否相对定位使用 :return: 返回ChromiumElement元素或它们组成的列表 """ # ---------------处理定位符--------------- - if isinstance(loc, (str, tuple)): - loc = get_loc(loc) + if isinstance(locator, (str, tuple)): + loc = get_loc(locator) else: - raise ValueError(f"定位符必须为str或长度为2的tuple对象。现在是:{loc}") + raise ValueError(f"定位符必须为str或长度为2的tuple对象。现在是:{locator}") loc_str = loc[1] if loc[0] == 'xpath' and loc[1].lstrip().startswith('/'): diff --git a/DrissionPage/_elements/chromium_element.pyi b/DrissionPage/_elements/chromium_element.pyi index cbddbdc..1403ae1 100644 --- a/DrissionPage/_elements/chromium_element.pyi +++ b/DrissionPage/_elements/chromium_element.pyi @@ -48,7 +48,7 @@ class ChromiumElement(DrissionElement): def __repr__(self) -> str: ... def __call__(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], index: int = 1, timeout: float = None) -> Union[ChromiumElement, NoneElement]: ... @@ -103,57 +103,57 @@ class ChromiumElement(DrissionElement): index: int = 1) -> Union[ChromiumElement, NoneElement]: ... def child(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[ChromiumElement, str, NoneElement]: ... def prev(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[ChromiumElement, str, NoneElement]: ... def next(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[ChromiumElement, str, NoneElement]: ... def before(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[ChromiumElement, str, NoneElement]: ... def after(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[ChromiumElement, str, NoneElement]: ... def children(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[ChromiumElement, str]]: ... def prevs(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[ChromiumElement, str]]: ... def nexts(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[ChromiumElement, str]]: ... def befores(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[ChromiumElement, str]]: ... def afters(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[ChromiumElement, str]]: ... @@ -176,22 +176,22 @@ class ChromiumElement(DrissionElement): def run_async_js(self, script: str, *args, as_expr: bool = False) -> None: ... def ele(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], index: int = 1, timeout: float = None) -> Union[ChromiumElement, NoneElement]: ... def eles(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], timeout: float = None) -> List[ChromiumElement]: ... def s_ele(self, - loc_or_str: Union[Tuple[str, str], str] = None, + locator: Union[Tuple[str, str], str] = None, index: int = 1) -> Union[SessionElement, NoneElement]: ... - def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = None) -> List[SessionElement]: ... + def s_eles(self, locator: Union[Tuple[str, str], str] = None) -> List[SessionElement]: ... def _find_elements(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], timeout: float = None, index: Optional[int] = 1, relative: bool = False, @@ -248,7 +248,8 @@ class ShadowRoot(BaseElement): def __repr__(self) -> str: ... - def __call__(self, loc_or_str: Union[Tuple[str, str], str], + def __call__(self, + locator: Union[Tuple[str, str], str], timeout: float = None) -> ChromiumElement: ... def __eq__(self, other: ShadowRoot) -> bool: ... @@ -271,43 +272,47 @@ class ShadowRoot(BaseElement): def parent(self, level_or_loc: Union[str, int] = 1, index: int = 1) -> ChromiumElement: ... - def child(self, filter_loc: Union[tuple, str] = '', + def child(self, + locator: Union[tuple, str] = '', index: int = 1) -> Union[ChromiumElement, NoneElement]: ... - def next(self, filter_loc: Union[tuple, str] = '', + def next(self, + locator: Union[tuple, str] = '', index: int = 1) -> Union[ChromiumElement, NoneElement]: ... - def before(self, filter_loc: Union[tuple, str] = '', + def before(self, + locator: Union[tuple, str] = '', index: int = 1) -> Union[ChromiumElement, NoneElement]: ... - def after(self, filter_loc: Union[tuple, str] = '', + def after(self, + locator: Union[tuple, str] = '', index: int = 1) -> Union[ChromiumElement, NoneElement]: ... - def children(self, filter_loc: Union[tuple, str] = '') -> List[ChromiumElement]: ... + def children(self, locator: Union[tuple, str] = '') -> List[ChromiumElement]: ... - def nexts(self, filter_loc: Union[tuple, str] = '') -> List[ChromiumElement]: ... + def nexts(self, locator: Union[tuple, str] = '') -> List[ChromiumElement]: ... - def befores(self, filter_loc: Union[tuple, str] = '') -> List[ChromiumElement]: ... + def befores(self, locator: Union[tuple, str] = '') -> List[ChromiumElement]: ... - def afters(self, filter_loc: Union[tuple, str] = '') -> List[ChromiumElement]: ... + def afters(self, locator: Union[tuple, str] = '') -> List[ChromiumElement]: ... def ele(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], index: int = 1, timeout: float = None) -> Union[ChromiumElement, NoneElement]: ... def eles(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], timeout: float = None) -> List[ChromiumElement]: ... def s_ele(self, - loc_or_str: Union[Tuple[str, str], str] = None, + locator: Union[Tuple[str, str], str] = None, index: int = 1) -> Union[SessionElement, NoneElement]: ... - def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[SessionElement]: ... + def s_eles(self, locator: Union[Tuple[str, str], str]) -> List[SessionElement]: ... def _find_elements(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], timeout: float = None, index: Optional[int] = 1, relative: bool = False, diff --git a/DrissionPage/_elements/session_element.py b/DrissionPage/_elements/session_element.py index 761668c..8b6173d 100644 --- a/DrissionPage/_elements/session_element.py +++ b/DrissionPage/_elements/session_element.py @@ -37,14 +37,14 @@ class SessionElement(DrissionElement): attrs = [f"{attr}='{self.attrs[attr]}'" for attr in self.attrs] return f'' - def __call__(self, loc_or_str, timeout=None): + def __call__(self, locator, timeout=None): """在内部查找元素 例:ele2 = ele1('@id=ele_id') - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 不起实际作用 :return: SessionElement对象或属性、文本 """ - return self.ele(loc_or_str) + return self.ele(locator) def __eq__(self, other): return self.xpath == getattr(other, 'xpath', None) @@ -89,104 +89,104 @@ class SessionElement(DrissionElement): """ return super().parent(level_or_loc, index) - def child(self, filter_loc='', index=1, timeout=None, ele_only=True): + def child(self, locator='', index=1, timeout=None, ele_only=True): """返回当前元素的一个符合条件的直接子元素,可用查询语法筛选,可指定返回筛选结果的第几个 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 第几个查询结果,1开始 :param timeout: 此参数不起实际作用 :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 直接子元素或节点文本 """ - return super().child(filter_loc, index, timeout, ele_only=ele_only) + return super().child(locator, index, timeout, ele_only=ele_only) - def prev(self, filter_loc='', index=1, timeout=None, ele_only=True): + def prev(self, locator='', index=1, timeout=None, ele_only=True): """返回当前元素前面一个符合条件的同级元素,可用查询语法筛选,可指定返回筛选结果的第几个 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 前面第几个查询结果,1开始 :param timeout: 此参数不起实际作用 :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 同级元素 """ - return super().prev(filter_loc, index, timeout, ele_only=ele_only) + return super().prev(locator, index, timeout, ele_only=ele_only) - def next(self, filter_loc='', index=1, timeout=None, ele_only=True): + def next(self, locator='', index=1, timeout=None, ele_only=True): """返回当前元素后面一个符合条件的同级元素,可用查询语法筛选,可指定返回筛选结果的第几个 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 第几个查询结果,1开始 :param timeout: 此参数不起实际作用 :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 同级元素 """ - return super().next(filter_loc, index, timeout, ele_only=ele_only) + return super().next(locator, index, timeout, ele_only=ele_only) - def before(self, filter_loc='', index=1, timeout=None, ele_only=True): + def before(self, locator='', index=1, timeout=None, ele_only=True): """返回文档中当前元素前面符合条件的一个元素,可用查询语法筛选,可指定返回筛选结果的第几个 查找范围不限同级元素,而是整个DOM文档 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 前面第几个查询结果,1开始 :param timeout: 此参数不起实际作用 :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素前面的某个元素或节点 """ - return super().before(filter_loc, index, timeout, ele_only=ele_only) + return super().before(locator, index, timeout, ele_only=ele_only) - def after(self, filter_loc='', index=1, timeout=None, ele_only=True): + def after(self, locator='', index=1, timeout=None, ele_only=True): """返回文档中此当前元素后面符合条件的一个元素,可用查询语法筛选,可指定返回筛选结果的第几个 查找范围不限同级元素,而是整个DOM文档 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 第几个查询结果,1开始 :param timeout: 此参数不起实际作用 :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素后面的某个元素或节点 """ - return super().after(filter_loc, index, timeout, ele_only=ele_only) + return super().after(locator, index, timeout, ele_only=ele_only) - def children(self, filter_loc='', timeout=0, ele_only=True): + def children(self, locator='', timeout=0, ele_only=True): """返回当前元素符合条件的直接子元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 此参数不起实际作用 :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 直接子元素或节点文本组成的列表 """ - return super().children(filter_loc, timeout, ele_only=ele_only) + return super().children(locator, timeout, ele_only=ele_only) - def prevs(self, filter_loc='', timeout=None, ele_only=True): + def prevs(self, locator='', timeout=None, ele_only=True): """返回当前元素前面符合条件的同级元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 此参数不起实际作用 :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 同级元素或节点文本组成的列表 """ - return super().prevs(filter_loc, timeout, ele_only=ele_only) + return super().prevs(locator, timeout, ele_only=ele_only) - def nexts(self, filter_loc='', timeout=None, ele_only=True): + def nexts(self, locator='', timeout=None, ele_only=True): """返回当前元素后面符合条件的同级元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 此参数不起实际作用 :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 同级元素或节点文本组成的列表 """ - return super().nexts(filter_loc, timeout, ele_only=ele_only) + return super().nexts(locator, timeout, ele_only=ele_only) - def befores(self, filter_loc='', timeout=None, ele_only=True): + def befores(self, locator='', timeout=None, ele_only=True): """返回文档中当前元素前面符合条件的元素或节点组成的列表,可用查询语法筛选 查找范围不限同级元素,而是整个DOM文档 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 此参数不起实际作用 :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素前面的元素或节点组成的列表 """ - return super().befores(filter_loc, timeout, ele_only=ele_only) + return super().befores(locator, timeout, ele_only=ele_only) - def afters(self, filter_loc='', timeout=None, ele_only=True): + def afters(self, locator='', timeout=None, ele_only=True): """返回文档中当前元素后面符合条件的元素或节点组成的列表,可用查询语法筛选 查找范围不限同级元素,而是整个DOM文档 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 此参数不起实际作用 :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素后面的元素或节点组成的列表 """ - return super().afters(filter_loc, timeout, ele_only=ele_only) + return super().afters(locator, timeout, ele_only=ele_only) def attr(self, attr): """返回attribute属性值 @@ -221,48 +221,48 @@ class SessionElement(DrissionElement): else: return self.inner_ele.get(attr) - def ele(self, loc_or_str, index=1, timeout=None): + def ele(self, locator, index=1, timeout=None): """返回当前元素下级符合条件的一个元素、属性或节点文本 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param index: 第几个元素,从1开始,可传入负数获取倒数第几个 :param timeout: 不起实际作用 :return: SessionElement对象或属性、文本 """ - return self._ele(loc_or_str, index=index, method='ele()') + return self._ele(locator, index=index, method='ele()') - def eles(self, loc_or_str, timeout=None): + def eles(self, locator, timeout=None): """返回当前元素下级所有符合条件的子元素、属性或节点文本 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 不起实际作用 :return: SessionElement对象或属性、文本组成的列表 """ - return self._ele(loc_or_str, index=None) + return self._ele(locator, index=None) - def s_ele(self, loc_or_str=None, index=1): + def s_ele(self, locator=None, index=1): """返回当前元素下级符合条件的一个元素、属性或节点文本 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :return: SessionElement对象或属性、文本 """ - return self._ele(loc_or_str, index=index, method='s_ele()') + return self._ele(locator, index=index, method='s_ele()') - def s_eles(self, loc_or_str): + def s_eles(self, locator): """返回当前元素下级所有符合条件的子元素、属性或节点文本 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :return: SessionElement对象或属性、文本组成的列表 """ - return self._ele(loc_or_str, index=None) + return self._ele(locator, index=None) - def _find_elements(self, loc_or_str, timeout=None, index=1, relative=False, raise_err=None): + def _find_elements(self, locator, timeout=None, index=1, relative=False, raise_err=None): """返回当前元素下级符合条件的子元素、属性或节点文本 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 不起实际作用,用于和父类对应 :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 :param relative: WebPage用的表示是否相对定位的参数 :param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置 :return: SessionElement对象 """ - return make_session_ele(self, loc_or_str, index=index) + return make_session_ele(self, locator, index=index) def _get_ele_path(self, mode): """获取css路径或xpath路径 diff --git a/DrissionPage/_elements/session_element.pyi b/DrissionPage/_elements/session_element.pyi index 4e6fb8f..66d6156 100644 --- a/DrissionPage/_elements/session_element.pyi +++ b/DrissionPage/_elements/session_element.pyi @@ -29,7 +29,7 @@ class SessionElement(DrissionElement): def __repr__(self) -> str: ... def __call__(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], index: int = 1, timeout: float = None) -> Union[SessionElement, NoneElement]: ... @@ -58,79 +58,79 @@ class SessionElement(DrissionElement): index: int = 1) -> Union[SessionElement, NoneElement]: ... def child(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[SessionElement, str, NoneElement]: ... def prev(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[SessionElement, str, NoneElement]: ... def next(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[SessionElement, str, NoneElement]: ... def before(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[SessionElement, str, NoneElement]: ... def after(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[SessionElement, str, NoneElement]: ... def children(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[SessionElement, str]]: ... def prevs(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[SessionElement, str]]: ... def nexts(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[SessionElement, str]]: ... def befores(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[SessionElement, str]]: ... def afters(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[SessionElement, str]]: ... def attr(self, attr: str) -> Optional[str]: ... def ele(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], index: int = 1, timeout: float = None) -> Union[SessionElement, NoneElement]: ... def eles(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], timeout: float = None) -> List[SessionElement]: ... def s_ele(self, - loc_or_str: Union[Tuple[str, str], str] = None, + locator: Union[Tuple[str, str], str] = None, index: int = 1) -> Union[SessionElement, NoneElement]: ... - def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[SessionElement]: ... + def s_eles(self, locator: Union[Tuple[str, str], str]) -> List[SessionElement]: ... def _find_elements(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], timeout: float = None, index: Optional[int] = 1, relative: bool = False, diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 705fcf3..489c19d 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -245,15 +245,15 @@ class ChromiumBase(BasePage): self.run_cdp('Page.setInterceptFileChooserDialog', enabled=False) self._upload_list = None - def __call__(self, loc_or_str, index=1, timeout=None): + def __call__(self, locator, index=1, timeout=None): """在内部查找元素 例:ele = page('@id=ele_id') - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param index: 获取第几个元素,从1开始,可传入负数获取倒数第几个 :param timeout: 超时时间(秒) :return: ChromiumElement对象 """ - return self.ele(loc_or_str, index, timeout) + return self.ele(locator, index, timeout) def _wait_to_stop(self): """eager策略超时时使页面停止加载""" @@ -492,60 +492,60 @@ class ChromiumBase(BasePage): return [{'name': cookie['name'], 'value': cookie['value'], 'domain': cookie['domain']} for cookie in cookies] - def ele(self, loc_or_ele, index=1, timeout=None): + def ele(self, locator, index=1, timeout=None): """获取一个符合条件的元素对象 - :param loc_or_ele: 定位符或元素对象 + :param locator: 定位符或元素对象 :param index: 获取第几个元素,从1开始,可传入负数获取倒数第几个 :param timeout: 查找超时时间(秒) :return: ChromiumElement对象 """ - return self._ele(loc_or_ele, timeout=timeout, index=index, method='ele()') + return self._ele(locator, timeout=timeout, index=index, method='ele()') - def eles(self, loc_or_str, timeout=None): + def eles(self, locator, timeout=None): """获取所有符合条件的元素对象 - :param loc_or_str: 定位符或元素对象 + :param locator: 定位符或元素对象 :param timeout: 查找超时时间(秒) :return: ChromiumElement对象组成的列表 """ - return self._ele(loc_or_str, timeout=timeout, index=None) + return self._ele(locator, timeout=timeout, index=None) - def s_ele(self, loc_or_ele=None, index=1): + def s_ele(self, locator=None, index=1): """查找一个符合条件的元素以SessionElement形式返回,处理复杂页面时效率很高 - :param loc_or_ele: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :return: SessionElement对象或属性、文本 """ - r = make_session_ele(self, loc_or_ele, index=index) + r = make_session_ele(self, locator, index=index) if isinstance(r, NoneElement): if Settings.raise_when_ele_not_found: - raise ElementNotFoundError(None, 's_ele()', {'loc_or_ele': loc_or_ele}) + raise ElementNotFoundError(None, 's_ele()', {'locator': locator}) else: r.method = 's_ele()' - r.args = {'loc_or_ele': loc_or_ele} + r.args = {'locator': locator} return r - def s_eles(self, loc_or_str): + def s_eles(self, locator): """查找所有符合条件的元素以SessionElement列表形式返回 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :return: SessionElement对象组成的列表 """ - return make_session_ele(self, loc_or_str, index=None) + return make_session_ele(self, locator, index=None) - def _find_elements(self, loc_or_ele, timeout=None, index=1, relative=False, raise_err=None): + def _find_elements(self, locator, timeout=None, index=1, relative=False, raise_err=None): """执行元素查找 - :param loc_or_ele: 定位符或元素对象 + :param locator: 定位符或元素对象 :param timeout: 查找超时时间(秒) :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 :param relative: WebPage用的表示是否相对定位的参数 :param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置 :return: ChromiumElement对象或元素对象组成的列表 """ - if isinstance(loc_or_ele, (str, tuple)): - loc = get_loc(loc_or_ele)[1] - elif loc_or_ele._type in ('ChromiumElement', 'ChromiumFrame'): - return loc_or_ele + if isinstance(locator, (str, tuple)): + loc = get_loc(locator)[1] + elif locator._type in ('ChromiumElement', 'ChromiumFrame'): + return locator else: - raise ValueError('loc_or_str参数只能是tuple、str、ChromiumElement类型。') + raise ValueError('locator参数只能是tuple、str、ChromiumElement类型。') self.wait.doc_loaded() timeout = timeout if timeout is not None else self.timeout @@ -712,14 +712,14 @@ class ChromiumBase(BasePage): r.args = {'loc_ind_ele': loc_ind_ele} return r - def get_frames(self, loc=None, timeout=None): + def get_frames(self, locator=None, timeout=None): """获取所有符合条件的frame对象 - :param loc: 定位符,为None时返回所有 + :param locator: 定位符,为None时返回所有 :param timeout: 查找超时时间(秒) :return: ChromiumFrame对象组成的列表 """ - loc = loc or 'xpath://*[name()="iframe" or name()="frame"]' - frames = self._ele(loc, timeout=timeout, index=None, raise_err=False) + locator = locator or 'xpath://*[name()="iframe" or name()="frame"]' + frames = self._ele(locator, timeout=timeout, index=None, raise_err=False) return [i for i in frames if i._type == 'ChromiumFrame'] def get_session_storage(self, item=None): diff --git a/DrissionPage/_pages/chromium_base.pyi b/DrissionPage/_pages/chromium_base.pyi index 8e9223f..dca5bc0 100644 --- a/DrissionPage/_pages/chromium_base.pyi +++ b/DrissionPage/_pages/chromium_base.pyi @@ -95,7 +95,7 @@ class ChromiumBase(BasePage): def _d_set_runtime_settings(self) -> None: ... def __call__(self, - loc_or_str: Union[Tuple[str, str], str, ChromiumElement], + locator: Union[Tuple[str, str], str, ChromiumElement], index: int = 1, timeout: float = None) -> Union[ChromiumElement, NoneElement]: ... @@ -181,22 +181,22 @@ class ChromiumBase(BasePage): all_info: bool = False) -> Union[list, dict]: ... def ele(self, - loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame], + locator: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame], index: int = 1, timeout: float = None) -> Union[ChromiumElement, NoneElement]: ... def eles(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], timeout: float = None) -> List[ChromiumElement]: ... def s_ele(self, - loc_or_ele: Union[Tuple[str, str], str] = None, + locator: Union[Tuple[str, str], str] = None, index: int = 1) -> Union[SessionElement, NoneElement]: ... - def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[SessionElement]: ... + def s_eles(self, locator: Union[Tuple[str, str], str]) -> List[SessionElement]: ... def _find_elements(self, - loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame], + locator: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame], timeout: float = None, index: Optional[int] = 1, relative: bool = False, @@ -217,7 +217,7 @@ class ChromiumBase(BasePage): def get_frame(self, loc_ind_ele: Union[str, int, tuple, ChromiumFrame], timeout: float = None) -> ChromiumFrame: ... - def get_frames(self, loc: Union[str, tuple] = None, timeout: float = None) -> List[ChromiumFrame]: ... + def get_frames(self, locator: Union[str, tuple] = None, timeout: float = None) -> List[ChromiumFrame]: ... def run_cdp(self, cmd: str, **cmd_args) -> dict: ... diff --git a/DrissionPage/_pages/chromium_frame.py b/DrissionPage/_pages/chromium_frame.py index f947823..dd8946d 100644 --- a/DrissionPage/_pages/chromium_frame.py +++ b/DrissionPage/_pages/chromium_frame.py @@ -64,15 +64,15 @@ class ChromiumFrame(ChromiumBase): break sleep(.1) - def __call__(self, loc_or_str, index=1, timeout=None): + def __call__(self, locator, index=1, timeout=None): """在内部查找元素 例:ele2 = ele1('@id=ele_id') - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :param timeout: 超时时间(秒) :return: ChromiumElement对象或属性、文本 """ - return self.ele(loc_or_str, index=index, timeout=timeout) + return self.ele(locator, index=index, timeout=timeout) def __eq__(self, other): return self._frame_id == getattr(other, '_frame_id', None) @@ -395,85 +395,85 @@ class ChromiumFrame(ChromiumBase): """ return self.frame_ele.parent(level_or_loc, index) - def prev(self, filter_loc='', index=1, timeout=0, ele_only=True): + def prev(self, locator='', index=1, timeout=0, ele_only=True): """返回当前元素前面一个符合条件的同级元素,可用查询语法筛选,可指定返回筛选结果的第几个 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 前面第几个查询结果,1开始 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 同级元素或节点 """ - return self.frame_ele.prev(filter_loc, index, timeout, ele_only=ele_only) + return self.frame_ele.prev(locator, index, timeout, ele_only=ele_only) - def next(self, filter_loc='', index=1, timeout=0, ele_only=True): + def next(self, locator='', index=1, timeout=0, ele_only=True): """返回当前元素后面一个符合条件的同级元素,可用查询语法筛选,可指定返回筛选结果的第几个 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 后面第几个查询结果,1开始 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 同级元素或节点 """ - return self.frame_ele.next(filter_loc, index, timeout, ele_only=ele_only) + return self.frame_ele.next(locator, index, timeout, ele_only=ele_only) - def before(self, filter_loc='', index=1, timeout=None, ele_only=True): + def before(self, locator='', index=1, timeout=None, ele_only=True): """返回文档中当前元素前面符合条件的一个元素,可用查询语法筛选,可指定返回筛选结果的第几个 查找范围不限同级元素,而是整个DOM文档 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 前面第几个查询结果,1开始 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素前面的某个元素或节点 """ - return self.frame_ele.before(filter_loc, index, timeout, ele_only=ele_only) + return self.frame_ele.before(locator, index, timeout, ele_only=ele_only) - def after(self, filter_loc='', index=1, timeout=None, ele_only=True): + def after(self, locator='', index=1, timeout=None, ele_only=True): """返回文档中此当前元素后面符合条件的一个元素,可用查询语法筛选,可指定返回筛选结果的第几个 查找范围不限同级元素,而是整个DOM文档 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param index: 后面第几个查询结果,1开始 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素后面的某个元素或节点 """ - return self.frame_ele.after(filter_loc, index, timeout, ele_only=ele_only) + return self.frame_ele.after(locator, index, timeout, ele_only=ele_only) - def prevs(self, filter_loc='', timeout=0, ele_only=True): + def prevs(self, locator='', timeout=0, ele_only=True): """返回当前元素前面符合条件的同级元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 同级元素或节点文本组成的列表 """ - return self.frame_ele.prevs(filter_loc, timeout, ele_only=ele_only) + return self.frame_ele.prevs(locator, timeout, ele_only=ele_only) - def nexts(self, filter_loc='', timeout=0, ele_only=True): + def nexts(self, locator='', timeout=0, ele_only=True): """返回当前元素后面符合条件的同级元素或节点组成的列表,可用查询语法筛选 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 查找节点的超时时间(秒) :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 同级元素或节点文本组成的列表 """ - return self.frame_ele.nexts(filter_loc, timeout, ele_only=ele_only) + return self.frame_ele.nexts(locator, timeout, ele_only=ele_only) - def befores(self, filter_loc='', timeout=None, ele_only=True): + def befores(self, locator='', timeout=None, ele_only=True): """返回文档中当前元素前面符合条件的元素或节点组成的列表,可用查询语法筛选 查找范围不限同级元素,而是整个DOM文档 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 查找节点的超时时间 :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素前面的元素或节点组成的列表 """ - return self.frame_ele.befores(filter_loc, timeout, ele_only=ele_only) + return self.frame_ele.befores(locator, timeout, ele_only=ele_only) - def afters(self, filter_loc='', timeout=None, ele_only=True): + def afters(self, locator='', timeout=None, ele_only=True): """返回文档中当前元素后面符合条件的元素或节点组成的列表,可用查询语法筛选 查找范围不限同级元素,而是整个DOM文档 - :param filter_loc: 用于筛选的查询语法 + :param locator: 用于筛选的查询语法 :param timeout: 查找节点的超时时间 :param ele_only: 是否只获取元素,为False时把文本、注释节点也纳入 :return: 本元素前面的元素或节点组成的列表 """ - return self.frame_ele.afters(filter_loc, timeout, ele_only=ele_only) + return self.frame_ele.afters(locator, timeout, ele_only=ele_only) def get_screenshot(self, path=None, name=None, as_bytes=None, as_base64=None): """对页面进行截图,可对整个网页、可见网页、指定范围截图。对可视范围外截图需要90以上版本浏览器支持 @@ -562,20 +562,20 @@ class ChromiumFrame(ChromiumBase): self.tab.remove_ele(new_ele) return r - def _find_elements(self, loc_or_ele, timeout=None, index=1, relative=False, raise_err=None): + def _find_elements(self, locator, timeout=None, index=1, relative=False, raise_err=None): """在frame内查找单个元素 - :param loc_or_ele: 定位符或元素对象 + :param locator: 定位符或元素对象 :param timeout: 查找超时时间 :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 :param relative: WebPage用的表示是否相对定位的参数 :param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置 :return: ChromiumElement对象 """ - if isinstance(loc_or_ele, ChromiumElement): - return loc_or_ele + if isinstance(locator, ChromiumElement): + return locator self.wait.doc_loaded() - return self.doc_ele._ele(loc_or_ele, index=index, timeout=timeout, - raise_err=raise_err) if index is not None else self.doc_ele.eles(loc_or_ele, timeout) + return self.doc_ele._ele(locator, index=index, timeout=timeout, + raise_err=raise_err) if index is not None else self.doc_ele.eles(locator, timeout) def _is_inner_frame(self): """返回当前frame是否同域""" diff --git a/DrissionPage/_pages/chromium_frame.pyi b/DrissionPage/_pages/chromium_frame.pyi index f18f4ac..15eaa6c 100644 --- a/DrissionPage/_pages/chromium_frame.pyi +++ b/DrissionPage/_pages/chromium_frame.pyi @@ -43,7 +43,7 @@ class ChromiumFrame(ChromiumBase): self._listener: FrameListener = ... def __call__(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], index: int = 1, timeout: float = None) -> Union[ChromiumElement, NoneElement]: ... @@ -148,46 +148,46 @@ class ChromiumFrame(ChromiumBase): index: int = 1) -> Union[ChromiumElement, NoneElement]: ... def prev(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = 0, ele_only: bool = True) -> Union[ChromiumElement, NoneElement, str]: ... def next(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = 0, ele_only: bool = True) -> Union[ChromiumElement, NoneElement, str]: ... def before(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[ChromiumElement, NoneElement, str]: ... def after(self, - filter_loc: Union[tuple, str, int] = '', + locator: Union[tuple, str, int] = '', index: int = 1, timeout: float = None, ele_only: bool = True) -> Union[ChromiumElement, NoneElement, str]: ... def prevs(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = 0, ele_only: bool = True) -> List[Union[ChromiumElement, str]]: ... def nexts(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = 0, ele_only: bool = True) -> List[Union[ChromiumElement, str]]: ... def befores(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[ChromiumElement, str]]: ... def afters(self, - filter_loc: Union[tuple, str] = '', + locator: Union[tuple, str] = '', timeout: float = None, ele_only: bool = True) -> List[Union[ChromiumElement, str]]: ... @@ -208,7 +208,7 @@ class ChromiumFrame(ChromiumBase): ele: ChromiumElement = None) -> Union[str, bytes]: ... def _find_elements(self, - loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame], + locator: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame], timeout: float = None, index: Optional[int] = 1, relative: bool = False, diff --git a/DrissionPage/_pages/chromium_tab.py b/DrissionPage/_pages/chromium_tab.py index 7e52202..25d0ec1 100644 --- a/DrissionPage/_pages/chromium_tab.py +++ b/DrissionPage/_pages/chromium_tab.py @@ -113,18 +113,18 @@ class WebPageTab(SessionPage, ChromiumTab, BasePage): super(SessionPage, self).__init__(page=page, tab_id=tab_id) self._type = 'WebPageTab' - def __call__(self, loc_or_str, index=1, timeout=None): + def __call__(self, locator, index=1, timeout=None): """在内部查找元素 例:ele = page('@id=ele_id') - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :param timeout: 超时时间(秒) :return: 子元素对象 """ if self._mode == 'd': - return super(SessionPage, self).__call__(loc_or_str, index=index, timeout=timeout) + return super(SessionPage, self).__call__(locator, index=index, timeout=timeout) elif self._mode == 's': - return super().__call__(loc_or_str, index=index) + return super().__call__(locator, index=index) @property def set(self): @@ -258,49 +258,49 @@ class WebPageTab(SessionPage, ChromiumTab, BasePage): return self.response return super().post(url, show_errmsg, retry, interval, **kwargs) - def ele(self, loc_or_ele, index=1, timeout=None): + def ele(self, locator, index=1, timeout=None): """返回第一个符合条件的元素、属性或节点文本 - :param loc_or_ele: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :param timeout: 查找元素超时时间(秒),默认与页面等待时间一致 :return: 元素对象或属性、文本节点文本 """ if self._mode == 's': - return super().ele(loc_or_ele, index=index) + return super().ele(locator, index=index) elif self._mode == 'd': - return super(SessionPage, self).ele(loc_or_ele, index=index, timeout=timeout) + return super(SessionPage, self).ele(locator, index=index, timeout=timeout) - def eles(self, loc_or_str, timeout=None): + def eles(self, locator, timeout=None): """返回页面中所有符合条件的元素、属性或节点文本 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 查找元素超时时间(秒),默认与页面等待时间一致 :return: 元素对象或属性、文本组成的列表 """ if self._mode == 's': - return super().eles(loc_or_str) + return super().eles(locator) elif self._mode == 'd': - return super(SessionPage, self).eles(loc_or_str, timeout=timeout) + return super(SessionPage, self).eles(locator, timeout=timeout) - def s_ele(self, loc_or_ele=None, index=1): + def s_ele(self, locator=None, index=1): """查找第一个符合条件的元素以SessionElement形式返回,d模式处理复杂页面时效率很高 - :param loc_or_ele: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :return: SessionElement对象或属性、文本 """ if self._mode == 's': - return super().s_ele(loc_or_ele, index=index) + return super().s_ele(locator, index=index) elif self._mode == 'd': - return super(SessionPage, self).s_ele(loc_or_ele, index=index) + return super(SessionPage, self).s_ele(locator, index=index) - def s_eles(self, loc_or_str): + def s_eles(self, locator): """查找所有符合条件的元素以SessionElement形式返回,d模式处理复杂页面时效率很高 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :return: SessionElement对象或属性、文本组成的列表 """ if self._mode == 's': - return super().s_eles(loc_or_str) + return super().s_eles(locator) elif self._mode == 'd': - return super(SessionPage, self).s_eles(loc_or_str) + return super(SessionPage, self).s_eles(locator) def change_mode(self, mode=None, go=True, copy_cookies=True): """切换模式,接收's'或'd',除此以外的字符串会切换为 d 模式 @@ -384,9 +384,9 @@ class WebPageTab(SessionPage, ChromiumTab, BasePage): if self._response is not None: self._response.close() - def _find_elements(self, loc_or_ele, timeout=None, index=1, relative=False, raise_err=None): + def _find_elements(self, locator, timeout=None, index=1, relative=False, raise_err=None): """返回页面中符合条件的元素、属性或节点文本,默认返回第一个 - :param loc_or_ele: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 :param timeout: 查找元素超时时间(秒),d模式专用 :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 :param relative: WebPage用的表示是否相对定位的参数 @@ -394,9 +394,9 @@ class WebPageTab(SessionPage, ChromiumTab, BasePage): :return: 元素对象或属性、文本节点文本 """ if self._mode == 's': - return super()._find_elements(loc_or_ele, index=index) + return super()._find_elements(locator, index=index) elif self._mode == 'd': - return super(SessionPage, self)._find_elements(loc_or_ele, timeout=timeout, index=index, relative=relative) + return super(SessionPage, self)._find_elements(locator, timeout=timeout, index=index, relative=relative) def __repr__(self): return f'' diff --git a/DrissionPage/_pages/chromium_tab.pyi b/DrissionPage/_pages/chromium_tab.pyi index a837051..e4b55b8 100644 --- a/DrissionPage/_pages/chromium_tab.pyi +++ b/DrissionPage/_pages/chromium_tab.pyi @@ -80,7 +80,7 @@ class WebPageTab(SessionPage, ChromiumTab): self._has_session = ... def __call__(self, - loc_or_str: Union[Tuple[str, str], str, ChromiumElement, SessionElement], + locator: Union[Tuple[str, str], str, ChromiumElement, SessionElement], index: int = 1, timeout: float = None) -> Union[ChromiumElement, SessionElement, NoneElement]: ... @@ -150,19 +150,19 @@ class WebPageTab(SessionPage, ChromiumTab): cert: Any | None = ...) -> Union[bool, None]: ... def ele(self, - loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, SessionElement], + locator: Union[Tuple[str, str], str, ChromiumElement, SessionElement], index: int = 1, timeout: float = None) -> Union[ChromiumElement, SessionElement, NoneElement]: ... def eles(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], timeout: float = None) -> List[Union[ChromiumElement, SessionElement]]: ... def s_ele(self, - loc_or_ele: Union[Tuple[str, str], str] = None, + locator: Union[Tuple[str, str], str] = None, index: int = 1) -> Union[SessionElement, NoneElement]: ... - def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[SessionElement]: ... + def s_eles(self, locator: Union[Tuple[str, str], str]) -> List[SessionElement]: ... def change_mode(self, mode: str = None, go: bool = True, copy_cookies: bool = True) -> None: ... @@ -200,7 +200,7 @@ class WebPageTab(SessionPage, ChromiumTab): def set(self) -> WebPageTabSetter: ... def _find_elements(self, - loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, SessionElement, ChromiumFrame], + locator: Union[Tuple[str, str], str, ChromiumElement, SessionElement, ChromiumFrame], timeout: float = None, index: Optional[int] = 1, relative: bool = False, diff --git a/DrissionPage/_pages/session_page.py b/DrissionPage/_pages/session_page.py index 2065f96..38177b4 100644 --- a/DrissionPage/_pages/session_page.py +++ b/DrissionPage/_pages/session_page.py @@ -69,15 +69,15 @@ class SessionPage(BasePage): if not self._session: self._session, self._headers = self._session_options.make_session() - def __call__(self, loc_or_str, index=1, timeout=None): + def __call__(self, locator, index=1, timeout=None): """在内部查找元素 例:ele2 = ele1('@id=ele_id') - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :param timeout: 不起实际作用,用于和ChromiumElement对应,便于无差别调用 :return: SessionElement对象或属性文本 """ - return self.ele(loc_or_str, index=index) + return self.ele(locator, index=index) # -----------------共有属性和方法------------------- @property @@ -176,48 +176,47 @@ class SessionPage(BasePage): """ return self._s_connect(url, 'post', show_errmsg, retry, interval, **kwargs) - def ele(self, loc_or_ele, index=1, timeout=None): + def ele(self, locator, index=1, timeout=None): """返回页面中符合条件的一个元素、属性或节点文本 - :param loc_or_ele: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :param timeout: 不起实际作用,用于和ChromiumElement对应,便于无差别调用 :return: SessionElement对象或属性、文本 """ - return self._ele(loc_or_ele, index=index, method='ele()') + return self._ele(locator, index=index, method='ele()') - def eles(self, loc_or_str, timeout=None): + def eles(self, locator, timeout=None): """返回页面中所有符合条件的元素、属性或节点文本 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 不起实际作用,用于和ChromiumElement对应,便于无差别调用 :return: SessionElement对象或属性、文本组成的列表 """ - return self._ele(loc_or_str, index=None) + return self._ele(locator, index=None) - def s_ele(self, loc_or_ele=None, index=1): + def s_ele(self, locator=None, index=1): """返回页面中符合条件的一个元素、属性或节点文本 - :param loc_or_ele: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :return: SessionElement对象或属性、文本 """ - return make_session_ele(self.html) if loc_or_ele is None else self._ele(loc_or_ele, - index=index, method='s_ele()') + return make_session_ele(self.html) if locator is None else self._ele(locator, index=index, method='s_ele()') - def s_eles(self, loc_or_str): + def s_eles(self, locator): """返回页面中符合条件的所有元素、属性或节点文本 - :param loc_or_str: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 :return: SessionElement对象或属性、文本 """ - return self._ele(loc_or_str, index=None) + return self._ele(locator, index=None) - def _find_elements(self, loc_or_ele, timeout=None, index=1, raise_err=None): + def _find_elements(self, locator, timeout=None, index=1, raise_err=None): """返回页面中符合条件的元素、属性或节点文本,默认返回第一个 - :param loc_or_ele: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 :param timeout: 不起实际作用,用于和父类对应 :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 :param raise_err: 找不到元素是是否抛出异常,为None时根据全局设置 :return: SessionElement对象 """ - return loc_or_ele if isinstance(loc_or_ele, SessionElement) else make_session_ele(self, loc_or_ele, index=index) + return locator if isinstance(locator, SessionElement) else make_session_ele(self, locator, index=index) def get_cookies(self, as_dict=False, all_domains=False, all_info=False): """返回cookies diff --git a/DrissionPage/_pages/session_page.pyi b/DrissionPage/_pages/session_page.pyi index c134a85..15f9b24 100644 --- a/DrissionPage/_pages/session_page.pyi +++ b/DrissionPage/_pages/session_page.pyi @@ -41,7 +41,7 @@ class SessionPage(BasePage): def _create_session(self) -> None: ... def __call__(self, - loc_or_str: Union[Tuple[str, str], str, SessionElement], + locator: Union[Tuple[str, str], str, SessionElement], index: int = 1, timeout: float = None) -> Union[SessionElement, NoneElement]: ... @@ -91,22 +91,22 @@ class SessionPage(BasePage): cert: Any | None = ...) -> bool: ... def ele(self, - loc_or_ele: Union[Tuple[str, str], str, SessionElement], + locator: Union[Tuple[str, str], str, SessionElement], index: int = 1, timeout: float = None) -> Union[SessionElement, NoneElement]: ... def eles(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], timeout: float = None) -> List[SessionElement]: ... def s_ele(self, - loc_or_ele: Union[Tuple[str, str], str, SessionElement] = None, + locator: Union[Tuple[str, str], str, SessionElement] = None, index: int = 1) -> Union[SessionElement, NoneElement]: ... - def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[SessionElement]: ... + def s_eles(self, loc: Union[Tuple[str, str], str]) -> List[SessionElement]: ... def _find_elements(self, - loc_or_ele: Union[Tuple[str, str], str, SessionElement], + locator: Union[Tuple[str, str], str, SessionElement], timeout: float = None, index: Optional[int] = 1, raise_err: bool = None) \ diff --git a/DrissionPage/_pages/web_page.py b/DrissionPage/_pages/web_page.py index 95f2154..8fa0b15 100644 --- a/DrissionPage/_pages/web_page.py +++ b/DrissionPage/_pages/web_page.py @@ -51,18 +51,18 @@ class WebPage(SessionPage, ChromiumPage, BasePage): self._type = 'WebPage' self.change_mode(self._mode, go=False, copy_cookies=False) - def __call__(self, loc_or_str, index=1, timeout=None): + def __call__(self, locator, index=1, timeout=None): """在内部查找元素 例:ele = page('@id=ele_id') - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :param timeout: 超时时间(秒) :return: 子元素对象 """ if self._mode == 'd': - return super(SessionPage, self).__call__(loc_or_str, index=index, timeout=timeout) + return super(SessionPage, self).__call__(locator, index=index, timeout=timeout) elif self._mode == 's': - return super().__call__(loc_or_str, index=index) + return super().__call__(locator, index=index) @property def set(self): @@ -196,49 +196,49 @@ class WebPage(SessionPage, ChromiumPage, BasePage): return self.response return super().post(url, show_errmsg, retry, interval, **kwargs) - def ele(self, loc_or_ele, index=1, timeout=None): + def ele(self, locator, index=1, timeout=None): """返回第一个符合条件的元素、属性或节点文本 - :param loc_or_ele: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :param timeout: 查找元素超时时间(秒),默认与页面等待时间一致 :return: 元素对象或属性、文本节点文本 """ if self._mode == 's': - return super().ele(loc_or_ele, index=index) + return super().ele(locator, index=index) elif self._mode == 'd': - return super(SessionPage, self).ele(loc_or_ele, index=index, timeout=timeout) + return super(SessionPage, self).ele(locator, index=index, timeout=timeout) - def eles(self, loc_or_str, timeout=None): + def eles(self, locator, timeout=None): """返回页面中所有符合条件的元素、属性或节点文本 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 查找元素超时时间(秒),默认与页面等待时间一致 :return: 元素对象或属性、文本组成的列表 """ if self._mode == 's': - return super().eles(loc_or_str) + return super().eles(locator) elif self._mode == 'd': - return super(SessionPage, self).eles(loc_or_str, timeout=timeout) + return super(SessionPage, self).eles(locator, timeout=timeout) - def s_ele(self, loc_or_ele=None, index=1): + def s_ele(self, locator=None, index=1): """查找第一个符合条件的元素以SessionElement形式返回,d模式处理复杂页面时效率很高 - :param loc_or_ele: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :param index: 获取第几个,从1开始,可传入负数获取倒数第几个 :return: SessionElement对象或属性、文本 """ if self._mode == 's': - return super().s_ele(loc_or_ele, index=index) + return super().s_ele(locator, index=index) elif self._mode == 'd': - return super(SessionPage, self).s_ele(loc_or_ele, index=index) + return super(SessionPage, self).s_ele(locator, index=index) - def s_eles(self, loc_or_str): + def s_eles(self, locator): """查找所有符合条件的元素以SessionElement形式返回,d模式处理复杂页面时效率很高 - :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是loc元组,或查询字符串 :return: SessionElement对象或属性、文本组成的列表 """ if self._mode == 's': - return super().s_eles(loc_or_str) + return super().s_eles(locator) elif self._mode == 'd': - return super(SessionPage, self).s_eles(loc_or_str) + return super(SessionPage, self).s_eles(locator) def change_mode(self, mode=None, go=True, copy_cookies=True): """切换模式,接收's'或'd',除此以外的字符串会切换为 d 模式 @@ -376,9 +376,9 @@ class WebPage(SessionPage, ChromiumPage, BasePage): if self._response is not None: self._response.close() - def _find_elements(self, loc_or_ele, timeout=None, index=1, relative=False, raise_err=None): + def _find_elements(self, locator, timeout=None, index=1, relative=False, raise_err=None): """返回页面中符合条件的元素、属性或节点文本,默认返回第一个 - :param loc_or_ele: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 + :param locator: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 :param timeout: 查找元素超时时间,d模式专用 :param index: 第几个结果,从1开始,可传入负数获取倒数第几个,为None返回所有 :param relative: WebPage用的表示是否相对定位的参数 @@ -386,9 +386,9 @@ class WebPage(SessionPage, ChromiumPage, BasePage): :return: 元素对象或属性、文本节点文本 """ if self._mode == 's': - return super()._find_elements(loc_or_ele, index=index) + return super()._find_elements(locator, index=index) elif self._mode == 'd': - return super(SessionPage, self)._find_elements(loc_or_ele, timeout=timeout, index=index, relative=relative) + return super(SessionPage, self)._find_elements(locator, timeout=timeout, index=index, relative=relative) def quit(self, timeout=5, force=True): """关闭浏览器和Session diff --git a/DrissionPage/_pages/web_page.pyi b/DrissionPage/_pages/web_page.pyi index 607d130..517dfd3 100644 --- a/DrissionPage/_pages/web_page.pyi +++ b/DrissionPage/_pages/web_page.pyi @@ -37,7 +37,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): self._chromium_options: Union[ChromiumOptions, None] = ... def __call__(self, - loc_or_str: Union[Tuple[str, str], str, ChromiumElement, SessionElement], + locator: Union[Tuple[str, str], str, ChromiumElement, SessionElement], index: int = 1, timeout: float = None) -> Union[ChromiumElement, SessionElement, NoneElement]: ... @@ -105,19 +105,19 @@ class WebPage(SessionPage, ChromiumPage, BasePage): cert: Any | None = ...) -> Union[bool, None]: ... def ele(self, - loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, SessionElement], + locator: Union[Tuple[str, str], str, ChromiumElement, SessionElement], index: int = 1, timeout: float = None) -> Union[ChromiumElement, SessionElement, NoneElement]: ... def eles(self, - loc_or_str: Union[Tuple[str, str], str], + locator: Union[Tuple[str, str], str], timeout: float = None) -> List[Union[ChromiumElement, SessionElement]]: ... def s_ele(self, - loc_or_ele: Union[Tuple[str, str], str] = None, + locator: Union[Tuple[str, str], str] = None, index: int = 1) -> Union[SessionElement, NoneElement]: ... - def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[SessionElement]: ... + def s_eles(self, locator: Union[Tuple[str, str], str]) -> List[SessionElement]: ... def change_mode(self, mode: str = None, go: bool = True, copy_cookies: bool = True) -> None: ... @@ -169,7 +169,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def set(self) -> WebPageSetter: ... def _find_elements(self, - loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, SessionElement, ChromiumFrame], + locator: Union[Tuple[str, str], str, ChromiumElement, SessionElement, ChromiumFrame], timeout: float = None, index: Optional[int] = 1, relative: bool = False, diff --git a/DrissionPage/_units/selector.py b/DrissionPage/_units/selector.py index 9e7f721..3f9b08e 100644 --- a/DrissionPage/_units/selector.py +++ b/DrissionPage/_units/selector.py @@ -103,13 +103,13 @@ class SelectElement(object): """ return self._select(index, 'index', False, timeout) - def by_loc(self, loc, timeout=None): + def by_locator(self, locator, timeout=None): """用定位符选择指定的项 - :param loc: 定位符 + :param locator: 定位符 :param timeout: 超时时间 :return: 是否选择成功 """ - return self._by_loc(loc, timeout) + return self._by_loc(locator, timeout) def by_option(self, option): """选中单个或多个option元素 @@ -142,13 +142,13 @@ class SelectElement(object): """ return self._select(index, 'index', True, timeout) - def cancel_by_loc(self, loc, timeout=None): + def cancel_by_locator(self, locator, timeout=None): """用定位符取消选择指定的项 - :param loc: 定位符 + :param locator: 定位符 :param timeout: 超时时间 :return: 是否选择成功 """ - return self._by_loc(loc, timeout, True) + return self._by_loc(locator, timeout, True) def cancel_by_option(self, option): """取消选中单个或多个option元素 diff --git a/DrissionPage/_units/selector.pyi b/DrissionPage/_units/selector.pyi index 8e74554..06f0553 100644 --- a/DrissionPage/_units/selector.pyi +++ b/DrissionPage/_units/selector.pyi @@ -38,7 +38,7 @@ class SelectElement(object): def by_index(self, index: Union[int, list, tuple], timeout: float = None) -> bool: ... - def by_loc(self, loc: Union[str, Tuple[str, str]], timeout: float = None) -> bool: ... + def by_locator(self, locator: Union[str, Tuple[str, str]], timeout: float = None) -> bool: ... def by_option(self, option: Union[ChromiumElement, List[ChromiumElement], Tuple[ChromiumElement]]) -> None: ... @@ -48,7 +48,7 @@ class SelectElement(object): def cancel_by_index(self, index: Union[int, list, tuple], timeout: float = None) -> bool: ... - def cancel_by_loc(self, loc: Union[str, Tuple[str, str]], timeout: float = None) -> bool: ... + def cancel_by_locator(self, locator: Union[str, Tuple[str, str]], timeout: float = None) -> bool: ... def cancel_by_option(self, option: Union[ChromiumElement, List[ChromiumElement], Tuple[ChromiumElement]]) -> None: ... diff --git a/DrissionPage/_units/waiter.py b/DrissionPage/_units/waiter.py index 52ff58c..09867c9 100644 --- a/DrissionPage/_units/waiter.py +++ b/DrissionPage/_units/waiter.py @@ -73,14 +73,14 @@ class BaseWaiter(object): return False return ele.wait.hidden(timeout, raise_err=raise_err) - def ele_loaded(self, loc, timeout=None, raise_err=None): + def ele_loaded(self, locator, timeout=None, raise_err=None): """等待元素加载到DOM - :param loc: 要等待的元素,输入定位符 + :param locator: 要等待的元素,输入定位符 :param timeout: 超时时间,默认读取页面超时时间 :param raise_err: 等待失败时是否报错,为None时根据Settings设置 :return: 成功返回元素对象,失败返回False """ - ele = self._driver._ele(loc, raise_err=False, timeout=timeout) + ele = self._driver._ele(locator, raise_err=False, timeout=timeout) if ele: return ele if raise_err is True or Settings.raise_when_wait_failed is True: diff --git a/DrissionPage/_units/waiter.pyi b/DrissionPage/_units/waiter.pyi index a04dd9a..c5eb2a9 100644 --- a/DrissionPage/_units/waiter.pyi +++ b/DrissionPage/_units/waiter.pyi @@ -34,7 +34,7 @@ class BaseWaiter(object): raise_err: bool = None) -> bool: ... def ele_loaded(self, - loc: Union[str, tuple], + locator: Union[str, tuple], timeout: float = None, raise_err: bool = None) -> Union[bool, ChromiumElement]: ...