改进浏览器eles()筛选功能

This commit is contained in:
g1879 2024-06-19 17:48:48 +08:00
parent fa9e2ae7d7
commit 143bd191c1
2 changed files with 282 additions and 213 deletions

View File

@ -36,10 +36,9 @@ class ElementsList(list):
self._getter = Getter(self) self._getter = Getter(self)
return self._getter return self._getter
def search(self, get_all=False, displayed=None, checked=None, selected=None, enabled=None, clickable=None, def search(self, displayed=None, checked=None, selected=None, enabled=None, clickable=None,
have_rect=None, have_text=None): have_rect=None, have_text=None):
"""或关系筛选元素 """或关系筛选元素
:param get_all: 是否返回所有筛选到的元素
:param displayed: 是否显示boolNone为忽略该项 :param displayed: 是否显示boolNone为忽略该项
:param checked: 是否被选中boolNone为忽略该项 :param checked: 是否被选中boolNone为忽略该项
:param selected: 是否被选择boolNone为忽略该项 :param selected: 是否被选择boolNone为忽略该项
@ -49,8 +48,7 @@ class ElementsList(list):
:param have_text: 是否含有文本boolNone为忽略该项 :param have_text: 是否含有文本boolNone为忽略该项
:return: 筛选结果 :return: 筛选结果
""" """
if get_all: r = ElementsList(self._page)
r = ElementsList()
for i in self: for i in self:
if ((displayed is not None and (displayed is True and i.states.is_displayed) or ( if ((displayed is not None and (displayed is True and i.states.is_displayed) or (
displayed is False and not i.states.is_displayed)) displayed is False and not i.states.is_displayed))
@ -67,8 +65,20 @@ class ElementsList(list):
or (have_text is not None and (have_text is True and i.raw_text) or ( or (have_text is not None and (have_text is True and i.raw_text) or (
have_text is False and not i.raw_text))): have_text is False and not i.raw_text))):
r.append(i) r.append(i)
return r return Filter(r)
def search_one(self, displayed=None, checked=None, selected=None, enabled=None, clickable=None,
have_rect=None, have_text=None):
"""或关系筛选元素
:param displayed: 是否显示boolNone为忽略该项
:param checked: 是否被选中boolNone为忽略该项
:param selected: 是否被选择boolNone为忽略该项
:param enabled: 是否可用boolNone为忽略该项
:param clickable: 是否可点击boolNone为忽略该项
:param have_rect: 是否拥有大小和位置boolNone为忽略该项
:param have_text: 是否含有文本boolNone为忽略该项
:return: 筛选结果
"""
for i in self: for i in self:
if ((displayed is not None and (displayed is True and i.states.is_displayed) or ( if ((displayed is not None and (displayed is True and i.states.is_displayed) or (
displayed is False and not i.states.is_displayed)) displayed is False and not i.states.is_displayed))
@ -86,7 +96,7 @@ class ElementsList(list):
have_text is False and not i.raw_text))): have_text is False and not i.raw_text))):
return i return i
return NoneElement(self._page, method='filter()', args={'get_all': get_all, 'displayed': displayed, return NoneElement(self._page, method='filter()', args={'displayed': displayed,
'checked': checked, 'selected': selected, 'checked': checked, 'selected': selected,
'enabled': enabled, 'clickable': clickable, 'enabled': enabled, 'clickable': clickable,
'have_rect': have_rect, 'have_text': have_text}) 'have_rect': have_rect, 'have_text': have_text})
@ -96,82 +106,79 @@ class BaseFilter(object):
def __init__(self, _list): def __init__(self, _list):
self._list = _list self._list = _list
def displayed(self): def displayed(self, equal=True):
"""返回显示的元素""" """以是否显示为条件筛选元素
return self._any_state('is_displayed') :param equal: 是否匹配显示的元素False匹配不显示的
def hidden(self):
"""返回不显示的元素"""
return self._any_state('is_displayed', True)
def checked(self):
"""返回被选中的元素"""
return self._any_state('is_checked')
def not_checked(self):
"""返回没被选中的元素"""
return self._any_state('is_checked', True)
def selected(self):
"""返回被选中的列表元素"""
return self._any_state('is_selected')
def not_selected(self):
"""返回没被选中的列表元素"""
return self._any_state('is_selected', True)
def enabled(self):
"""返回有效的元素"""
return self._any_state('is_enabled')
def disabled(self):
"""返回无效的元素"""
return self._any_state('is_enabled', True)
def clickable(self):
"""返回可被点击的元素"""
return self._any_state('is_clickable')
def not_clickable(self):
"""返回不可被点击的元素"""
return self._any_state('is_clickable', True)
def have_rect(self):
"""返回有大小和位置的元素"""
return self._any_state('has_rect')
def no_rect(self):
"""返回没有大小和位置的元素"""
return self._any_state('has_rect', True)
def style(self, name, value):
"""返回拥有某个style值的元素
:param name: 属性名称
:param value: 属性值
:return: 筛选结果 :return: 筛选结果
""" """
return self._get_attr(name, value, 'style') return self._any_state('is_displayed', equal=equal)
def property(self, name, value): def checked(self, equal=True):
"""返回拥有某个property值的元素 """以是否被选中为条件筛选元素
:param name: 属性名称 :param equal: 是否匹配被选中的元素False匹配不被选中的
:param value: 属性值
:return: 筛选结果 :return: 筛选结果
""" """
return self._get_attr(name, value, 'property') return self._any_state('is_checked', equal=equal)
def attr(self, name, value): def selected(self, equal=True):
"""返回拥有某个attribute值的元素 """以是否被选择为条件筛选元素,用于<select>元素项目
:param name: 属性名称 :param equal: 是否匹配被选择的元素False匹配不被选择的
:param value: 属性值
:return: 筛选结果 :return: 筛选结果
""" """
return self._get_attr(name, value, 'attr') return self._any_state('is_selected', equal=equal)
def _get_attr(self, name, value, method): def enabled(self, equal=True):
"""以是否可用为条件筛选元素
:param equal: 是否匹配可用的元素False表示匹配disabled状态的
:return: 筛选结果
"""
return self._any_state('is_enabled', equal=equal)
def clickable(self, equal=True):
"""以是否可点击为条件筛选元素
:param equal: 是否匹配可点击的元素False表示匹配不是可点击的
:return: 筛选结果
"""
return self._any_state('is_clickable', equal=equal)
def have_rect(self, equal=True):
"""以是否有大小为条件筛选元素
:param equal: 是否匹配有大小的元素False表示匹配没有大小的
:return: 筛选结果
"""
return self._any_state('has_rect', equal=equal)
def style(self, name, value, equal=True):
"""以是否拥有某个style值为条件筛选元素
:param name: 属性名称
:param value: 属性值
:param equal: True表示匹配name值为value值的元素False表示匹配name值不为value值的
:return: 筛选结果
"""
return self._get_attr(name, value, 'style', equal=equal)
def property(self, name, value, equal=True):
"""以是否拥有某个property值为条件筛选元素
:param name: 属性名称
:param value: 属性值
:param equal: True表示匹配name值为value值的元素False表示匹配name值不为value值的
:return: 筛选结果
"""
return self._get_attr(name, value, 'property', equal=equal)
def attr(self, name, value, equal=True):
"""以是否拥有某个attribute值为条件筛选元素
:param name: 属性名称
:param value: 属性值
:param equal: True表示匹配name值为value值的元素False表示匹配name值不为value值的
:return: 筛选结果
"""
return self._get_attr(name, value, 'attr', equal=equal)
def _get_attr(self, name, value, method, equal=True):
pass pass
def _any_state(self, name, is_not=False): def _any_state(self, name, equal=True):
pass pass
@ -188,43 +195,92 @@ class Filter(BaseFilter):
"""返回用于获取元素属性的对象""" """返回用于获取元素属性的对象"""
return self._list.get return self._list.get
def have_text(self): @property
"""返回包含文本的元素""" def search_one(self, displayed=None, checked=None, selected=None, enabled=None, clickable=None,
r = ElementsList() have_rect=None, have_text=None):
"""或关系筛选元素
:param displayed: 是否显示boolNone为忽略该项
:param checked: 是否被选中boolNone为忽略该项
:param selected: 是否被选择boolNone为忽略该项
:param enabled: 是否可用boolNone为忽略该项
:param clickable: 是否可点击boolNone为忽略该项
:param have_rect: 是否拥有大小和位置boolNone为忽略该项
:param have_text: 是否含有文本boolNone为忽略该项
:return: 筛选结果
"""
return self._list.search_one(displayed=displayed, checked=checked, selected=selected, enabled=enabled,
clickable=clickable, have_rect=have_rect, have_text=have_text)
@property
def search(self, displayed=None, checked=None, selected=None, enabled=None, clickable=None,
have_rect=None, have_text=None):
"""或关系筛选元素
:param displayed: 是否显示boolNone为忽略该项
:param checked: 是否被选中boolNone为忽略该项
:param selected: 是否被选择boolNone为忽略该项
:param enabled: 是否可用boolNone为忽略该项
:param clickable: 是否可点击boolNone为忽略该项
:param have_rect: 是否拥有大小和位置boolNone为忽略该项
:param have_text: 是否含有文本boolNone为忽略该项
:return: 筛选结果
"""
return self._list.search(displayed=displayed, checked=checked, selected=selected, enabled=enabled,
clickable=clickable, have_rect=have_rect, have_text=have_text)
def text(self, text, fuzzy=True, contain=True):
"""以是否含有指定文本为条件筛选元素
:param text: 用于匹配的文本
:param fuzzy: 是否模糊匹配
:param contain: 是否包含该字符串False表示不包含
:return: 筛选结果
"""
r = ElementsList(self._list._page)
if contain:
for i in self._list: for i in self._list:
if i.raw_text: t = i.raw_text
if (fuzzy and text in t) or (not fuzzy and text == t):
r.append(i)
else:
for i in self._list:
t = i.raw_text
if (fuzzy and text not in t) or (not fuzzy and text != t):
r.append(i) r.append(i)
self._list = r self._list = r
return self return self
def _get_attr(self, name, value, method): def _get_attr(self, name, value, method, equal=True):
"""返回通过某个方法可获得某个值的元素 """返回通过某个方法可获得某个值的元素
:param name: 属性名称 :param name: 属性名称
:param value: 属性值 :param value: 属性值
:param method: 方法名称 :param method: 方法名称
:return: 筛选结果 :return: 筛选结果
""" """
r = ElementsList() r = ElementsList(self._list._page)
if equal:
for i in self._list: for i in self._list:
if getattr(i, method)(name) == value: if getattr(i, method)(name) == value:
r.append(i) r.append(i)
else:
for i in self._list:
if getattr(i, method)(name) != value:
r.append(i)
self._list = r self._list = r
return self return self
def _any_state(self, name, is_not=False): def _any_state(self, name, equal=True):
""" """
:param name: 状态名称 :param name: 状态名称
:param is_not: 是否选择否定的 :param equal: 是否是指定状态False表示否定状态
:return: 选中的列表 :return: 选中的列表
""" """
r = ElementsList() r = ElementsList(self._list._page)
if is_not: if equal:
for i in self._list: for i in self._list:
if not getattr(i.states, name): if getattr(i.states, name):
r.append(i) r.append(i)
else: else:
for i in self._list: for i in self._list:
if getattr(i.states, name): if not getattr(i.states, name):
r.append(i) r.append(i)
self._list = r self._list = r
return self return self
@ -232,41 +288,58 @@ class Filter(BaseFilter):
class FilterOne(BaseFilter): class FilterOne(BaseFilter):
def have_text(self): def text(self, text, fuzzy=True, contain=True):
"""返回包含文本的元素""" """以是否含有指定文本为条件筛选元素
:param text: 用于匹配的文本
:param fuzzy: 是否模糊匹配
:param contain: 是否包含该字符串False表示不包含
:return: 选中的元素
"""
if contain:
for i in self._list: for i in self._list:
if i.raw_text: t = i.raw_text
if (fuzzy and text in t) or (not fuzzy and text == t):
return i return i
return NoneElement(self._list._page, method='have_text()') else:
for i in self._list:
t = i.raw_text
if (fuzzy and text not in t) or (not fuzzy and text != t):
return i
return NoneElement(self._list._page, 'text()', args={'text': text, 'fuzzy': fuzzy, 'contain': contain})
def _get_attr(self, name, value, method): def _get_attr(self, name, value, method, equal=True):
"""返回通过某个方法可获得某个值的元素 """返回通过某个方法可获得某个值的元素
:param name: 属性名称 :param name: 属性名称
:param value: 属性值 :param value: 属性值
:param method: 方法名称 :param method: 方法名称
:return: 筛选结果 :param equal: 是否是指定状态False表示否定状态
:return: 选中的元素
""" """
if equal:
for i in self._list: for i in self._list:
if getattr(i, method)(name) == value: if getattr(i, method)(name) == value:
return i return i
else:
for i in self._list:
if getattr(i, method)(name) != value:
return i
return NoneElement(self._list._page, f'{method}()', args={'name': name, 'value': value}) return NoneElement(self._list._page, f'{method}()', args={'name': name, 'value': value})
def _any_state(self, name, is_not=False): def _any_state(self, name, equal=True):
""" """
:param name: 状态名称 :param name: 状态名称
:param is_not: 是否选择否定的 :param equal: 是否是指定状态False表示否定状态
:return: 选中的列表 :return: 选中的元素
""" """
if is_not: if equal:
for i in self._list:
if not getattr(i.states, name):
return i
else:
for i in self._list: for i in self._list:
if getattr(i.states, name): if getattr(i.states, name):
return i return i
else:
return NoneElement(self._list._page, f'{name}()', args={'name': name, 'is_not': is_not}) for i in self._list:
if not getattr(i.states, name):
return i
return NoneElement(self._list._page, f'{name}()', args={'equal': equal})
class Getter(object): class Getter(object):
@ -274,12 +347,18 @@ class Getter(object):
self._list = _list self._list = _list
def links(self): def links(self):
"""返回所有元素的link属性组成的列表"""
return [e.link for e in self._list] return [e.link for e in self._list]
def texts(self): def texts(self):
"""返回所有元素的text属性组成的列表"""
return [e.text for e in self._list] return [e.text for e in self._list]
def attrs(self, name): def attrs(self, name):
"""返回所有元素指定的attr属性组成的列表
:param name: 属性名称
:return: 属性文本组成的列表
"""
return [e.attr(name) for e in self._list] return [e.attr(name) for e in self._list]

