优化类型注解

This commit is contained in:
g1879 2022-01-20 22:14:19 +08:00
parent 7b8f6ebe03
commit 2be7ed6154
6 changed files with 61 additions and 48 deletions

View File

@ -17,7 +17,7 @@ from selenium.webdriver.support.wait import WebDriverWait
from .base import DrissionElement, BaseElement
from .common import str_to_loc, get_usable_path, format_html, get_ele_txt, get_loc
from .session_element import make_session_ele
from .session_element import make_session_ele, SessionElement
class DriverElement(DrissionElement):
@ -38,7 +38,7 @@ class DriverElement(DrissionElement):
def __call__(self,
loc_or_str: Union[Tuple[str, str], str],
timeout: float = None):
timeout: float = None) -> Union['DriverElement', str, None]:
"""在内部查找元素 \n
ele2 = ele1('@id=ele_id') \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
@ -110,7 +110,7 @@ class DriverElement(DrissionElement):
def ele(self,
loc_or_str: Union[Tuple[str, str], str],
timeout: float = None):
timeout: float = None) -> Union['DriverElement', str, None]:
"""返回当前元素下级符合条件的第一个元素、属性或节点文本 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:param timeout: 查找元素超时时间默认与元素所在页面等待时间一致
@ -120,7 +120,7 @@ class DriverElement(DrissionElement):
def eles(self,
loc_or_str: Union[Tuple[str, str], str],
timeout: float = None):
timeout: float = None) -> List[Union['DriverElement', str]]:
"""返回当前元素下级所有符合条件的子元素、属性或节点文本 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:param timeout: 查找元素超时时间默认与元素所在页面等待时间一致
@ -128,14 +128,14 @@ class DriverElement(DrissionElement):
"""
return self._ele(loc_or_str, timeout=timeout, single=False)
def s_ele(self, loc_or_str: Union[Tuple[str, str], str] = None):
def s_ele(self, loc_or_str: Union[Tuple[str, str], str] = None) -> Union[SessionElement, str, None]:
"""查找第一个符合条件的元素以SessionElement形式返回处理复杂页面时效率很高 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:return: SessionElement对象或属性文本
"""
return make_session_ele(self, loc_or_str)
def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = None):
def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = None) -> List[Union[SessionElement, str]]:
"""查找所有符合条件的元素以SessionElement列表形式返回 \n
:param loc_or_str: 定位符
:return: SessionElement或属性文本组成的列表
@ -145,7 +145,7 @@ class DriverElement(DrissionElement):
def _ele(self,
loc_or_str: Union[Tuple[str, str], str],
timeout: float = None,
single: bool = True):
single: bool = True) -> Union['DriverElement', str, None, List[Union['DriverElement', str]]]:
"""返回当前元素下级符合条件的子元素、属性或节点文本,默认返回第一个 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:param timeout: 查找元素超时时间
@ -738,7 +738,7 @@ class DriverElement(DrissionElement):
def make_driver_ele(page_or_ele,
loc: Union[str, Tuple[str, str]],
single: bool = True,
timeout: float = None) -> Union[DriverElement, List[DriverElement], str, None]:
timeout: float = None) -> Union[DriverElement, str, None, List[Union[DriverElement, str]]]:
"""执行driver模式元素的查找 \n
页面查找元素及元素查找下级元素皆使用此方法 \n
:param page_or_ele: DriverPage对象或DriverElement对象

View File

@ -19,7 +19,7 @@ from selenium.webdriver.support.wait import WebDriverWait
from .base import BasePage
from .common import get_usable_path
from .driver_element import DriverElement, make_driver_ele, Scroll, ElementWaiter
from .session_element import make_session_ele
from .session_element import make_session_ele, SessionElement
class DriverPage(BasePage):
@ -33,7 +33,7 @@ class DriverPage(BasePage):
self._scroll = None
def __call__(self, loc_or_str: Union[Tuple[str, str], str, DriverElement, WebElement],
timeout: float = None) -> Union[DriverElement, List[DriverElement], str]:
timeout: float = None) -> Union[DriverElement, str, None]:
"""在内部查找元素 \n
ele = page('@id=ele_id') \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
@ -95,7 +95,7 @@ class DriverPage(BasePage):
def ele(self,
loc_or_ele: Union[Tuple[str, str], str, DriverElement, WebElement],
timeout: float = None) -> Union[DriverElement, List[DriverElement], str, None]:
timeout: float = None) -> Union[DriverElement, str, None]:
"""返回页面中符合条件的第一个元素 \n
:param loc_or_ele: 元素的定位信息可以是元素对象loc元组或查询字符串
:param timeout: 查找元素超时时间默认与页面等待时间一致
@ -105,7 +105,7 @@ class DriverPage(BasePage):
def eles(self,
loc_or_str: Union[Tuple[str, str], str],
timeout: float = None) -> List[DriverElement]:
timeout: float = None) -> List[Union[DriverElement, str]]:
"""返回页面中所有符合条件的元素 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:param timeout: 查找元素超时时间默认与页面等待时间一致
@ -113,7 +113,7 @@ class DriverPage(BasePage):
"""
return self._ele(loc_or_str, timeout, single=False)
def s_ele(self, loc_or_ele: Union[Tuple[str, str], str, DriverElement] = None):
def s_ele(self, loc_or_ele: Union[Tuple[str, str], str, DriverElement] = None) -> Union[SessionElement, str, None]:
"""查找第一个符合条件的元素以SessionElement形式返回处理复杂页面时效率很高 \n
:param loc_or_ele: 元素的定位信息可以是loc元组或查询字符串
:return: SessionElement对象或属性文本
@ -123,7 +123,7 @@ class DriverPage(BasePage):
else:
return make_session_ele(self, loc_or_ele)
def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = None):
def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = None) -> List[Union[SessionElement, str]]:
"""查找所有符合条件的元素以SessionElement列表形式返回 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:return: SessionElement对象组成的列表
@ -133,7 +133,7 @@ class DriverPage(BasePage):
def _ele(self,
loc_or_ele: Union[Tuple[str, str], str, DriverElement, WebElement],
timeout: float = None,
single: bool = True) -> Union[DriverElement, List[DriverElement], str, None]:
single: bool = True) -> Union[DriverElement, str, None, List[Union[DriverElement, str]]]:
"""返回页面中符合条件的元素,默认返回第一个 \n
:param loc_or_ele: 元素的定位信息可以是元素对象loc元组或查询字符串
:param timeout: 查找元素超时时间

