From 7d8a1d88d642515756b69de913607301e2603ce0 Mon Sep 17 00:00:00 2001 From: g1879 Date: Tue, 3 Nov 2020 12:30:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E8=B0=83=EF=BC=8C=E5=AE=8C=E5=96=84el?= =?UTF-8?q?e()=E7=B1=BB=E5=9E=8B=E5=88=A4=E6=96=AD=E5=8F=8A=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/driver_page.py | 23 +++++++++++++++++------ DrissionPage/session_page.py | 26 ++++++++++++++++++-------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/DrissionPage/driver_page.py b/DrissionPage/driver_page.py index 6054a5f..5864900 100644 --- a/DrissionPage/driver_page.py +++ b/DrissionPage/driver_page.py @@ -7,14 +7,14 @@ from glob import glob from pathlib import Path from time import time, sleep -from typing import Union, List, Any +from typing import Union, List, Any, Tuple from urllib.parse import quote from selenium.common.exceptions import NoAlertPresentException from selenium.webdriver.chrome.webdriver import WebDriver 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 @@ -106,10 +106,10 @@ class DriverPage(object): return self._url_available def ele(self, - loc_or_ele: Union[tuple, str, DriverElement, WebElement], + loc_or_ele: Union[Tuple[str, str], str, DriverElement, WebElement], mode: str = 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 @@ -139,20 +139,31 @@ class DriverPage(object): :return: DriverElement对象 """ 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(('/', '(')): loc_or_ele = loc_or_ele[0], f'//{loc_or_ele[1]}' + elif isinstance(loc_or_ele, DriverElement): return loc_or_ele + elif isinstance(loc_or_ele, WebElement): return DriverElement(loc_or_ele, self.timeout) + else: raise ValueError('Argument loc_or_str can only be tuple, str, DriverElement, DriverElement.') timeout = timeout or self.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 - 用loc元组查找: \n diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index 78e46f3..fbede1c 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -11,7 +11,7 @@ from random import randint from re import search as re_SEARCH from re import sub as re_SUB from time import time, sleep -from typing import Union, List +from typing import Union, List, Tuple from urllib import parse from urllib.parse import urlparse, quote @@ -69,9 +69,9 @@ class SessionPage(object): return self.response.html.html def ele(self, - loc_or_ele: Union[tuple, str, SessionElement, Element], + loc_or_ele: Union[Tuple[str, str], str, SessionElement, Element], 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 @@ -100,20 +100,29 @@ class SessionPage(object): :return: SessionElement对象 """ if isinstance(loc_or_ele, (str, tuple)): - loc_or_ele = get_loc_from_str(loc_or_ele) if isinstance(loc_or_ele, str) \ - else translate_loc_to_xpath(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(('/', '(')): loc_or_ele = loc_or_ele[0], f'//{loc_or_ele[1]}' + elif isinstance(loc_or_ele, SessionElement): return loc_or_ele + elif isinstance(loc_or_ele, Element): return SessionElement(loc_or_ele) + else: 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 - 用loc元组查找: \n @@ -242,6 +251,7 @@ class SessionPage(object): :param goal_path: 存放路径 :param rename: 重命名文件,可不写扩展名 :param file_exists: 若存在同名文件,可选择 'rename', 'overwrite', 'skip' 方式处理 + :param post_data: post方式的数据 :param show_msg: 是否显示下载信息 :param show_errmsg: 是否抛出和显示异常 :param kwargs: 连接参数