优化元素筛选,未完成

This commit is contained in:
g1879 2024-06-19 23:33:42 +08:00
parent 5ffff60118
commit 5221ea3cb0
2 changed files with 34 additions and 6 deletions

View File

@ -120,6 +120,11 @@ class ChromiumElementsList(SessionElementsList):
class SessionFilterOne(object):
def __init__(self, _list):
self._list = _list
self._index = 1
def __call__(self, index=1):
self._index = index
return self
def attr(self, name, value, equal=True):
"""以是否拥有某个attribute值为条件筛选元素
@ -138,15 +143,21 @@ class SessionFilterOne(object):
:return: 筛选结果
"""
if contain:
num = 0
for i in self._list:
t = i.raw_text
if (fuzzy and text in t) or (not fuzzy and text == t):
return i
num += 1
if self._index == num:
return i
else:
num = 0
for i in self._list:
t = i.raw_text
if (fuzzy and text not in t) or (not fuzzy and text != t):
return i
num += 1
if self._index == num:
return i
return NoneElement(self._list._page, 'text()', args={'text': text, 'fuzzy': fuzzy, 'contain': contain})
def _get_attr(self, name, value, method, equal=True):
@ -157,13 +168,19 @@ class SessionFilterOne(object):
:return: 筛选结果
"""
if equal:
num = 0
for i in self._list:
if getattr(i, method)(name) == value:
return i
num += 1
if self._index == num:
return i
else:
num = 0
for i in self._list:
if getattr(i, method)(name) != value:
return i
num += 1
if self._index == num:
return i
return NoneElement(self._list._page, f'{method}()', args={'name': name, 'value': value, 'equal': equal})
@ -296,13 +313,19 @@ class ChromiumFilterOne(SessionFilterOne):
:return: 选中的元素
"""
if equal:
num = 0
for i in self._list:
if getattr(i.states, name):
return i
num += 1
if self._index == num:
return i
else:
num = 0
for i in self._list:
if not getattr(i.states, name):
return i
num += 1
if self._index == num:
return i
return NoneElement(self._list._page, f'{name}()', args={'equal': equal})

View File

@ -72,9 +72,12 @@ class ChromiumElementsList(SessionElementsList):
class SessionFilterOne(object):
_list: SessionElementsList = ...
_index: int = ...
def __init__(self, _list: SessionElementsList, index: int = 1): ...
def __call__(self, index: int = 1) -> SessionFilterOne: ...
def attr(self, name: str, value: str, equal: bool = True) -> SessionElement: ...
def text(self, text: str, fuzzy: bool = True, contain: bool = True) -> SessionElement: ...
@ -115,6 +118,8 @@ class ChromiumFilterOne(SessionFilterOne):
def __init__(self, _list: ChromiumElementsList): ...
def __call__(self, index: int = 1) -> ChromiumFilterOne: ...
def displayed(self, equal: bool = True) -> ChromiumElement: ...
def checked(self, equal: bool = True) -> ChromiumElement: ...