View File

@ -64,8 +64,7 @@ class MixPage(SessionPage, DriverPage, BasePage):
def __call__(self,
loc_or_str: Union[Tuple[str, str], str, DriverElement, SessionElement, WebElement],
timeout: float = None) \
-> Union[DriverElement, SessionElement, str, List[DriverElement], List[SessionElement]]:
timeout: float = None) -> Union[DriverElement, SessionElement, str, None]:
"""在内部查找元素 \n
ele = page('@id=ele_id') \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
@ -133,8 +132,7 @@ class MixPage(SessionPage, DriverPage, BasePage):
def ele(self,
loc_or_ele: Union[Tuple[str, str], str, DriverElement, SessionElement, WebElement],
timeout: float = None) \
-> Union[DriverElement, SessionElement, str, List[SessionElement], List[DriverElement]]:
timeout: float = None) -> Union[DriverElement, SessionElement, str, None]:
"""返回第一个符合条件的元素、属性或节点文本 \n
:param loc_or_ele: 元素的定位信息可以是元素对象loc元组或查询字符串
:param timeout: 查找元素超时时间默认与页面等待时间一致
@ -147,7 +145,7 @@ class MixPage(SessionPage, DriverPage, BasePage):
def eles(self,
loc_or_str: Union[Tuple[str, str], str],
timeout: float = None) -> Union[List[DriverElement], List[SessionElement], List[str]]:
timeout: float = None) -> List[Union[DriverElement, SessionElement, str]]:
"""返回页面中所有符合条件的元素、属性或节点文本 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:param timeout: 查找元素超时时间默认与页面等待时间一致
@ -159,7 +157,7 @@ class MixPage(SessionPage, DriverPage, BasePage):
return super(SessionPage, self).eles(loc_or_str, timeout=timeout)
def s_ele(self, loc_or_ele: Union[Tuple[str, str], str, DriverElement, SessionElement] = None) \
-> Union[SessionElement, List[SessionElement], List[str]]:
-> Union[SessionElement, str, None]:
"""查找第一个符合条件的元素以SessionElement形式返回d模式处理复杂页面时效率很高 \n
:param loc_or_ele: 元素的定位信息可以是loc元组或查询字符串
:return: SessionElement对象或属性文本
@ -169,8 +167,7 @@ class MixPage(SessionPage, DriverPage, BasePage):
elif self._mode == 'd':
return super(SessionPage, self).s_ele(loc_or_ele)
def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = None) \
-> Union[SessionElement, List[SessionElement], List[str]]:
def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = None) -> List[Union[SessionElement, str]]:
"""查找所有符合条件的元素以SessionElement形式返回d模式处理复杂页面时效率很高 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:return: SessionElement对象或属性文本组成的列表
@ -183,7 +180,8 @@ class MixPage(SessionPage, DriverPage, BasePage):
def _ele(self,
loc_or_ele: Union[Tuple[str, str], str, DriverElement, SessionElement, WebElement],
timeout: float = None, single: bool = True) \
-> Union[DriverElement, SessionElement, str, List[SessionElement], List[DriverElement]]:
-> Union[DriverElement, SessionElement, str, None, List[Union[SessionElement, str]], List[
Union[DriverElement, str]]]:
"""返回页面中符合条件的元素、属性或节点文本,默认返回第一个 \n
:param loc_or_ele: 元素的定位信息可以是元素对象loc元组或查询字符串
:param timeout: 查找元素超时时间d模式专用