View File

@ -34,14 +34,22 @@ class ElementsList(list):
def get(self) -> Getter: ... def get(self) -> Getter: ...
def search(self, def search(self,
get_all: bool = False,
displayed: Optional[bool] = None, displayed: Optional[bool] = None,
checked: Optional[bool] = None, checked: Optional[bool] = None,
selected: Optional[bool] = None, selected: Optional[bool] = None,
enabled: Optional[bool] = None, enabled: Optional[bool] = None,
clickable: Optional[bool] = None, clickable: Optional[bool] = None,
have_rect: Optional[bool] = None, have_rect: Optional[bool] = None,
have_text: Optional[bool] = None) -> Union[ElementsList, ChromiumElement]: ... have_text: Optional[bool] = None) -> Filter: ...
def search_one(self,
displayed: Optional[bool] = None,
checked: Optional[bool] = None,
selected: Optional[bool] = None,
enabled: Optional[bool] = None,
clickable: Optional[bool] = None,
have_rect: Optional[bool] = None,
have_text: Optional[bool] = None) -> ChromiumElement: ...
def __next__(self) -> ChromiumElement: ... def __next__(self) -> ChromiumElement: ...
@ -51,46 +59,34 @@ class BaseFilter(object):
def __init__(self, _list: ElementsList): ... def __init__(self, _list: ElementsList): ...
def displayed(self): ... def displayed(self, equal: bool = True): ...
def hidden(self): ... def checked(self, equal: bool = True): ...
def checked(self): ... def selected(self, equal: bool = True): ...
def not_checked(self): ... def enabled(self, equal: bool = True): ...
def selected(self): ... def clickable(self, equal: bool = True): ...
def not_selected(self): ... def have_rect(self, equal: bool = True): ...
def enabled(self): ... def style(self, name: str, value: str, equal: bool = True): ...
def disabled(self): ...
def clickable(self): ...
def not_clickable(self): ...
def have_rect(self): ...
def no_rect(self): ...
# def have_text(self): ...
def style(self, name: str, value: str): ...
def property(self, def property(self,
name: str, name: str,
value: str): ... value: str,
equal: bool = True): ...
def attr(self, name: str, value: str): ... def attr(self, name: str, value: str, equal: bool = True): ...
def _get_attr(self, def _get_attr(self,
name: str, name: str,
value: str, value: str,
method: str): ... method: str,
equal: bool = True): ...
def _any_state(self, name: str, is_not: bool = False): ... def _any_state(self, name: str, equal: bool = True): ...
class Filter: class Filter:
@ -105,46 +101,52 @@ class Filter:
@property @property
def get(self) -> Getter: ... def get(self) -> Getter: ...
def displayed(self) -> Filter: ... def displayed(self, equal: bool = True) -> Filter: ...
def hidden(self) -> Filter: ... def checked(self, equal: bool = True) -> Filter: ...
def checked(self) -> Filter: ... def selected(self, equal: bool = True) -> Filter: ...
def not_checked(self) -> Filter: ... def enabled(self, equal: bool = True) -> Filter: ...
def selected(self) -> Filter: ... def clickable(self, equal: bool = True) -> Filter: ...
def not_selected(self) -> Filter: ... def have_rect(self, equal: bool = True) -> Filter: ...
def enabled(self) -> Filter: ... def style(self, name: str, value: str, equal: bool = True) -> Filter: ...
def disabled(self) -> Filter: ...
def clickable(self) -> Filter: ...
def not_clickable(self) -> Filter: ...
def have_rect(self) -> Filter: ...
def no_rect(self) -> Filter: ...
def have_text(self) -> Filter: ...
def style(self, name: str, value: str) -> Filter: ...
def property(self, def property(self,
name: str, name: str,
value: str) -> Filter: ... value: str, equal: bool = True) -> Filter: ...
def attr(self, name: str, value: str) -> Filter: ... def attr(self, name: str, value: str, equal: bool = True) -> Filter: ...
def text(self, text: str, fuzzy: bool = True, contain: bool = True) -> Filter: ...
def search(self,
displayed: Optional[bool] = None,
checked: Optional[bool] = None,
selected: Optional[bool] = None,
enabled: Optional[bool] = None,
clickable: Optional[bool] = None,
have_rect: Optional[bool] = None,
have_text: Optional[bool] = None) -> Filter: ...
def search_one(self,
displayed: Optional[bool] = None,
checked: Optional[bool] = None,
selected: Optional[bool] = None,
enabled: Optional[bool] = None,
clickable: Optional[bool] = None,
have_rect: Optional[bool] = None,
have_text: Optional[bool] = None) -> ChromiumElement: ...
def _get_attr(self, def _get_attr(self,
name: str, name: str,
value: str, value: str,
method: str) -> Filter: ... method: str, equal: bool = True) -> Filter: ...
def _any_state(self, name: str, is_not: bool = False) -> Filter: ... def _any_state(self, name: str, equal: bool = True) -> Filter: ...
class FilterOne(BaseFilter): class FilterOne(BaseFilter):
@ -152,46 +154,34 @@ class FilterOne(BaseFilter):
def __init__(self, _list: ElementsList): ... def __init__(self, _list: ElementsList): ...
def displayed(self) -> ChromiumElement: ... def displayed(self, equal: bool = True) -> ChromiumElement: ...
def hidden(self) -> ChromiumElement: ... def checked(self, equal: bool = True) -> ChromiumElement: ...
def checked(self) -> ChromiumElement: ... def selected(self, equal: bool = True) -> ChromiumElement: ...
def not_checked(self) -> ChromiumElement: ... def enabled(self, equal: bool = True) -> ChromiumElement: ...
def selected(self) -> ChromiumElement: ... def clickable(self, equal: bool = True) -> ChromiumElement: ...
def not_selected(self) -> ChromiumElement: ... def have_rect(self, equal: bool = True) -> ChromiumElement: ...
def enabled(self) -> ChromiumElement: ... def style(self, name: str, value: str, equal: bool = True) -> ChromiumElement: ...
def disabled(self) -> ChromiumElement: ...
def clickable(self) -> ChromiumElement: ...
def not_clickable(self) -> ChromiumElement: ...
def have_rect(self) -> ChromiumElement: ...
def no_rect(self) -> ChromiumElement: ...
def have_text(self) -> ChromiumElement: ...
def style(self, name: str, value: str) -> ChromiumElement: ...
def property(self, def property(self,
name: str, name: str,
value: str) -> ChromiumElement: ... value: str, equal: bool = True) -> ChromiumElement: ...
def attr(self, name: str, value: str) -> ChromiumElement: ... def attr(self, name: str, value: str, equal: bool = True) -> ChromiumElement: ...
def text(self, text: str, fuzzy: bool = True, contain: bool = True) -> ChromiumElement: ...
def _get_attr(self, def _get_attr(self,
name: str, name: str,
value: str, value: str,
method: str) -> ChromiumElement: ... method: str, equal: bool = True) -> ChromiumElement: ...
def _any_state(self, name: str, is_not: bool = False) -> ChromiumElement: ... def _any_state(self, name: str, equal: bool = True) -> ChromiumElement: ...
class Getter(object): class Getter(object):
@ -199,8 +189,8 @@ class Getter(object):
def __init__(self, _list: ElementsList): ... def __init__(self, _list: ElementsList): ...
def links(self) -> list: ... def links(self) -> List[str]: ...
def texts(self) -> list: ... def texts(self) -> List[str]: ...
def attrs(self, name) -> list: ... def attrs(self, name: str) -> List[str]: ...