4.0.3.5统一各locator参数名称;select.by_loc()改成by.locator()

This commit is contained in:
g1879 2024-01-22 17:13:10 +08:00
parent 8aae35d31b
commit 9e4b39be55
21 changed files with 483 additions and 479 deletions

View File

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

View File

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

View File

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

View File

@ -82,13 +82,13 @@ class ChromiumElement(DrissionElement):
attrs = [f"{attr}='{attrs[attr]}'" for attr in attrs]
return f'<ChromiumElement {self.tag} {" ".join(attrs)}>'
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'<ShadowRoot in {self.parent_ele}>'
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('/'):

View File

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

View File

@ -37,14 +37,14 @@ class SessionElement(DrissionElement):
attrs = [f"{attr}='{self.attrs[attr]}'" for attr in self.attrs]
return f'<SessionElement {self.tag} {" ".join(attrs)}>'
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路径

View File

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

View File

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

View File

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

View File

@ -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是否同域"""

View File

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

View File

@ -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'<WebPageTab browser_id={self.browser.id} tab_id={self.tab_id}>'

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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]: ...