View File

@ -29,7 +29,9 @@ 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: Union[Tuple[str, str], str], timeout=None):
def __call__(self,
loc_or_str: Union[Tuple[str, str], str],
timeout=None) -> Union['SessionElement', str, None]:
"""在内部查找元素 \n
ele2 = ele1('@id=ele_id') \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
@ -103,7 +105,9 @@ class SessionElement(DrissionElement):
else:
return self.inner_ele.get(attr)
def ele(self, loc_or_str: Union[Tuple[str, str], str], timeout=None):
def ele(self,
loc_or_str: Union[Tuple[str, str], str],
timeout=None) -> Union['SessionElement', str, None]:
"""返回当前元素下级符合条件的第一个元素、属性或节点文本 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:param timeout: 不起实际作用用于和DriverElement对应便于无差别调用
@ -111,7 +115,9 @@ class SessionElement(DrissionElement):
"""
return self._ele(loc_or_str)
def eles(self, loc_or_str: Union[Tuple[str, str], str], timeout=None):
def eles(self,
loc_or_str: Union[Tuple[str, str], str],
timeout=None) -> List[Union['SessionElement', str]]:
"""返回当前元素下级所有符合条件的子元素、属性或节点文本 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:param timeout: 不起实际作用用于和DriverElement对应便于无差别调用
@ -119,21 +125,26 @@ class SessionElement(DrissionElement):
"""
return self._ele(loc_or_str, single=False)
def s_ele(self, loc_or_str: Union[Tuple[str, str], str] = None):
def s_ele(self,
loc_or_str: Union[Tuple[str, str], str] = None) -> Union['SessionElement', str, None]:
"""返回当前元素下级符合条件的第一个元素、属性或节点文本 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:return: SessionElement对象或属性文本
"""
return self._ele(loc_or_str)
def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = None):
def s_eles(self,
loc_or_str: Union[Tuple[str, str], str] = None) -> List[Union['SessionElement', str]]:
"""返回当前元素下级所有符合条件的子元素、属性或节点文本 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:return: SessionElement对象或属性文本组成的列表
"""
return self._ele(loc_or_str, single=False)
def _ele(self, loc_or_str: Union[Tuple[str, str], str], timeout=None, single: bool = True):
def _ele(self,
loc_or_str: Union[Tuple[str, str], str],
timeout=None,
single: bool = True) -> Union['SessionElement', str, None, List[Union['SessionElement', str]]]:
"""返回当前元素下级符合条件的子元素、属性或节点文本,默认返回第一个 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:param timeout: 不起实际作用用于和父类对应
@ -189,7 +200,7 @@ class SessionElement(DrissionElement):
def make_session_ele(html_or_ele: Union[str, BaseElement, BasePage],
loc: Union[str, Tuple[str, str]] = None,
single: bool = True) -> Union[SessionElement, List[SessionElement], str, None]:
single: bool = True) -> Union[SessionElement, str, None, List[Union[SessionElement, str]]]:
"""从接收到的对象或html文本中查找元素返回SessionElement对象 \n
如要直接从html生成SessionElement而不在下级查找loc输入None即可 \n
:param html_or_ele: html文本BaseParser对象

View File

@ -30,8 +30,9 @@ class SessionPage(BasePage):
self._session = session
self._response = None
def __call__(self, loc_or_str: Union[Tuple[str, str], str, SessionElement], timeout=None) \
-> Union[SessionElement, List[SessionElement], str]:
def __call__(self,
loc_or_str: Union[Tuple[str, str], str, SessionElement],
timeout=None) -> Union[SessionElement, str, None]:
"""在内部查找元素 \n
ele2 = ele1('@id=ele_id') \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
@ -97,8 +98,9 @@ class SessionPage(BasePage):
return self._url_available
def ele(self, loc_or_ele: Union[Tuple[str, str], str, SessionElement], timeout: float = None) \
-> Union[SessionElement, List[SessionElement], str, None]:
def ele(self,
loc_or_ele: Union[Tuple[str, str], str, SessionElement],
timeout: float = None) -> Union[SessionElement, str, None]:
"""返回页面中符合条件的第一个元素、属性或节点文本 \n
:param loc_or_ele: 元素的定位信息可以是元素对象loc元组或查询字符串
:param timeout: 不起实际作用用于和DriverElement对应便于无差别调用
@ -106,7 +108,9 @@ class SessionPage(BasePage):
"""
return self._ele(loc_or_ele)
def eles(self, loc_or_str: Union[Tuple[str, str], str], timeout: float = None) -> List[SessionElement]:
def eles(self,
loc_or_str: Union[Tuple[str, str], str],
timeout: float = None) -> List[Union[SessionElement, str]]:
"""返回页面中所有符合条件的元素、属性或节点文本 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:param timeout: 不起实际作用用于和DriverElement对应便于无差别调用
@ -114,14 +118,14 @@ class SessionPage(BasePage):
"""
return self._ele(loc_or_str, single=False)
def s_ele(self, loc_or_ele: Union[Tuple[str, str], str, SessionElement] = None):
def s_ele(self, loc_or_ele: Union[Tuple[str, str], str, SessionElement] = None) -> Union[SessionElement, str, None]:
"""返回页面中符合条件的第一个元素、属性或节点文本 \n
:param loc_or_ele: 元素的定位信息可以是元素对象loc元组或查询字符串
:return: SessionElement对象或属性文本
"""
return make_session_ele(self.html) if loc_or_ele is None else self._ele(loc_or_ele)
def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = None):
def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = None) -> List[Union[SessionElement, str]]:
"""返回页面中符合条件的所有元素、属性或节点文本 \n
:param loc_or_str: 元素的定位信息可以是元素对象loc元组或查询字符串
:return: SessionElement对象或属性文本
@ -131,7 +135,7 @@ class SessionPage(BasePage):
def _ele(self,
loc_or_ele: Union[Tuple[str, str], str, SessionElement],
timeout: float = None,
single: bool = True) -> Union[SessionElement, List[SessionElement], str, None]:
single: bool = True) -> Union[SessionElement, str, None, List[Union[SessionElement, str]]]:
"""返回页面中符合条件的元素、属性或节点文本,默认返回第一个 \n
:param loc_or_ele: 元素的定位信息可以是元素对象loc元组或查询字符串
:param timeout: 不起实际作用用于和父类对应
@ -168,7 +172,7 @@ class SessionPage(BasePage):
mode: str = 'get',
data: Union[dict, str] = None,
show_errmsg: bool = False,
**kwargs) -> Response:
**kwargs) -> Union[Response, None]:
"""尝试连接,重试若干次 \n
:param to_url: 要访问的url
:param times: 重试次数

View File

@ -12,7 +12,7 @@ from selenium.webdriver.remote.webelement import WebElement
from .base import BaseElement
from .common import get_loc
from .driver_element import make_driver_ele, DriverElement
from .session_element import make_session_ele
from .session_element import make_session_ele, SessionElement
class ShadowRootElement(BaseElement):
@ -27,7 +27,7 @@ class ShadowRootElement(BaseElement):
def __call__(self,
loc_or_str: Union[Tuple[str, str], str],
timeout: float = None) -> Union[DriverElement, List[DriverElement], str]:
timeout: float = None) -> Union[DriverElement, str, None]:
"""在内部查找元素 \n
ele2 = ele1('@id=ele_id') \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
@ -72,7 +72,7 @@ class ShadowRootElement(BaseElement):
return self.parent_ele.ele(loc, timeout=0)
def next(self, index: int = 1, filter_loc: Union[tuple, str] = '') -> DriverElement:
def next(self, index: int = 1, filter_loc: Union[tuple, str] = '') -> Union[DriverElement, None]:
"""返回后面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 \n
:param index: 第几个查询结果元素
:param filter_loc: 用于筛选元素的查询语法
@ -96,7 +96,7 @@ class ShadowRootElement(BaseElement):
def ele(self,
loc_or_str: Union[Tuple[str, str], str],
timeout: float = None) -> Union[DriverElement, List[DriverElement]]:
timeout: float = None) -> Union[DriverElement, str, None]:
"""返回当前元素下级符合条件的第一个元素,默认返回 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:param timeout: 查找元素超时时间默认与元素所在页面等待时间一致
@ -106,7 +106,7 @@ class ShadowRootElement(BaseElement):
def eles(self,
loc_or_str: Union[Tuple[str, str], str],
timeout: float = None) -> List[DriverElement]:
timeout: float = None) -> List[Union[DriverElement, str]]:
"""返回当前元素下级所有符合条件的子元素 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:param timeout: 查找元素超时时间默认与元素所在页面等待时间一致
@ -114,14 +114,14 @@ class ShadowRootElement(BaseElement):
"""
return self._ele(loc_or_str, timeout=timeout, single=False)
def s_ele(self, loc_or_ele=None):
def s_ele(self, loc_or_ele=None) -> Union[SessionElement, str, None]:
"""查找第一个符合条件的元素以SessionElement形式返回处理复杂页面时效率很高 \n
:param loc_or_ele: 元素的定位信息可以是loc元组或查询字符串
:return: SessionElement对象或属性文本
"""
return make_session_ele(self, loc_or_ele)
def s_eles(self, loc_or_ele):
def s_eles(self, loc_or_ele) -> List[Union[SessionElement, str]]:
"""查找所有符合条件的元素以SessionElement列表形式返回处理复杂页面时效率很高 \n
:param loc_or_ele: 元素的定位信息可以是loc元组或查询字符串
:return: SessionElement对象或属性文本
@ -131,7 +131,7 @@ class ShadowRootElement(BaseElement):
def _ele(self,
loc_or_str: Union[Tuple[str, str], str],
timeout: float = None,
single: bool = True) -> Union[DriverElement, List[DriverElement]]:
single: bool = True) -> Union[DriverElement, str, None, List[Union[DriverElement, str]]]:
"""返回当前元素下级符合条件的子元素,默认返回第一个 \n
:param loc_or_str: 元素的定位信息可以是loc元组或查询字符串
:param timeout: 查找元素超时时间