mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
微调,完善ele()类型判断及注解
This commit is contained in:
parent
ba8d771545
commit
7d8a1d88d6
@ -7,14 +7,14 @@
|
|||||||
from glob import glob
|
from glob import glob
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from time import time, sleep
|
from time import time, sleep
|
||||||
from typing import Union, List, Any
|
from typing import Union, List, Any, Tuple
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
from selenium.common.exceptions import NoAlertPresentException
|
from selenium.common.exceptions import NoAlertPresentException
|
||||||
from selenium.webdriver.chrome.webdriver import WebDriver
|
from selenium.webdriver.chrome.webdriver import WebDriver
|
||||||
from selenium.webdriver.remote.webelement import WebElement
|
from selenium.webdriver.remote.webelement import WebElement
|
||||||
|
|
||||||
from .common import get_loc_from_str, get_available_file_name
|
from .common import get_loc_from_str, get_available_file_name, translate_loc_to_xpath
|
||||||
from .driver_element import DriverElement, execute_driver_find
|
from .driver_element import DriverElement, execute_driver_find
|
||||||
|
|
||||||
|
|
||||||
@ -106,10 +106,10 @@ class DriverPage(object):
|
|||||||
return self._url_available
|
return self._url_available
|
||||||
|
|
||||||
def ele(self,
|
def ele(self,
|
||||||
loc_or_ele: Union[tuple, str, DriverElement, WebElement],
|
loc_or_ele: Union[Tuple[str, str], str, DriverElement, WebElement],
|
||||||
mode: str = None,
|
mode: str = None,
|
||||||
timeout: float = None,
|
timeout: float = None,
|
||||||
show_errmsg: bool = False) -> Union[DriverElement, List[DriverElement], None]:
|
show_errmsg: bool = False) -> Union[DriverElement, List[DriverElement or str], str, None]:
|
||||||
"""返回页面中符合条件的元素,默认返回第一个 \n
|
"""返回页面中符合条件的元素,默认返回第一个 \n
|
||||||
示例: \n
|
示例: \n
|
||||||
- 接收到元素对象时: \n
|
- 接收到元素对象时: \n
|
||||||
@ -139,20 +139,31 @@ class DriverPage(object):
|
|||||||
:return: DriverElement对象
|
:return: DriverElement对象
|
||||||
"""
|
"""
|
||||||
if isinstance(loc_or_ele, (str, tuple)):
|
if isinstance(loc_or_ele, (str, tuple)):
|
||||||
loc_or_ele = get_loc_from_str(loc_or_ele) if isinstance(loc_or_ele, str) else loc_or_ele
|
if isinstance(loc_or_ele, str):
|
||||||
|
loc_or_ele = get_loc_from_str(loc_or_ele)
|
||||||
|
else:
|
||||||
|
if len(loc_or_ele) != 2:
|
||||||
|
raise ValueError("Len of loc_or_ele must be 2 when it's a tuple.")
|
||||||
|
loc_or_ele = translate_loc_to_xpath(loc_or_ele)
|
||||||
if loc_or_ele[0] == 'xpath' and not loc_or_ele[1].startswith(('/', '(')):
|
if loc_or_ele[0] == 'xpath' and not loc_or_ele[1].startswith(('/', '(')):
|
||||||
loc_or_ele = loc_or_ele[0], f'//{loc_or_ele[1]}'
|
loc_or_ele = loc_or_ele[0], f'//{loc_or_ele[1]}'
|
||||||
|
|
||||||
elif isinstance(loc_or_ele, DriverElement):
|
elif isinstance(loc_or_ele, DriverElement):
|
||||||
return loc_or_ele
|
return loc_or_ele
|
||||||
|
|
||||||
elif isinstance(loc_or_ele, WebElement):
|
elif isinstance(loc_or_ele, WebElement):
|
||||||
return DriverElement(loc_or_ele, self.timeout)
|
return DriverElement(loc_or_ele, self.timeout)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ValueError('Argument loc_or_str can only be tuple, str, DriverElement, DriverElement.')
|
raise ValueError('Argument loc_or_str can only be tuple, str, DriverElement, DriverElement.')
|
||||||
|
|
||||||
timeout = timeout or self.timeout
|
timeout = timeout or self.timeout
|
||||||
return execute_driver_find(self.driver, loc_or_ele, mode, show_errmsg, timeout)
|
return execute_driver_find(self.driver, loc_or_ele, mode, show_errmsg, timeout)
|
||||||
|
|
||||||
def eles(self, loc_or_str: Union[tuple, str], timeout: float = None, show_errmsg=False) -> List[DriverElement]:
|
def eles(self,
|
||||||
|
loc_or_str: Union[Tuple[str, str], str],
|
||||||
|
timeout: float = None,
|
||||||
|
show_errmsg=False) -> List[DriverElement or str]:
|
||||||
"""返回页面中所有符合条件的元素 \n
|
"""返回页面中所有符合条件的元素 \n
|
||||||
示例: \n
|
示例: \n
|
||||||
- 用loc元组查找: \n
|
- 用loc元组查找: \n
|
||||||
|
@ -11,7 +11,7 @@ from random import randint
|
|||||||
from re import search as re_SEARCH
|
from re import search as re_SEARCH
|
||||||
from re import sub as re_SUB
|
from re import sub as re_SUB
|
||||||
from time import time, sleep
|
from time import time, sleep
|
||||||
from typing import Union, List
|
from typing import Union, List, Tuple
|
||||||
from urllib import parse
|
from urllib import parse
|
||||||
from urllib.parse import urlparse, quote
|
from urllib.parse import urlparse, quote
|
||||||
|
|
||||||
@ -69,9 +69,9 @@ class SessionPage(object):
|
|||||||
return self.response.html.html
|
return self.response.html.html
|
||||||
|
|
||||||
def ele(self,
|
def ele(self,
|
||||||
loc_or_ele: Union[tuple, str, SessionElement, Element],
|
loc_or_ele: Union[Tuple[str, str], str, SessionElement, Element],
|
||||||
mode: str = None,
|
mode: str = None,
|
||||||
show_errmsg: bool = False) -> Union[SessionElement, List[SessionElement], None]:
|
show_errmsg: bool = False) -> Union[SessionElement, List[SessionElement or str], str, None]:
|
||||||
"""返回页面中符合条件的元素,默认返回第一个 \n
|
"""返回页面中符合条件的元素,默认返回第一个 \n
|
||||||
示例: \n
|
示例: \n
|
||||||
- 接收到元素对象时: \n
|
- 接收到元素对象时: \n
|
||||||
@ -100,20 +100,29 @@ class SessionPage(object):
|
|||||||
:return: SessionElement对象
|
:return: SessionElement对象
|
||||||
"""
|
"""
|
||||||
if isinstance(loc_or_ele, (str, tuple)):
|
if isinstance(loc_or_ele, (str, tuple)):
|
||||||
loc_or_ele = get_loc_from_str(loc_or_ele) if isinstance(loc_or_ele, str) \
|
if isinstance(loc_or_ele, str):
|
||||||
else translate_loc_to_xpath(loc_or_ele)
|
loc_or_ele = get_loc_from_str(loc_or_ele)
|
||||||
|
else:
|
||||||
|
if len(loc_or_ele) != 2:
|
||||||
|
raise ValueError("Len of loc_or_ele must be 2 when it's a tuple.")
|
||||||
|
loc_or_ele = translate_loc_to_xpath(loc_or_ele)
|
||||||
if loc_or_ele[0] == 'xpath' and not loc_or_ele[1].startswith(('/', '(')):
|
if loc_or_ele[0] == 'xpath' and not loc_or_ele[1].startswith(('/', '(')):
|
||||||
loc_or_ele = loc_or_ele[0], f'//{loc_or_ele[1]}'
|
loc_or_ele = loc_or_ele[0], f'//{loc_or_ele[1]}'
|
||||||
|
|
||||||
elif isinstance(loc_or_ele, SessionElement):
|
elif isinstance(loc_or_ele, SessionElement):
|
||||||
return loc_or_ele
|
return loc_or_ele
|
||||||
|
|
||||||
elif isinstance(loc_or_ele, Element):
|
elif isinstance(loc_or_ele, Element):
|
||||||
return SessionElement(loc_or_ele)
|
return SessionElement(loc_or_ele)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ValueError('Argument loc_or_str can only be tuple, str, SessionElement, Element.')
|
raise ValueError('Argument loc_or_str can only be tuple, str, SessionElement, Element.')
|
||||||
return execute_session_find(self.response.html, loc_or_ele, mode, show_errmsg)
|
|
||||||
# return execute_session_find(self, loc_or_ele, mode, show_errmsg)
|
|
||||||
|
|
||||||
def eles(self, loc_or_str: Union[tuple, str], show_errmsg: bool = False) -> List[SessionElement]:
|
return execute_session_find(self.response.html, loc_or_ele, mode, show_errmsg)
|
||||||
|
|
||||||
|
def eles(self,
|
||||||
|
loc_or_str: Union[Tuple[str, str], str],
|
||||||
|
show_errmsg: bool = False) -> List[SessionElement or str]:
|
||||||
"""返回页面中所有符合条件的元素 \n
|
"""返回页面中所有符合条件的元素 \n
|
||||||
示例: \n
|
示例: \n
|
||||||
- 用loc元组查找: \n
|
- 用loc元组查找: \n
|
||||||
@ -242,6 +251,7 @@ class SessionPage(object):
|
|||||||
:param goal_path: 存放路径
|
:param goal_path: 存放路径
|
||||||
:param rename: 重命名文件,可不写扩展名
|
:param rename: 重命名文件,可不写扩展名
|
||||||
:param file_exists: 若存在同名文件,可选择 'rename', 'overwrite', 'skip' 方式处理
|
:param file_exists: 若存在同名文件,可选择 'rename', 'overwrite', 'skip' 方式处理
|
||||||
|
:param post_data: post方式的数据
|
||||||
:param show_msg: 是否显示下载信息
|
:param show_msg: 是否显示下载信息
|
||||||
:param show_errmsg: 是否抛出和显示异常
|
:param show_errmsg: 是否抛出和显示异常
|
||||||
:param kwargs: 连接参数
|
:param kwargs: 连接参数
|
||||||
|
Loading…
x
Reference in New Issue
Block a user