diff --git a/DrissionPage/action_chains.py b/DrissionPage/action_chains.py index 4f56529..db53af0 100644 --- a/DrissionPage/action_chains.py +++ b/DrissionPage/action_chains.py @@ -5,7 +5,7 @@ """ from time import sleep -from .common import _location_in_viewport +from .common import location_in_viewport from .keys import _modifierBit, _keyDescriptionForString @@ -43,7 +43,7 @@ class ActionChains: else: raise TypeError('ele_or_loc参数只能接受坐标(x, y)或ChromiumElement对象。') - if not _location_in_viewport(self.page, lx, ly): + if not location_in_viewport(self.page, lx, ly): # 把元素滚动到页面中间 clientWidth = self.page.run_js('return document.body.clientWidth;') clientHeight = self.page.run_js('return document.body.clientHeight;') diff --git a/DrissionPage/action_chains.pyi b/DrissionPage/action_chains.pyi index 4c0d8be..8452ad0 100644 --- a/DrissionPage/action_chains.pyi +++ b/DrissionPage/action_chains.pyi @@ -16,38 +16,39 @@ class ActionChains: def __init__(self, page: ChromiumBase): self.page: ChromiumPage = ... self._dr: ChromiumDriver = ... + self.modifier: int = ... self.curr_x: int = ... self.curr_y: int = ... - self.modifier: int = ... def move_to(self, ele_or_loc: Union[ChromiumElement, Tuple[int, int], str], - offset_x: int = ..., offset_y: int = ...) -> ActionChains: ... + offset_x: int = 0, offset_y: int = 0) -> ActionChains: ... - def move(self, offset_x: int = ..., offset_y: int = ...) -> ActionChains: ... + def move(self, offset_x: int = 0, offset_y: int = 0) -> ActionChains: ... - def click(self, on_ele: Union[ChromiumElement, str] = ...) -> ActionChains: ... + def click(self, on_ele: Union[ChromiumElement, str] = None) -> ActionChains: ... - def r_click(self, on_ele: Union[ChromiumElement, str] = ...) -> ActionChains: ... + def r_click(self, on_ele: Union[ChromiumElement, str] = None) -> ActionChains: ... - def m_click(self, on_ele: Union[ChromiumElement, str] = ...) -> ActionChains: ... + def m_click(self, on_ele: Union[ChromiumElement, str] = None) -> ActionChains: ... - def hold(self, on_ele: Union[ChromiumElement, str] = ...) -> ActionChains: ... + def hold(self, on_ele: Union[ChromiumElement, str] = None) -> ActionChains: ... - def release(self, on_ele: Union[ChromiumElement, str] = ...) -> ActionChains: ... + def release(self, on_ele: Union[ChromiumElement, str] = None) -> ActionChains: ... - def r_hold(self, on_ele: Union[ChromiumElement, str] = ...) -> ActionChains: ... + def r_hold(self, on_ele: Union[ChromiumElement, str] = None) -> ActionChains: ... - def r_release(self, on_ele: Union[ChromiumElement, str] = ...) -> ActionChains: ... + def r_release(self, on_ele: Union[ChromiumElement, str] = None) -> ActionChains: ... - def m_hold(self, on_ele: Union[ChromiumElement, str] = ...) -> ActionChains: ... + def m_hold(self, on_ele: Union[ChromiumElement, str] = None) -> ActionChains: ... - def m_release(self, on_ele: Union[ChromiumElement, str] = ...) -> ActionChains: ... + def m_release(self, on_ele: Union[ChromiumElement, str] = None) -> ActionChains: ... def _hold(self, button: str) -> ActionChains: ... def _release(self, button: str) -> ActionChains: ... - def scroll(self, delta_x: int = ..., delta_y: int = ..., on_ele: Union[ChromiumElement, str] = ...) -> ActionChains: ... + def scroll(self, delta_x: int = 0, delta_y: int = 0, + on_ele: Union[ChromiumElement, str] = None) -> ActionChains: ... def up(self, pixel: int) -> ActionChains: ... diff --git a/DrissionPage/base.py b/DrissionPage/base.py index cd4fc99..2455697 100644 --- a/DrissionPage/base.py +++ b/DrissionPage/base.py @@ -190,7 +190,7 @@ class DrissionElement(BaseElement): """ return self._get_brothers(filter_loc=filter_loc, direction='preceding', brother=False, timeout=timeout) - def afters(self, filter_loc, timeout=None): + def afters(self, filter_loc='', timeout=None): """返回前面全部兄弟元素或节点组成的列表,可用查询语法筛选 \n :param filter_loc: 用于筛选元素的查询语法 :param timeout: 查找元素的超时时间 diff --git a/DrissionPage/base.pyi b/DrissionPage/base.pyi index 76832f3..ef7bb57 100644 --- a/DrissionPage/base.pyi +++ b/DrissionPage/base.pyi @@ -11,9 +11,9 @@ class BaseParser(object): def __call__(self, loc_or_str: Union[Tuple[str, str], str]): ... - def ele(self, loc_or_ele: Union[Tuple[str, str], str, BaseElement], timeout:float=...): ... + def ele(self, loc_or_ele: Union[Tuple[str, str], str, BaseElement], timeout: float = None): ... - def eles(self, loc_or_str: Union[Tuple[str, str], str], timeout=...): ... + def eles(self, loc_or_str: Union[Tuple[str, str], str], timeout=None): ... # ----------------以下属性或方法待后代实现---------------- @property @@ -24,28 +24,29 @@ class BaseParser(object): def s_eles(self, loc_or_str: Union[Tuple[str, str], str]): ... @abstractmethod - def _ele(self, loc_or_ele, timeout:float=..., single:bool=...): ... + def _ele(self, loc_or_ele, timeout: float = None, single: bool = True): ... class BaseElement(BaseParser): - def __init__(self, page: BasePage): + def __init__(self, page: BasePage = None): self.page: BasePage = ... # ----------------以下属性或方法由后代实现---------------- @property - def tag(self)->str: ... + def tag(self) -> str: ... @abstractmethod - def _ele(self, loc_or_str: Union[Tuple[str, str], str], timeout:float=..., single:bool=..., relative:bool=...): ... + def _ele(self, loc_or_str: Union[Tuple[str, str], str], timeout: float = None, single: bool = True, + relative: bool = False): ... - def parent(self, level_or_loc: Union[tuple, str, int] = ...): ... + def parent(self, level_or_loc: Union[tuple, str, int] = 1): ... - def prev(self, index: int = ...) -> None: ... + def prev(self, index: int = 1) -> None: ... def prevs(self) -> None: ... - def next(self, index: int = ...): ... + def next(self, index: int = 1): ... def nexts(self): ... @@ -68,52 +69,52 @@ class DrissionElement(BaseElement): @property def comments(self) -> list: ... - def texts(self, text_node_only: bool = ...) -> list: ... + def texts(self, text_node_only: bool = False) -> list: ... - def parent(self, level_or_loc: Union[tuple, str, int] = ...) -> Union['DrissionElement', None]: ... + def parent(self, level_or_loc: Union[tuple, str, int] = 1) -> Union['DrissionElement', None]: ... def prev(self, - index: int = ..., - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> Union['DrissionElement', str, None]: ... + index: int = 1, + filter_loc: Union[tuple, str] = '', + timeout: float = 0) -> Union['DrissionElement', str, None]: ... def next(self, - index: int = ..., - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> Union['DrissionElement', str, None]: ... + index: int = 1, + filter_loc: Union[tuple, str] = '', + timeout: float = 0) -> Union['DrissionElement', str, None]: ... def before(self, - index: int = ..., - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> Union['DrissionElement', str, None]: ... + index: int = 1, + filter_loc: Union[tuple, str] = '', + timeout: float = None) -> Union['DrissionElement', str, None]: ... def after(self, - index: int = ..., - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> Union['DrissionElement', str, None]: ... + index: int = 1, + filter_loc: Union[tuple, str] = '', + timeout: float = None) -> Union['DrissionElement', str, None]: ... def prevs(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union['DrissionElement', str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = 0) -> List[Union['DrissionElement', str]]: ... def nexts(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union['DrissionElement', str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = 0) -> List[Union['DrissionElement', str]]: ... def befores(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union['DrissionElement', str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = None) -> List[Union['DrissionElement', str]]: ... def afters(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union['DrissionElement', str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = None) -> List[Union['DrissionElement', str]]: ... def _get_brothers(self, - index: int = ..., - filter_loc: Union[tuple, str] = ..., - direction: str = ..., - brother: bool = ..., - timeout: float = ...) -> List[Union['DrissionElement', str]]: ... + index: int = None, + filter_loc: Union[tuple, str] = '', + direction: str = 'following', + brother: bool = True, + timeout: float = 0.5) -> List[Union['DrissionElement', str]]: ... # ----------------以下属性或方法由后代实现---------------- @property @@ -133,7 +134,7 @@ class DrissionElement(BaseElement): class BasePage(BaseParser): - def __init__(self, timeout: float = ...): + def __init__(self, timeout: float = None): self._url_available: bool = ... self.retry_times: int = ... self.retry_interval: float = ... @@ -164,11 +165,11 @@ class BasePage(BaseParser): def json(self) -> dict: ... @abstractmethod - def get_cookies(self, as_dict: bool = ...) -> Union[list, dict]: ... + def get_cookies(self, as_dict: bool = False) -> Union[list, dict]: ... @abstractmethod def get(self, url: str, - show_errmsg: bool = ..., - retry: int = ..., - interval: float = ...): ... + show_errmsg: bool = False, + retry: int = None, + interval: float = None): ... diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index 888ebd2..8497679 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -395,7 +395,7 @@ class ChromiumBase(BasePage): """ return make_session_ele(self, loc_or_ele) - def s_eles(self, loc_or_str=None): + def s_eles(self, loc_or_str): """查找所有符合条件的元素以SessionElement列表形式返回 \n :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 :return: SessionElement对象组成的列表 @@ -572,7 +572,8 @@ class ChromiumBase(BasePage): :param value: 项的值,设置为False时,删除该项 :return: None """ - js = f'sessionStorage.removeItem("{item}");' if item is False else f'sessionStorage.setItem("{item}","{value}");' + js = f'sessionStorage.removeItem("{item}");' if item is False \ + else f'sessionStorage.setItem("{item}","{value}");' return self.run_js(js, as_expr=True) def set_local_storage(self, item, value): diff --git a/DrissionPage/chromium_base.pyi b/DrissionPage/chromium_base.pyi index 71e814f..c74b8db 100644 --- a/DrissionPage/chromium_base.pyi +++ b/DrissionPage/chromium_base.pyi @@ -21,8 +21,8 @@ class ChromiumBase(BasePage): def __init__(self, address: str, - tab_id: str = ..., - timeout: float = ...): + tab_id: str = None, + timeout: float = None): self._control_session: Session = ... self.address: str = ... self._tab_obj: ChromiumDriver = ... @@ -38,14 +38,14 @@ class ChromiumBase(BasePage): self._debug_recorder: Recorder = ... def _connect_browser(self, - addr_driver_opts: Union[str, ChromiumDriver, DriverOptions] = ..., - tab_id: str = ...) -> None: ... + addr_driver_opts: Union[str, ChromiumDriver, DriverOptions] = None, + tab_id: str = None) -> None: ... - def _init_page(self, tab_id: str = ...) -> None: ... + def _init_page(self, tab_id: str = None) -> None: ... def _get_document(self) -> None: ... - def _wait_loaded(self, timeout: float = ...) -> bool: ... + def _wait_loaded(self, timeout: float = None) -> bool: ... def _onFrameStartedLoading(self, **kwargs): ... @@ -60,7 +60,7 @@ class ChromiumBase(BasePage): def _set_options(self) -> None: ... def __call__(self, loc_or_str: Union[Tuple[str, str], str, ChromiumElement], - timeout: float = ...) -> Union[ChromiumElement, ChromiumFrame, None]: ... + timeout: float = None) -> Union[ChromiumElement, ChromiumFrame, None]: ... @property def title(self) -> str: ... @@ -107,22 +107,22 @@ class ChromiumBase(BasePage): @property def set_page_load_strategy(self) -> PageLoadStrategy: ... - def set_timeouts(self, implicit: float = ..., page_load: float = ..., script: float = ...) -> None: ... + def set_timeouts(self, implicit: float = None, page_load: float = None, script: float = None) -> None: ... - def run_js(self, script: str, as_expr: bool = ..., *args: Any) -> Any: ... + def run_js(self, script: str, as_expr: bool = False, *args: Any) -> Any: ... - def run_async_js(self, script: str, as_expr: bool = ..., *args: Any) -> None: ... + def run_async_js(self, script: str, as_expr: bool = False, *args: Any) -> None: ... def get(self, url: str, - show_errmsg: bool = ..., - retry: int = ..., - interval: float = ..., - timeout: float = ...) -> Union[None, bool]: ... + show_errmsg: bool = False, + retry: int = None, + interval: float = None, + timeout: float = None) -> Union[None, bool]: ... - def wait_loading(self, timeout: float = ...) -> bool: ... + def wait_loading(self, timeout: float = 1) -> bool: ... - def get_cookies(self, as_dict: bool = ...) -> Union[list, dict]: ... + def get_cookies(self, as_dict: bool = False) -> Union[list, dict]: ... def set_cookies(self, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ... @@ -130,33 +130,33 @@ class ChromiumBase(BasePage): def ele(self, loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame], - timeout: float = ...) -> Union[ChromiumElement, ChromiumFrame, None]: ... + timeout: float = None) -> Union[ChromiumElement, ChromiumFrame, None]: ... def eles(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> List[Union[ChromiumElement, ChromiumFrame]]: ... + timeout: float = None) -> List[Union[ChromiumElement, ChromiumFrame]]: ... - def s_ele(self, loc_or_ele: Union[Tuple[str, str], str] = ...) \ + def s_ele(self, loc_or_ele: Union[Tuple[str, str], str] = None) \ -> Union[SessionElement, str, None]: ... - def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = ...) -> List[Union[SessionElement, str]]: ... + def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[Union[SessionElement, str]]: ... def _ele(self, loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame], - timeout: float = ..., single: bool = ..., relative: bool = ...) \ + timeout: float = None, single: bool = True, relative: bool = False) \ -> Union[ChromiumElement, ChromiumFrame, None, List[Union[ChromiumElement, ChromiumFrame]]]: ... def wait_ele(self, loc_or_ele: Union[str, tuple, ChromiumElement], - timeout: float = ...) -> 'ChromiumElementWaiter': ... + timeout: float = None) -> 'ChromiumElementWaiter': ... def scroll_to_see(self, loc_or_ele: Union[str, tuple, ChromiumElement]) -> None: ... - def refresh(self, ignore_cache: bool = ...) -> None: ... + def refresh(self, ignore_cache: bool = False) -> None: ... - def forward(self, steps: int = ...) -> None: ... + def forward(self, steps: int = 1) -> None: ... - def back(self, steps: int = ...) -> None: ... + def back(self, steps: int = 1) -> None: ... def _forward_or_back(self, steps: int) -> None: ... @@ -182,10 +182,10 @@ class ChromiumBase(BasePage): def _d_connect(self, to_url: str, - times: int = ..., - interval: float = ..., - show_errmsg: bool = ..., - timeout: float = ...) -> Union[bool, None]: ... + times: int = 0, + interval: float = 1, + show_errmsg: bool = False, + timeout: float = None) -> Union[bool, None]: ... class Timeout(object): diff --git a/DrissionPage/chromium_element.py b/DrissionPage/chromium_element.py index ab6db0e..7544ff2 100644 --- a/DrissionPage/chromium_element.py +++ b/DrissionPage/chromium_element.py @@ -9,7 +9,7 @@ from pathlib import Path from time import perf_counter, sleep from .base import DrissionElement, BaseElement -from .common import make_absolute_link, get_loc, get_ele_txt, format_html, is_js_func, _location_in_viewport +from .common import make_absolute_link, get_loc, get_ele_txt, format_html, is_js_func, location_in_viewport from .keys import _keys_to_typing, _keyDescriptionForString, _keyDefinitions from .session_element import make_session_ele @@ -262,6 +262,14 @@ class ChromiumElement(DrissionElement): """ return super().befores(filter_loc, timeout) + def afters(self, filter_loc='', timeout=None): + """返回当前元素后面符合条件的全部兄弟元素或节点组成的列表,可用查询语法筛选。查找范围不限兄弟元素,而是整个DOM文档 \n + :param filter_loc: 用于筛选元素的查询语法 + :param timeout: 查找元素的超时时间 + :return: 本元素前面的元素或节点组成的列表 + """ + return super().afters(filter_loc, timeout) + def wait_ele(self, loc_or_ele, timeout=None): """返回用于等待子元素到达某个状态的等待器对象 \n :param loc_or_ele: 可以是元素、查询字符串、loc元组 @@ -311,7 +319,7 @@ class ChromiumElement(DrissionElement): def is_in_viewport(self): """返回元素是否出现在视口中,以元素可以接受点击的点为判断""" x, y = self.location - return _location_in_viewport(self.page, x, y) if x else False + return location_in_viewport(self.page, x, y) if x else False def attr(self, attr): """返回attribute属性值 \n @@ -811,7 +819,7 @@ class ChromiumElement(DrissionElement): """ try: return self.page.run_cdp('DOM.getBoxModel', nodeId=self.node_id, not_change=True)['model'][quad] - except: + except Exception: return None def _get_absolute_rect(self, x, y): @@ -1393,7 +1401,7 @@ def _offset_scroll(ele, offset_x, offset_y): lx = loc_x + offset_x if offset_x else cp_x ly = loc_y + offset_y if offset_y else cp_y - if not _location_in_viewport(ele.page, lx, ly): + if not location_in_viewport(ele.page, lx, ly): ele.page.scroll.to_location(lx, ly) cl_x, cl_y = ele.client_location ccp_x, ccp_y = ele._client_click_point diff --git a/DrissionPage/chromium_element.pyi b/DrissionPage/chromium_element.pyi index 4bab636..7da383e 100644 --- a/DrissionPage/chromium_element.pyi +++ b/DrissionPage/chromium_element.pyi @@ -18,7 +18,7 @@ class ChromiumElement(DrissionElement): def __init__(self, page: ChromiumBase, - node_id: str = ..., obj_id: str = ..., backend_id: str = ...): + node_id: str = None, obj_id: str = None, backend_id: str = None): self._tag: str = ... self.page: Union[ChromiumPage, WebPage] = ... self._node_id: str = ... @@ -32,7 +32,7 @@ class ChromiumElement(DrissionElement): def __call__(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> Union[ChromiumElement, ChromiumFrame, str, None]: ... + timeout: float = None) -> Union[ChromiumElement, ChromiumFrame, str, None]: ... @property def tag(self) -> str: ... @@ -101,43 +101,47 @@ class ChromiumElement(DrissionElement): @property def scroll(self) -> ChromiumScroll: ... - def parent(self, level_or_loc: Union[tuple, str, int] = ...) -> Union[ChromiumElement, None]: ... + def parent(self, level_or_loc: Union[tuple, str, int] = 1) -> Union[ChromiumElement, None]: ... def prev(self, - filter_loc: Union[tuple, str] = ..., - index: int = ..., - timeout: float = ...) -> Union[ChromiumElement, str, None]: ... + filter_loc: Union[tuple, str] = '', + index: int = 1, + timeout: float = 0) -> Union[ChromiumElement, str, None]: ... def next(self, - filter_loc: Union[tuple, str] = ..., - index: int = ..., - timeout: float = ...) -> Union[ChromiumElement, str, None]: ... + filter_loc: Union[tuple, str] = '', + index: int = 1, + timeout: float = 0) -> Union[ChromiumElement, str, None]: ... def before(self, - filter_loc: Union[tuple, str] = ..., - index: int = ..., - timeout: float = ...) -> Union[ChromiumElement, str, None]: ... + filter_loc: Union[tuple, str] = '', + index: int = 1, + timeout: float = None) -> Union[ChromiumElement, str, None]: ... def after(self, - filter_loc: Union[tuple, str] = ..., - index: int = ..., - timeout: float = ...) -> Union[ChromiumElement, str, None]: ... + filter_loc: Union[tuple, str] = '', + index: int = 1, + timeout: float = None) -> Union[ChromiumElement, str, None]: ... def prevs(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union[ChromiumElement, str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = 0) -> List[Union[ChromiumElement, str]]: ... def nexts(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union[ChromiumElement, str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = 0) -> List[Union[ChromiumElement, str]]: ... def befores(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union[ChromiumElement, str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = None) -> List[Union[ChromiumElement, str]]: ... + + def afters(self, + filter_loc: Union[tuple, str] = '', + timeout: float = None) -> List[Union[ChromiumElement, str]]: ... def wait_ele(self, loc_or_ele: Union[str, tuple, ChromiumElement], - timeout: float = ...) -> ChromiumElementWaiter: ... + timeout: float = None) -> ChromiumElementWaiter: ... @property def select(self) -> ChromiumSelect: ... @@ -169,66 +173,67 @@ class ChromiumElement(DrissionElement): def set_innerHTML(self, html: str) -> None: ... - def run_js(self, script: str, as_expr: bool = ..., *args: Any) -> Any: ... + def run_js(self, script: str, as_expr: bool = False, *args: Any) -> Any: ... - def run_async_js(self, script: str, as_expr: bool = ..., *args: Any) -> None: ... + def run_async_js(self, script: str, as_expr: bool = False, *args: Any) -> None: ... def ele(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> Union[ChromiumElement, ChromiumFrame, str, None]: ... + timeout: float = None) -> Union[ChromiumElement, ChromiumFrame, str, None]: ... def eles(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> List[Union[ChromiumElement, ChromiumFrame, str]]: ... + timeout: float = None) -> List[Union[ChromiumElement, ChromiumFrame, str]]: ... - def s_ele(self, loc_or_str: Union[Tuple[str, str], str] = ...) -> Union[SessionElement, str, None]: ... + def s_ele(self, loc_or_str: Union[Tuple[str, str], str] = None) -> Union[SessionElement, str, None]: ... - def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = ...) -> List[Union[SessionElement, str]]: ... + def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = None) -> List[Union[SessionElement, str]]: ... def _ele(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ..., single: bool = ..., relative: bool = ...) \ + timeout: float = None, single: bool = True, relative: bool = False) \ -> Union[ChromiumElement, ChromiumFrame, str, None, List[Union[ChromiumElement, ChromiumFrame, str]]]: ... - def style(self, style: str, pseudo_ele: str = ...) -> str: ... + def style(self, style: str, pseudo_ele: str = '') -> str: ... def get_src(self) -> Union[bytes, str, None]: ... - def save(self, path: [str, bool] = ..., rename: str = ...) -> None: ... + def save(self, path: [str, bool] = None, rename: str = None) -> None: ... - def get_screenshot(self, path: [str, Path] = ..., as_bytes: [bool, str] = ...) -> Union[str, bytes]: ... + def get_screenshot(self, path: [str, Path] = None, as_bytes: [bool, str] = None) -> Union[str, bytes]: ... - def input(self, vals: Any, clear: bool = ...) -> None: ... + def input(self, vals: Any, clear: bool = True) -> None: ... def _set_file_input(self, files: Union[str, list, tuple]) -> None: ... - def clear(self, by_js: bool = ...) -> None: ... + def clear(self, by_js: bool = False) -> None: ... - def click(self, by_js: bool = ..., retry: bool = ..., timeout: float = ..., - wait_loading: Union[bool, float] = ...) -> bool: ... + def click(self, by_js: bool = None, retry: bool = False, timeout: float = 0.2, + wait_loading: Union[bool, float] = 0) -> bool: ... - def click_at(self, offset_x: Union[int, str] = ..., offset_y: Union[int, str] = ..., button: str = ...) -> None: ... + def click_at(self, offset_x: Union[int, str] = None, offset_y: Union[int, str] = None, + button: str = 'left') -> None: ... def r_click(self) -> None: ... def m_click(self) -> None: ... - def r_click_at(self, offset_x: Union[int, str] = ..., offset_y: Union[int, str] = ...) -> None: ... + def r_click_at(self, offset_x: Union[int, str] = None, offset_y: Union[int, str] = None) -> None: ... - def _click(self, client_x: int, client_y: int, button: str = ...) -> None: ... + def _click(self, client_x: int, client_y: int, button: str = 'left') -> None: ... - def hover(self, offset_x: int = ..., offset_y: int = ...) -> None: ... + def hover(self, offset_x: int = None, offset_y: int = None) -> None: ... - def drag(self, offset_x: int = ..., offset_y: int = ..., speed: int = ..., shake: bool = ...) -> None: ... + def drag(self, offset_x: int = 0, offset_y: int = 0, speed: int = 40, shake: bool = True) -> None: ... def drag_to(self, ele_or_loc: Union[tuple, ChromiumElement], - speed: int = ..., - shake: bool = ...) -> None: ... + speed: int = 40, + shake: bool = True) -> None: ... - def _get_obj_id(self, node_id: str = ..., backend_id: str = ...) -> str: ... + def _get_obj_id(self, node_id: str = None, backend_id: str = None) -> str: ... - def _get_node_id(self, obj_id: str = ..., backend_id: str = ...) -> str: ... + def _get_node_id(self, obj_id: str = None, backend_id: str = None) -> str: ... def _get_backend_id(self, node_id: str) -> str: ... @@ -243,8 +248,8 @@ class ChromiumShadowRootElement(BaseElement): def __init__(self, parent_ele: ChromiumElement, - obj_id: str = ..., - backend_id: str = ...): + obj_id: str = None, + backend_id: str = None): self._obj_id: str = ... self._node_id: str = ... self._backend_id: str = ... @@ -255,7 +260,7 @@ class ChromiumShadowRootElement(BaseElement): def __call__(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> Union[ChromiumElement, ChromiumFrame, None]: ... + timeout: float = None) -> Union[ChromiumElement, ChromiumFrame, None]: ... @property def is_enabled(self) -> bool: ... @@ -281,46 +286,46 @@ class ChromiumShadowRootElement(BaseElement): @property def inner_html(self) -> str: ... - def run_js(self, script: str, as_expr: bool = ..., *args: Any) -> Any: ... + def run_js(self, script: str, as_expr: bool = False, *args: Any) -> Any: ... - def run_async_js(self, script: str, as_expr: bool = ..., *args: Any) -> None: ... + def run_async_js(self, script: str, as_expr: bool = False, *args: Any) -> None: ... - def parent(self, level_or_loc: Union[str, int] = ...) -> ChromiumElement: ... + def parent(self, level_or_loc: Union[str, int] = 1) -> ChromiumElement: ... def next(self, - filter_loc: Union[tuple, str] = ..., - index: int = ...) -> Union[ChromiumElement, str, None]: ... + filter_loc: Union[tuple, str] = '', + index: int = 1) -> Union[ChromiumElement, str, None]: ... def before(self, - filter_loc: Union[tuple, str] = ..., - index: int = ...) -> Union[ChromiumElement, str, None]: ... + filter_loc: Union[tuple, str] = '', + index: int = 1) -> Union[ChromiumElement, str, None]: ... def after(self, - filter_loc: Union[tuple, str] = ..., - index: int = ...) -> Union[ChromiumElement, str, None]: ... + filter_loc: Union[tuple, str] = '', + index: int = 1) -> Union[ChromiumElement, str, None]: ... - def nexts(self, filter_loc: Union[tuple, str] = ...) -> List[Union[ChromiumElement, str]]: ... + def nexts(self, filter_loc: Union[tuple, str] = '') -> List[Union[ChromiumElement, str]]: ... - def befores(self, filter_loc: Union[tuple, str] = ...) -> List[Union[ChromiumElement, str]]: ... + def befores(self, filter_loc: Union[tuple, str] = '') -> List[Union[ChromiumElement, str]]: ... - def afters(self, filter_loc: Union[tuple, str] = ...) -> List[Union[ChromiumElement, str]]: ... + def afters(self, filter_loc: Union[tuple, str] = '') -> List[Union[ChromiumElement, str]]: ... def ele(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> Union[ChromiumElement, ChromiumFrame, None]: ... + timeout: float = None) -> Union[ChromiumElement, ChromiumFrame, None]: ... def eles(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> List[Union[ChromiumElement, ChromiumFrame]]: ... + timeout: float = None) -> List[Union[ChromiumElement, ChromiumFrame]]: ... - def s_ele(self, loc_or_str: Union[Tuple[str, str], str] = ...) -> Union[SessionElement, str, None]: ... + def s_ele(self, loc_or_str: Union[Tuple[str, str], str] = None) -> Union[SessionElement, str, None]: ... def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[Union[SessionElement, str]]: ... def _ele(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ..., - single: bool = ..., relative: bool = ...) \ + timeout: float = None, + single: bool = True, relative: bool = False) \ -> Union[ChromiumElement, ChromiumFrame, None, str, List[Union[ChromiumElement, ChromiumFrame, str]]]: ... def _get_node_id(self, obj_id: str) -> str: ... @@ -332,9 +337,9 @@ class ChromiumShadowRootElement(BaseElement): def find_in_chromium_ele(ele: ChromiumElement, loc: Union[str, Tuple[str, str]], - single: bool = ..., - timeout: float = ..., - relative: bool = ...) -> Union[ + single: bool = True, + timeout: float = None, + relative: bool = True) -> Union[ ChromiumElement, str, None, List[Union[ChromiumElement, str]]]: ... @@ -342,7 +347,7 @@ def _find_by_xpath(ele: ChromiumElement, xpath: str, single: bool, timeout: float, - relative: bool = ...) -> Union[ChromiumElement, List[ChromiumElement], None]: ... + relative: bool = True) -> Union[ChromiumElement, List[ChromiumElement], None]: ... def _find_by_css(ele: ChromiumElement, @@ -359,7 +364,7 @@ def _make_js_for_find_ele_by_xpath(xpath: str, type_txt: str, node_txt: str) -> def run_js(page_or_ele: Union[ChromiumBase, ChromiumElement, ChromiumShadowRootElement], script: str, - as_expr: bool = ..., timeout: float = ..., args: tuple = ..., not_change: bool = ...) -> Any: ... + as_expr: bool = False, timeout: float = None, args: tuple = ..., not_change: bool = ...) -> Any: ... def _parse_js_result(page: ChromiumBase, ele: ChromiumElement, result: dict): ... @@ -398,13 +403,13 @@ class ChromiumScroll(object): def to_location(self, x: int, y: int) -> None: ... - def up(self, pixel: int = ...) -> None: ... + def up(self, pixel: int = 300) -> None: ... - def down(self, pixel: int = ...) -> None: ... + def down(self, pixel: int = 300) -> None: ... - def left(self, pixel: int = ...) -> None: ... + def left(self, pixel: int = 300) -> None: ... - def right(self, pixel: int = ...) -> None: ... + def right(self, pixel: int = 300) -> None: ... class ChromiumSelect(object): @@ -428,30 +433,30 @@ class ChromiumSelect(object): def clear(self) -> None: ... - def by_text(self, text: Union[str, list, tuple], timeout: float = ...) -> bool: ... + def by_text(self, text: Union[str, list, tuple], timeout: float = None) -> bool: ... - def by_value(self, value: Union[str, list, tuple], timeout: float = ...) -> bool: ... + def by_value(self, value: Union[str, list, tuple], timeout: float = None) -> bool: ... - def by_index(self, index: Union[int, list, tuple], timeout: float = ...) -> bool: ... + def by_index(self, index: Union[int, list, tuple], timeout: float = None) -> bool: ... - def cancel_by_text(self, text: Union[str, list, tuple], timeout: float = ...) -> bool: ... + def cancel_by_text(self, text: Union[str, list, tuple], timeout: float = None) -> bool: ... - def cancel_by_value(self, value: Union[str, list, tuple], timeout: float = ...) -> bool: ... + def cancel_by_value(self, value: Union[str, list, tuple], timeout: float = None) -> bool: ... - def cancel_by_index(self, index: Union[int, list, tuple], timeout: float = ...) -> bool: ... + def cancel_by_index(self, index: Union[int, list, tuple], timeout: float = None) -> bool: ... def invert(self) -> None: ... def _select(self, - text_value_index: Union[str, int, list, tuple] = ..., - para_type: str = ..., - deselect: bool = ..., - timeout: float = ...) -> bool: ... + text_value_index: Union[str, int, list, tuple] = None, + para_type: str = 'text', + deselect: bool = False, + timeout: float = None) -> bool: ... def _select_multi(self, - text_value_index: Union[list, tuple] = ..., - para_type: str = ..., - deselect: bool = ...) -> bool: ... + text_value_index: Union[list, tuple] = None, + para_type: str = 'text', + deselect: bool = False) -> bool: ... class ChromiumElementWaiter(object): @@ -459,7 +464,7 @@ class ChromiumElementWaiter(object): def __init__(self, page_or_ele: Union[ChromiumBase, ChromiumElement], loc_or_ele: Union[str, tuple, ChromiumElement], - timeout: float = ...): + timeout: float = None): self.loc_or_ele: Union[str, tuple, ChromiumElement] = ... self.timeout: float = ... self.driver: Union[ChromiumPage, ChromiumPage] = ... diff --git a/DrissionPage/chromium_frame.py b/DrissionPage/chromium_frame.py index 8c05c49..10c82fa 100644 --- a/DrissionPage/chromium_frame.py +++ b/DrissionPage/chromium_frame.py @@ -64,7 +64,7 @@ class ChromiumFrame(ChromiumBase): try: self._tab_obj.DOM.describeNode(nodeId=self.node_id) - except: + except Exception: self._reload() sleep(2) @@ -359,6 +359,15 @@ class ChromiumFrame(ChromiumBase): self._check_ok() return self.frame_ele.befores(filter_loc, timeout) + def afters(self, filter_loc='', timeout=None): + """返回当前元素后面符合条件的全部兄弟元素或节点组成的列表,可用查询语法筛选。查找范围不限兄弟元素,而是整个DOM文档 \n + :param filter_loc: 用于筛选元素的查询语法 + :param timeout: 查找元素的超时时间 + :return: 本元素前面的元素或节点组成的列表 + """ + self._check_ok() + return self.frame_ele.afters(filter_loc, timeout) + def _ele(self, loc_or_ele, timeout=None, single=True, relative=False): """在frame内查找单个元素 \n :param loc_or_ele: 定位符或元素对象 diff --git a/DrissionPage/chromium_frame.pyi b/DrissionPage/chromium_frame.pyi index 3871b58..8963bbf 100644 --- a/DrissionPage/chromium_frame.pyi +++ b/DrissionPage/chromium_frame.pyi @@ -24,7 +24,7 @@ class ChromiumFrame(ChromiumBase): def __call__(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> Union[ChromiumElement, ChromiumFrame, str, None]: ... + timeout: float = None) -> Union[ChromiumElement, ChromiumFrame, str, None]: ... def __repr__(self) -> str: ... @@ -103,52 +103,56 @@ class ChromiumFrame(ChromiumBase): def remove_attr(self, attr: str) -> None: ... - def run_js(self, script: str, as_expr: bool = ..., *args: Any) -> Any: ... + def run_js(self, script: str, as_expr: bool = False, *args: Any) -> Any: ... - def parent(self, level_or_loc: Union[tuple, str, int] = ...) -> Union[ChromiumElement, None]: ... + def parent(self, level_or_loc: Union[tuple, str, int] = 1) -> Union[ChromiumElement, None]: ... def prev(self, - filter_loc: Union[tuple, str] = ..., - index: int = ..., - timeout: float = ...) -> Union[ChromiumElement, ChromiumFrame, str, None]: ... + filter_loc: Union[tuple, str] = '', + index: int = 1, + timeout: float = 0) -> Union[ChromiumElement, ChromiumFrame, str, None]: ... def next(self, - filter_loc: Union[tuple, str] = ..., - index: int = ..., - timeout: float = ...) -> Union[ChromiumElement, ChromiumFrame, str, None]: ... + filter_loc: Union[tuple, str] = '', + index: int = 1, + timeout: float = 0) -> Union[ChromiumElement, ChromiumFrame, str, None]: ... def before(self, - filter_loc: Union[tuple, str] = ..., - index: int = ..., - timeout: float = ...) -> Union[ChromiumElement, ChromiumFrame, str, None]: ... + filter_loc: Union[tuple, str] = '', + index: int = 1, + timeout: float = None) -> Union[ChromiumElement, ChromiumFrame, str, None]: ... def after(self, - filter_loc: Union[tuple, str] = ..., - index: int = ..., - timeout: float = ...) -> Union[ChromiumElement, ChromiumFrame, str, None]: ... + filter_loc: Union[tuple, str] = '', + index: int = 1, + timeout: float = None) -> Union[ChromiumElement, ChromiumFrame, str, None]: ... def prevs(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union[ChromiumElement, ChromiumFrame, str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = 0) -> List[Union[ChromiumElement, ChromiumFrame, str]]: ... def nexts(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union[ChromiumElement, ChromiumFrame, str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = 0) -> List[Union[ChromiumElement, ChromiumFrame, str]]: ... def befores(self, filter_loc: Union[tuple, str] = ..., timeout: float = ...) -> List[Union[ChromiumElement, ChromiumFrame, str]]: ... + def afters(self, + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> List[Union[ChromiumElement, ChromiumFrame, str]]: ... + def _ele(self, loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame], - timeout: float = ..., single: bool = ..., relative: bool = ...) \ + timeout: float = None, single: bool = True, relative: bool = False) \ -> Union[ChromiumElement, ChromiumFrame, None, List[Union[ChromiumElement, ChromiumFrame]]]: ... def _d_connect(self, to_url: str, - times: int = ..., - interval: float = ..., - show_errmsg: bool = ..., - timeout: float = ...) -> Union[bool, None]: ... + times: int = 0, + interval: float = 1, + show_errmsg: bool = False, + timeout: float = None) -> Union[bool, None]: ... def _is_inner_frame(self) -> bool: ... diff --git a/DrissionPage/chromium_page.pyi b/DrissionPage/chromium_page.pyi index ffd0cba..5880082 100644 --- a/DrissionPage/chromium_page.pyi +++ b/DrissionPage/chromium_page.pyi @@ -16,9 +16,9 @@ from .chromium_driver import ChromiumDriver class ChromiumPage(ChromiumBase): def __init__(self, - addr_driver_opts: Union[str, ChromiumDriver, DriverOptions] = ..., - tab_id: str = ..., - timeout: float = ...): + addr_driver_opts: Union[str, ChromiumDriver, DriverOptions] = None, + tab_id: str = None, + timeout: float = None): self.options: DriverOptions = ... self.process: popen = ... self._window_setter: WindowSetter = ... @@ -26,10 +26,10 @@ class ChromiumPage(ChromiumBase): self._alert: Alert = ... def _connect_browser(self, - addr_driver_opts: Union[str, ChromiumDriver, DriverOptions] = ..., - tab_id: str = ...) -> None: ... + addr_driver_opts: Union[str, ChromiumDriver, DriverOptions] = None, + tab_id: str = None) -> None: ... - def _init_page(self, tab_id: str = ...) -> None: ... + def _init_page(self, tab_id: str = None) -> None: ... def _set_options(self) -> None: ... @@ -48,29 +48,29 @@ class ChromiumPage(ChromiumBase): @property def set_window(self) -> 'WindowSetter': ... - def get_tab(self, tab_id: str = ...) -> ChromiumTab: ... + def get_tab(self, tab_id: str = None) -> ChromiumTab: ... - def get_screenshot(self, path: [str, Path] = ..., - as_bytes: [bool, str] = ..., - full_page: bool = ..., - left_top: Tuple[int, int] = ..., - right_bottom: Tuple[int, int] = ...) -> Union[str, bytes]: ... + def get_screenshot(self, path: [str, Path] = None, + as_bytes: [bool, str] = None, + full_page: bool = False, + left_top: Tuple[int, int] = None, + right_bottom: Tuple[int, int] = None) -> Union[str, bytes]: ... def to_front(self) -> None: ... - def new_tab(self, url: str = ..., switch_to: bool = ...) -> None: ... + def new_tab(self, url: str = None, switch_to: bool = True) -> None: ... def to_main_tab(self) -> None: ... - def to_tab(self, tab_id: str = ..., activate: bool = ...) -> None: ... + def to_tab(self, tab_id: str = None, activate: bool = True) -> None: ... - def _to_tab(self, tab_id: str = ..., activate: bool = ..., read_doc: bool = ...) -> None: ... + def _to_tab(self, tab_id: str = None, activate: bool = True, read_doc: bool = True) -> None: ... - def close_tabs(self, tab_ids: Union[str, List[str], Tuple[str]] = ..., others: bool = ...) -> None: ... + def close_tabs(self, tab_ids: Union[str, List[str], Tuple[str]] = None, others: bool = False) -> None: ... - def close_other_tabs(self, tab_ids: Union[str, List[str], Tuple[str]] = ...) -> None: ... + def close_other_tabs(self, tab_ids: Union[str, List[str], Tuple[str]] = None) -> None: ... - def handle_alert(self, accept: bool = ..., send: str = ..., timeout: float = ...) -> Union[str, None]: ... + def handle_alert(self, accept: bool = True, send: str = None, timeout: float = None) -> Union[str, None]: ... def hide_browser(self) -> None: ... @@ -108,16 +108,16 @@ class WindowSetter(object): def normal(self) -> None: ... - def size(self, width: int = ..., height: int = ...) -> None: ... + def size(self, width: int = None, height: int = None) -> None: ... - def location(self, x: int = ..., y: int = ...) -> None: ... + def location(self, x: int = None, y: int = None) -> None: ... def _get_info(self) -> dict: ... def _perform(self, bounds: dict) -> None: ... -def show_or_hide_browser(page: ChromiumPage, hide: bool = ...) -> None: ... +def show_or_hide_browser(page: ChromiumPage, hide: bool = True) -> None: ... def get_browser_progress_id(progress: Union[popen, None], address: str) -> Union[str, None]: ... diff --git a/DrissionPage/chromium_tab.pyi b/DrissionPage/chromium_tab.pyi index 7bab21a..932cba0 100644 --- a/DrissionPage/chromium_tab.pyi +++ b/DrissionPage/chromium_tab.pyi @@ -9,7 +9,7 @@ from .chromium_page import ChromiumPage class ChromiumTab(ChromiumBase): - def __init__(self, page:ChromiumPage, tab_id: str = ...): + def __init__(self, page: ChromiumPage, tab_id: str = None): self.page: ChromiumPage = ... def _set_options(self) -> None: ... diff --git a/DrissionPage/common.py b/DrissionPage/common.py index a465896..5a7692e 100644 --- a/DrissionPage/common.py +++ b/DrissionPage/common.py @@ -18,7 +18,7 @@ from requests import get as requests_get from .config import DriverOptions -def get_ele_txt(e) -> str: +def get_ele_txt(e): """获取元素内所有文本 :param e: 元素对象 :return: 元素内所有文本 @@ -85,7 +85,7 @@ def get_ele_txt(e) -> str: return format_html(re_str) -def get_loc(loc: Union[tuple, str], translate_css: bool = False) -> tuple: +def get_loc(loc, translate_css=False): """接收selenium定位元组或本库定位语法,转换为标准定位元组,可翻译css selector为xpath \n :param loc: selenium定位元组或本库定位语法 :param translate_css: 是否翻译css selector为xpath @@ -112,7 +112,7 @@ def get_loc(loc: Union[tuple, str], translate_css: bool = False) -> tuple: return loc -def str_to_loc(loc: str) -> tuple: +def str_to_loc(loc): """处理元素查找语句 \n 查找方式:属性、tag name及属性、文本、xpath、css selector、id、class \n @表示属性,.表示class,#表示id,=表示精确匹配,:表示模糊匹配,无控制字符串时默认搜索该字符串 \n @@ -280,7 +280,7 @@ def _make_search_str(search_str: str) -> str: return search_str -def translate_loc(loc: tuple) -> tuple: +def translate_loc(loc): """把By类型的loc元组转换为css selector或xpath类型的 \n :param loc: By类型的loc元组 :return: css selector或xpath类型的loc元组 @@ -322,7 +322,7 @@ def translate_loc(loc: tuple) -> tuple: return loc_by, loc_str -def format_html(text: str) -> str: +def format_html(text): """处理html编码字符 \n :param text: html文本 :return: 格式化后的html文本 @@ -330,7 +330,7 @@ def format_html(text: str) -> str: return unescape(text).replace('\xa0', ' ') if text else text -def clean_folder(folder_path: str, ignore: list = None) -> None: +def clean_folder(folder_path, ignore=None): """清空一个文件夹,除了ignore里的文件和文件夹 \n :param folder_path: 要清空的文件夹路径 :param ignore: 忽略列表 @@ -347,7 +347,7 @@ def clean_folder(folder_path: str, ignore: list = None) -> None: rmtree(f, True) -def unzip(zip_path: str, to_path: str) -> Union[list, None]: +def unzip(zip_path, to_path): """解压下载的chromedriver.zip文件""" if not zip_path: return @@ -356,7 +356,7 @@ def unzip(zip_path: str, to_path: str) -> Union[list, None]: return [f.extract(f.namelist()[0], path=to_path)] -def get_exe_path_from_port(port: Union[str, int]) -> Union[str, None]: +def get_exe_path_from_port(port): """获取端口号第一条进程的可执行文件路径 \n :param port: 端口号 :return: 可执行文件的绝对路径 @@ -371,7 +371,7 @@ def get_exe_path_from_port(port: Union[str, int]) -> Union[str, None]: return file_lst[2].strip() if len(file_lst) > 2 else None -def get_pid_from_port(port: Union[str, int]) -> Union[str, None]: +def get_pid_from_port(port): """获取端口号第一条进程的pid \n :param port: 端口号 :return: 进程id @@ -396,7 +396,7 @@ def get_pid_from_port(port: Union[str, int]) -> Union[str, None]: return None -def get_usable_path(path: Union[str, Path]) -> Path: +def get_usable_path(path): """检查文件或文件夹是否有重名,并返回可以使用的路径 \n :param path: 文件或文件夹路径 :return: 可用的路径,Path对象 @@ -424,7 +424,7 @@ def get_usable_path(path: Union[str, Path]) -> Path: return path -def make_valid_name(full_name: str) -> str: +def make_valid_name(full_name): """获取有效的文件名 \n :param full_name: 文件名 :return: 可用的文件名 @@ -450,7 +450,7 @@ def make_valid_name(full_name: str) -> str: return sub(r'[<>/\\|:*?\n]', '', full_name) -def get_long(txt) -> int: +def get_long(txt): """返回字符串中字符个数(一个汉字是2个字符) \n :param txt: 字符串 :return: 字符个数 @@ -459,7 +459,7 @@ def get_long(txt) -> int: return int((len(txt.encode('utf-8')) - txt_len) / 2 + txt_len) -def make_absolute_link(link, page=None) -> str: +def make_absolute_link(link, page=None): """获取绝对url :param link: 超链接 :param page: 页面对象 @@ -484,7 +484,7 @@ def make_absolute_link(link, page=None) -> str: return link -def is_js_func(func: str) -> bool: +def is_js_func(func): """检查文本是否js函数""" func = func.strip() if func.startswith('function') or func.startswith('async '): @@ -514,7 +514,7 @@ def _port_is_using(ip: str, port: str) -> Union[bool, None]: s.close() -def connect_chrome(option: DriverOptions) -> tuple: +def connect_chrome(option): """连接或启动chrome \n :param option: DriverOptions对象 :return: chrome 路径和进程对象组成的元组 @@ -543,8 +543,8 @@ def connect_chrome(option: DriverOptions) -> tuple: # 传入的路径找不到,主动在ini文件、注册表、系统变量中找 except FileNotFoundError: - from DrissionPage.easy_set import _get_chrome_path - chrome_path = _get_chrome_path(show_msg=False) + from DrissionPage.easy_set import get_chrome_path + chrome_path = get_chrome_path(show_msg=False) if not chrome_path: raise FileNotFoundError('无法找到chrome路径,请手动配置。') @@ -659,7 +659,7 @@ def _set_value_to_dict(target_dict: dict, src: list, value) -> None: exec(src) -def _location_in_viewport(page, loc_x: int, loc_y: int) -> bool: +def location_in_viewport(page, loc_x, loc_y): """判断给定的坐标是否在视口中 |n :param page: ChromePage对象 :param loc_x: 页面绝对坐标x diff --git a/DrissionPage/common.pyi b/DrissionPage/common.pyi new file mode 100644 index 0000000..566848c --- /dev/null +++ b/DrissionPage/common.pyi @@ -0,0 +1,59 @@ +# -*- coding:utf-8 -*- +""" +@Author : g1879 +@Contact : g1879@qq.com +""" +from pathlib import Path +from typing import Union +from requests import get as requests_get + +from .base import BasePage, DrissionElement +from .config import DriverOptions + + +def get_ele_txt(e: DrissionElement) -> str: ... + + +def get_loc(loc: Union[tuple, str], translate_css: bool = False) -> tuple: ... + + +def str_to_loc(loc: str) -> tuple: ... + + +def translate_loc(loc: tuple) -> tuple: ... + + +def format_html(text: str) -> str: ... + + +def clean_folder(folder_path: str, ignore: list = None) -> None: ... + + +def unzip(zip_path: str, to_path: str) -> Union[list, None]: ... + + +def get_exe_path_from_port(port: Union[str, int]) -> Union[str, None]: ... + + +def get_pid_from_port(port: Union[str, int]) -> Union[str, None]: ... + + +def get_usable_path(path: Union[str, Path]) -> Path: ... + + +def make_valid_name(full_name: str) -> str: ... + + +def get_long(txt) -> int: ... + + +def make_absolute_link(link, page: BasePage = None) -> str: ... + + +def is_js_func(func: str) -> bool: ... + + +def connect_chrome(option: DriverOptions) -> tuple: ... + + +def location_in_viewport(page, loc_x: int, loc_y: int) -> bool: ... diff --git a/DrissionPage/config.pyi b/DrissionPage/config.pyi index 4dded5d..536c829 100644 --- a/DrissionPage/config.pyi +++ b/DrissionPage/config.pyi @@ -13,7 +13,7 @@ from selenium.webdriver.chrome.options import Options class OptionsManager(object): - def __init__(self, path: str = ...): + def __init__(self, path: str = None): self.ini_path: str = ... self._conf: RawConfigParser = ... self._paths: dict = ... @@ -37,13 +37,13 @@ class OptionsManager(object): def set_item(self, section: str, item: str, value: Any) -> OptionsManager: ... - def save(self, path: str = ...) -> str: ... + def save(self, path: str = None) -> str: ... def save_to_default(self) -> str: ... class SessionOptions(object): - def __init__(self, read_file: bool = ..., ini_path: str = ...): + def __init__(self, read_file: bool = True, ini_path: str = None): self.ini_path: str = ... self._headers: dict = ... self._cookies: list = ... @@ -138,7 +138,7 @@ class SessionOptions(object): def set_proxies(self, proxies: dict) -> 'SessionOptions': ... - def save(self, path: str = ...) -> str: ... + def save(self, path: str = None) -> str: ... def save_to_default(self) -> str: ... @@ -147,7 +147,7 @@ class SessionOptions(object): class DriverOptions(Options): - def __init__(self, read_file: bool = ..., ini_path: str = ...): + def __init__(self, read_file: bool = True, ini_path: str = None): self.ini_path: str = ... self._driver_path: str = ... self._user_data_path: str = ... @@ -174,7 +174,7 @@ class DriverOptions(Options): # -------------重写父类方法结束------------- - def save(self, path: str = ...) -> str: ... + def save(self, path: str = None) -> str: ... def save_to_default(self) -> str: ... @@ -186,15 +186,15 @@ class DriverOptions(Options): def set_argument(self, arg: str, value: Union[bool, str]) -> 'DriverOptions': ... - def set_timeouts(self, implicit: float = ..., pageLoad: float = ..., script: float = ...) -> 'DriverOptions': ... + def set_timeouts(self, implicit: float = None, pageLoad: float = None, script: float = None) -> 'DriverOptions': ... - def set_headless(self, on_off: bool = ...) -> 'DriverOptions': ... + def set_headless(self, on_off: bool = True) -> 'DriverOptions': ... - def set_no_imgs(self, on_off: bool = ...) -> 'DriverOptions': ... + def set_no_imgs(self, on_off: bool = True) -> 'DriverOptions': ... - def set_no_js(self, on_off: bool = ...) -> 'DriverOptions': ... + def set_no_js(self, on_off: bool = True) -> 'DriverOptions': ... - def set_mute(self, on_off: bool = ...) -> 'DriverOptions': ... + def set_mute(self, on_off: bool = True) -> 'DriverOptions': ... def set_user_agent(self, user_agent: str) -> 'DriverOptions': ... @@ -203,13 +203,13 @@ class DriverOptions(Options): def set_page_load_strategy(self, value: str) -> 'DriverOptions': ... def set_paths(self, - driver_path: str = ..., - chrome_path: str = ..., - local_port: Union[int, str] = ..., - debugger_address: str = ..., - download_path: str = ..., - user_data_path: str = ..., - cache_path: str = ...) -> 'DriverOptions': ... + driver_path: str = None, + chrome_path: str = None, + local_port: Union[int, str] = None, + debugger_address: str = None, + download_path: str = None, + user_data_path: str = None, + cache_path: str = None) -> 'DriverOptions': ... def as_dict(self) -> dict: ... diff --git a/DrissionPage/configs.ini b/DrissionPage/configs.ini index 763cf8b..e92b7ab 100644 --- a/DrissionPage/configs.ini +++ b/DrissionPage/configs.ini @@ -1,10 +1,10 @@ [paths] -chromedriver_path = +chromedriver_path = D:\coding\projects\DrissionPage\DrissionPage\chromedriver.exe tmp_path = [chrome_options] debugger_address = 127.0.0.1:9222 -binary_location = chrome +binary_location = C:\Program Files\Google\Chrome\Application\chrome.exe arguments = ['--no-first-run', '--no-sandbox', '--disable-gpu', '--ignore-certificate-errors', '--disable-infobars', '--disable-popup-blocking'] extensions = [] experimental_options = {'prefs': {'profile.default_content_settings.popups': 0, 'profile.default_content_setting_values': {'notifications': 2}, 'plugins.plugins_list': [{'enabled': False, 'name': 'Chrome PDF Viewer'}]}, 'useAutomationExtension': False, 'excludeSwitches': ['enable-automation']} diff --git a/DrissionPage/drission.py b/DrissionPage/drission.py index b8b9dad..a509783 100644 --- a/DrissionPage/drission.py +++ b/DrissionPage/drission.py @@ -395,10 +395,10 @@ def create_driver(chrome_path, driver_path, options): except (WebDriverException, SessionNotCreatedException): print('打开失败,尝试获取driver。\n') from .easy_set import get_match_driver - from DrissionPage.easy_set import _get_chrome_path + from DrissionPage.easy_set import get_chrome_path if chrome_path == 'chrome': - chrome_path = _get_chrome_path(show_msg=False, from_ini=False) + chrome_path = get_chrome_path(show_msg=False, from_ini=False) if chrome_path: driver_path = get_match_driver(chrome_path=chrome_path, check_version=False, show_msg=True) diff --git a/DrissionPage/drission.pyi b/DrissionPage/drission.pyi index d328b2b..a6a0271 100644 --- a/DrissionPage/drission.pyi +++ b/DrissionPage/drission.pyi @@ -18,10 +18,10 @@ from .config import SessionOptions, DriverOptions class Drission(object): def __init__(self, - driver_or_options: Union[RemoteWebDriver, Options, DriverOptions, bool] = ..., - session_or_options: Union[Session, dict, SessionOptions, bool] = ..., - ini_path: str = ..., - proxy: dict = ...): + driver_or_options: Union[RemoteWebDriver, Options, DriverOptions, bool] = None, + session_or_options: Union[Session, dict, SessionOptions, bool] = None, + ini_path: str = None, + proxy: dict = None): self._session: Session = ... self._session_options: dict = ... self._proxy: dict = ... @@ -50,7 +50,7 @@ class Drission(object): def proxy(self) -> Union[None, dict]: ... @proxy.setter - def proxy(self, proxies: dict = ...) -> None: ... + def proxy(self, proxies: dict = None) -> None: ... @property def debugger_progress(self): ... @@ -63,12 +63,12 @@ class Drission(object): def show_browser(self) -> None: ... - def _show_or_hide_browser(self, hide: bool = ...) -> None: ... + def _show_or_hide_browser(self, hide: bool = True) -> None: ... def set_cookies(self, cookies: Union[RequestsCookieJar, list, tuple, str, dict], - set_session: bool = ..., - set_driver: bool = ...) -> None: ... + set_session: bool = False, + set_driver: bool = False) -> None: ... def _set_session(self, data: dict) -> None: ... @@ -76,7 +76,7 @@ class Drission(object): def cookies_to_driver(self, url: str) -> None: ... - def close_driver(self, kill: bool = ...) -> None: ... + def close_driver(self, kill: bool = False) -> None: ... def close_session(self) -> None: ... @@ -89,7 +89,7 @@ def user_agent_to_session(driver: RemoteWebDriver, session: Session) -> None: .. def create_driver(chrome_path: str, driver_path: str, options: Options) -> WebDriver: ... -def get_chrome_hwnds_from_pid(pid:str) -> list: ... +def get_chrome_hwnds_from_pid(pid: str) -> list: ... -def kill_progress(pid: str = ..., port: int = ...) -> bool: ... +def kill_progress(pid: str = None, port: int = None) -> bool: ... diff --git a/DrissionPage/driver_element.py b/DrissionPage/driver_element.py index da3fb4c..d0e1552 100644 --- a/DrissionPage/driver_element.py +++ b/DrissionPage/driver_element.py @@ -133,7 +133,7 @@ class DriverElement(DrissionElement): """ return make_session_ele(self, loc_or_str) - def s_eles(self, loc_or_str=None): + def s_eles(self, loc_or_str): """查找所有符合条件的元素以SessionElement列表形式返回 \n :param loc_or_str: 定位符 :return: SessionElement或属性、文本组成的列表 @@ -1136,7 +1136,7 @@ class ElementWaiter(object): return self._wait_ele('hidden') def _wait_ele(self, mode): - """执行等待 + """执行等待 \n :param mode: 等待模式 :return: 是否等待成功 """ diff --git a/DrissionPage/driver_element.pyi b/DrissionPage/driver_element.pyi index 1a07688..98fa815 100644 --- a/DrissionPage/driver_element.pyi +++ b/DrissionPage/driver_element.pyi @@ -18,7 +18,7 @@ from .session_element import SessionElement class DriverElement(DrissionElement): - def __init__(self, ele: WebElement, page: Union[DriverPage, MixPage] = ...): + def __init__(self, ele: WebElement, page: Union[DriverPage, MixPage] = None): self._inner_ele: WebElement = ... self._select: Select = ... self._scroll: Scroll = ... @@ -28,7 +28,7 @@ class DriverElement(DrissionElement): def __call__(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> Union['DriverElement', str, None]: ... + timeout: float = None) -> Union['DriverElement', str, None]: ... # -----------------共有属性和方法------------------- @property @@ -56,21 +56,21 @@ class DriverElement(DrissionElement): def ele(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> Union['DriverElement', str, None]: ... + timeout: float = None) -> Union['DriverElement', str, None]: ... def eles(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> List[Union['DriverElement', str]]: ... + timeout: float = None) -> List[Union['DriverElement', str]]: ... - def s_ele(self, loc_or_str: Union[Tuple[str, str], str] = ...) -> Union[SessionElement, str, None]: ... + def s_ele(self, loc_or_str: Union[Tuple[str, str], str] = None) -> Union[SessionElement, str, None]: ... - def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = ...) -> List[Union[SessionElement, str]]: ... + def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[Union[SessionElement, str]]: ... def _ele(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ..., - single: bool = ..., - relative: bool = ...) -> Union['DriverElement', str, None, List[Union['DriverElement', str]]]: ... + timeout: float = None, + single: bool = True, + relative: bool = False) -> Union['DriverElement', str, None, List[Union['DriverElement', str]]]: ... def _get_ele_path(self, mode) -> str: ... @@ -99,92 +99,92 @@ class DriverElement(DrissionElement): @property def scroll(self) -> Scroll: ... - def parent(self, level_or_loc: Union[tuple, str, int] = ...) -> Union['DriverElement', None]: ... + def parent(self, level_or_loc: Union[tuple, str, int] = 1) -> Union['DriverElement', None]: ... def prev(self, - index: int = ..., - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> Union['DriverElement', str, None]: ... + index: int = 1, + filter_loc: Union[tuple, str] = '', + timeout: float = 0) -> Union['DriverElement', str, None]: ... def next(self, - index: int = ..., - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> Union['DriverElement', str, None]: ... + index: int = 1, + filter_loc: Union[tuple, str] = '', + timeout: float = 0) -> Union['DriverElement', str, None]: ... def before(self, - index: int = ..., - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> Union['DriverElement', str, None]: ... + index: int = 1, + filter_loc: Union[tuple, str] = '', + timeout: float = None) -> Union['DriverElement', str, None]: ... def after(self, - index: int = ..., - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> Union['DriverElement', str, None]: ... + index: int = 1, + filter_loc: Union[tuple, str] = '', + timeout: float = None) -> Union['DriverElement', str, None]: ... def prevs(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union['DriverElement', str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = 0) -> List[Union['DriverElement', str]]: ... def nexts(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union['DriverElement', str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = 0) -> List[Union['DriverElement', str]]: ... def befores(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union['DriverElement', str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = None) -> List[Union['DriverElement', str]]: ... def afters(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union['DriverElement', str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = None) -> List[Union['DriverElement', str]]: ... - def left(self, index: int = ..., filter_loc: Union[tuple, str] = ...) -> DriverElement: ... + def left(self, index: int = 1, filter_loc: Union[tuple, str] = '') -> DriverElement: ... - def right(self, index: int = ..., filter_loc: Union[tuple, str] = ...) -> 'DriverElement': ... + def right(self, index: int = 1, filter_loc: Union[tuple, str] = '') -> 'DriverElement': ... - def above(self, index: int = ..., filter_loc: Union[tuple, str] = ...) -> 'DriverElement': ... + def above(self, index: int = 1, filter_loc: Union[tuple, str] = '') -> 'DriverElement': ... - def below(self, index: int = ..., filter_loc: Union[tuple, str] = ...) -> 'DriverElement': ... + def below(self, index: int = 1, filter_loc: Union[tuple, str] = '') -> 'DriverElement': ... - def near(self, index: int = ..., filter_loc: Union[tuple, str] = ...) -> 'DriverElement': ... + def near(self, index: int = 1, filter_loc: Union[tuple, str] = '') -> 'DriverElement': ... - def lefts(self, filter_loc: Union[tuple, str] = ...) -> List['DriverElement']: ... + def lefts(self, filter_loc: Union[tuple, str] = '') -> List['DriverElement']: ... - def rights(self, filter_loc: Union[tuple, str] = ...) -> List['DriverElement']: ... + def rights(self, filter_loc: Union[tuple, str] = '') -> List['DriverElement']: ... - def aboves(self, filter_loc: Union[tuple, str] = ...) -> List['DriverElement']: ... + def aboves(self, filter_loc: Union[tuple, str] = '') -> List['DriverElement']: ... - def belows(self, filter_loc: Union[tuple, str] = ...) -> List['DriverElement']: ... + def belows(self, filter_loc: Union[tuple, str] = '') -> List['DriverElement']: ... - def nears(self, filter_loc: Union[tuple, str] = ...) -> List['DriverElement']: ... + def nears(self, filter_loc: Union[tuple, str] = '') -> List['DriverElement']: ... def wait_ele(self, loc_or_ele: Union[str, tuple, DrissionElement, WebElement], - timeout: float = ...) -> 'ElementWaiter': ... + timeout: float = None) -> 'ElementWaiter': ... - def style(self, style: str, pseudo_ele: str = ...) -> str: ... + def style(self, style: str, pseudo_ele: str = '') -> str: ... - def click(self, by_js: bool = ..., timeout: float = ...) -> bool: ... + def click(self, by_js: bool = None, timeout: float = None) -> bool: ... def click_at(self, - x: Union[int, str] = ..., - y: Union[int, str] = ..., - by_js: bool = ...) -> None: ... + x: Union[int, str] = None, + y: Union[int, str] = None, + by_js: bool = False) -> None: ... def r_click(self) -> None: ... - def r_click_at(self, x: Union[int, str] = ..., y: Union[int, str] = ...) -> None: ... + def r_click_at(self, x: Union[int, str] = None, y: Union[int, str] = None) -> None: ... def input(self, vals: Union[str, tuple], - clear: bool = ..., - insure: bool = ..., - timeout: float = ...) -> bool: ... + clear: bool = True, + insure: bool = True, + timeout: float = None) -> bool: ... def run_script(self, script: str, *args) -> Any: ... def submit(self) -> Union[bool, None]: ... - def clear(self, insure: bool = ...) -> Union[None, bool]: ... + def clear(self, insure: bool = True) -> Union[None, bool]: ... def is_selected(self) -> bool: ... @@ -194,7 +194,7 @@ class DriverElement(DrissionElement): def is_valid(self) -> bool: ... - def screenshot(self, path: str = ..., filename: str = ..., as_bytes: bool = ...) -> Union[str, bytes]: ... + def screenshot(self, path: str = None, filename: str = None, as_bytes: bool = False) -> Union[str, bytes]: ... def prop(self, prop: str) -> str: ... @@ -204,29 +204,29 @@ class DriverElement(DrissionElement): def remove_attr(self, attr: str) -> bool: ... - def drag(self, x: int, y: int, speed: int = ..., shake: bool = ...) -> None: ... + def drag(self, x: int, y: int, speed: int = 40, shake: bool = True) -> None: ... def drag_to(self, ele_or_loc: Union[tuple, WebElement, DrissionElement], - speed: int = ..., - shake: bool = ...) -> None: ... + speed: int = 40, + shake: bool = True) -> None: ... - def hover(self, x: int = ..., y: int = ...) -> None: ... + def hover(self, x: int = None, y: int = None) -> None: ... def _get_relative_eles(self, mode: str, - loc: Union[tuple, str] = ...) -> Union[List['DriverElement'], 'DriverElement']: ... + loc: Union[tuple, str] = '') -> Union[List['DriverElement'], 'DriverElement']: ... def make_driver_ele(page_or_ele: Union[DriverPage, MixPage, DriverElement, ShadowRootElement], loc: Union[str, Tuple[str, str]], - single: bool = ..., - timeout: float = ...) -> Union[DriverElement, str, None, List[Union[DriverElement, str]]]: ... + single: bool = True, + timeout: float = None) -> Union[DriverElement, str, None, List[Union[DriverElement, str]]]: ... class ElementsByXpath(object): - def __init__(self, page, xpath: str = ..., single: bool = ..., timeout: float = ...): + def __init__(self, page, xpath: str = None, single: bool = False, timeout: float = 10): self.single: bool = ... self.xpath: str = ... self.page: Union[MixPage, DriverPage] = ... @@ -241,7 +241,7 @@ class Select(object): self.select_ele: SeleniumSelect = ... self.inner_ele: DriverElement = ... - def __call__(self, text_or_index: Union[str, int, list, tuple], timeout: float = ...) -> bool: ... + def __call__(self, text_or_index: Union[str, int, list, tuple], timeout: float = None) -> bool: ... @property def is_multi(self) -> bool: ... @@ -257,26 +257,26 @@ class Select(object): def clear(self) -> None: ... - def select(self, text_or_index: Union[str, int, list, tuple], timeout: float = ...) -> bool: ... + def select(self, text_or_index: Union[str, int, list, tuple], timeout: float = None) -> bool: ... - def select_by_value(self, value: Union[str, list, tuple], timeout: float = ...) -> bool: ... + def select_by_value(self, value: Union[str, list, tuple], timeout: float = None) -> bool: ... - def deselect(self, text_or_index: Union[str, int, list, tuple], timeout: float = ...) -> bool: ... + def deselect(self, text_or_index: Union[str, int, list, tuple], timeout: float = None) -> bool: ... - def deselect_by_value(self, value: Union[str, list, tuple], timeout: float = ...) -> bool: ... + def deselect_by_value(self, value: Union[str, list, tuple], timeout: float = None) -> bool: ... def invert(self) -> None: ... def _select(self, text_value_index: Union[str, int, list, tuple] = ..., - para_type: str = ..., - deselect: bool = ..., - timeout: float = ...) -> bool: ... + para_type: str = 'text', + deselect: bool = False, + timeout: float = None) -> bool: ... def _select_multi(self, - text_value_index: Union[list, tuple] = ..., - para_type: str = ..., - deselect: bool = ...) -> bool: ... + text_value_index: Union[list, tuple] = None, + para_type: str = 'text', + deselect: bool = False) -> bool: ... class ElementWaiter(object): @@ -284,7 +284,7 @@ class ElementWaiter(object): def __init__(self, page_or_ele, loc_or_ele: Union[str, tuple, DriverElement, WebElement], - timeout: float = ...): + timeout: float = None): self.target: Union[DriverElement, WebElement, tuple] = ... self.timeout: float = ... self.driver: Union[WebElement, RemoteWebDriver] = ... @@ -317,10 +317,10 @@ class Scroll(object): def to_location(self, x: int, y: int) -> None: ... - def up(self, pixel: int = ...) -> None: ... + def up(self, pixel: int = 300) -> None: ... - def down(self, pixel: int = ...) -> None: ... + def down(self, pixel: int = 300) -> None: ... - def left(self, pixel: int = ...) -> None: ... + def left(self, pixel: int = 300) -> None: ... - def right(self, pixel: int = ...) -> None: ... + def right(self, pixel: int = 300) -> None: ... diff --git a/DrissionPage/driver_page.py b/DrissionPage/driver_page.py index 9a84e6a..3610993 100644 --- a/DrissionPage/driver_page.py +++ b/DrissionPage/driver_page.py @@ -101,7 +101,7 @@ class DriverPage(BasePage): else: return make_session_ele(self, loc_or_ele) - def s_eles(self, loc_or_str=None): + def s_eles(self, loc_or_str): """查找所有符合条件的元素以SessionElement列表形式返回 \n :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 :return: SessionElement对象组成的列表 diff --git a/DrissionPage/driver_page.pyi b/DrissionPage/driver_page.pyi index 1e91dc8..542a402 100644 --- a/DrissionPage/driver_page.pyi +++ b/DrissionPage/driver_page.pyi @@ -18,14 +18,14 @@ from .session_element import SessionElement class DriverPage(BasePage): - def __init__(self, driver: RemoteWebDriver, timeout: float = ...) -> None: + def __init__(self, driver: RemoteWebDriver, timeout: float = 10) -> None: self._driver: RemoteWebDriver = ... self._url: str = ... self._wait_object: WebDriverWait = ... self._scroll: Scroll = ... def __call__(self, loc_or_str: Union[Tuple[str, str], str, DriverElement, WebElement], - timeout: float = ...) -> Union[DriverElement, str, None]: ... + timeout: float = None) -> Union[DriverElement, str, None]: ... # -----------------共有属性和方法------------------- @property @@ -39,29 +39,29 @@ class DriverPage(BasePage): def get(self, url: str, - show_errmsg: bool = ..., - retry: int = ..., - interval: float = ...) -> Union[None, bool]: ... + show_errmsg: bool = False, + retry: int = None, + interval: float = None) -> Union[None, bool]: ... def ele(self, loc_or_ele: Union[Tuple[str, str], str, DriverElement, WebElement], - timeout: float = ...) -> Union[DriverElement, str, None]: ... + timeout: float = None) -> Union[DriverElement, str, None]: ... def eles(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> List[Union[DriverElement, str]]: ... + timeout: float = None) -> List[Union[DriverElement, str]]: ... - def s_ele(self, loc_or_ele: Union[Tuple[str, str], str, DriverElement] = ...) -> Union[ - SessionElement, str, None]: ... + def s_ele(self, loc_or_ele: Union[Tuple[str, str], str, DriverElement] = None) \ + -> Union[SessionElement, str, None]: ... - def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = ...) -> List[Union[SessionElement, str]]: ... + def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[Union[SessionElement, str]]: ... def _ele(self, loc_or_ele: Union[Tuple[str, str], str, DriverElement, WebElement], - timeout: float = ..., - single: bool = ...) -> Union[DriverElement, str, None, List[Union[DriverElement, str]]]: ... + timeout: float = None, + single: bool = True) -> Union[DriverElement, str, None, List[Union[DriverElement, str]]]: ... - def get_cookies(self, as_dict: bool = ...) -> Union[list, dict]: ... + def get_cookies(self, as_dict: bool = False) -> Union[list, dict]: ... @property def timeout(self) -> float: ... @@ -71,9 +71,9 @@ class DriverPage(BasePage): def _d_connect(self, to_url: str, - times: int = ..., - interval: float = ..., - show_errmsg: bool = ...) -> Union[bool, None]: ... + times: int = 0, + interval: float = 1, + show_errmsg: bool = False) -> Union[bool, None]: ... # ----------------driver独有属性和方法----------------------- @property @@ -106,11 +106,11 @@ class DriverPage(BasePage): @property def to_frame(self) -> ToFrame: ... - def set_timeouts(self, implicit: float = ..., pageLoad: float = ..., script: float = ...) -> None: ... + def set_timeouts(self, implicit: float = None, pageLoad: float = None, script: float = None) -> None: ... def wait_ele(self, loc_or_ele: Union[str, tuple, DriverElement, WebElement], - timeout: float = ...) -> ElementWaiter: ... + timeout: float = None) -> ElementWaiter: ... def check_page(self) -> Union[bool, None]: ... @@ -120,31 +120,31 @@ class DriverPage(BasePage): def run_cdp(self, cmd: str, **cmd_args) -> Any: ... - def create_tab(self, url: str = ...) -> None: ... + def create_tab(self, url: str = '') -> None: ... - def close_tabs(self, num_or_handles: Union[int, str, list, tuple] = ...) -> None: ... + def close_tabs(self, num_or_handles: Union[int, str, list, tuple] = None) -> None: ... - def close_other_tabs(self, num_or_handles: Union[int, str, list, tuple] = ...) -> None: ... + def close_other_tabs(self, num_or_handles: Union[int, str, list, tuple] = None) -> None: ... - def to_tab(self, num_or_handle: Union[int, str] = ...) -> None: ... + def to_tab(self, num_or_handle: Union[int, str] = 0) -> None: ... def set_ua_to_tab(self, ua: str) -> None: ... - def get_session_storage(self, item: str = ...) -> Union[str, dict, None]: ... + def get_session_storage(self, item: str = None) -> Union[str, dict, None]: ... - def get_local_storage(self, item: str = ...) -> Union[str, dict, None]: ... + def get_local_storage(self, item: str = None) -> Union[str, dict, None]: ... def set_session_storage(self, item: str, value: Union[str, bool]) -> None: ... def set_local_storage(self, item: str, value: Union[str, bool]) -> None: ... def clean_cache(self, - session_storage: bool = ..., - local_storage: bool = ..., - cache: bool = ..., - cookies: bool = ...) -> None: ... + session_storage: bool = True, + local_storage: bool = True, + cache: bool = True, + cookies: bool = True) -> None: ... - def screenshot(self, path: str = ..., filename: str = ..., as_bytes: bool = ...) -> Union[str, bytes]: ... + def screenshot(self, path: str = None, filename: str = None, as_bytes: bool = False) -> Union[str, bytes]: ... def scroll_to_see(self, loc_or_ele: Union[str, tuple, WebElement, DriverElement]) -> None: ... @@ -156,11 +156,11 @@ class DriverPage(BasePage): def forward(self) -> None: ... - def set_window_size(self, width: int = ..., height: int = ...) -> None: ... + def set_window_size(self, width: int = None, height: int = None) -> None: ... def chrome_downloading(self, download_path: str) -> list: ... - def process_alert(self, ok: bool = ..., send: str = ..., timeout: float = ...) -> Union[str, None]: ... + def process_alert(self, ok: bool = True, send: str = None, timeout: float = None) -> Union[str, None]: ... class ToFrame(object): @@ -168,11 +168,12 @@ class ToFrame(object): def __init__(self, page: DriverPage): self.page: DriverPage = ... - def __call__(self, condition: Union[int, str, tuple, WebElement, DriverElement] = ...)->Union[DriverPage, MixPage]: ... + def __call__(self, condition: Union[int, str, tuple, WebElement, DriverElement] = 'main') -> Union[ + DriverPage, MixPage]: ... def main(self) -> DriverPage: ... - def parent(self, level: int = ...) -> DriverPage: ... + def parent(self, level: int = 1) -> DriverPage: ... def by_id(self, id_: str) -> DriverPage: ... diff --git a/DrissionPage/easy_set.py b/DrissionPage/easy_set.py index cdafd2d..e5d9171 100644 --- a/DrissionPage/easy_set.py +++ b/DrissionPage/easy_set.py @@ -17,7 +17,7 @@ from .drission import Drission from .session_page import SessionPage -def show_settings(ini_path: str = None) -> None: +def show_settings(ini_path=None): """打印ini文件内容""" om = OptionsManager(ini_path) print('paths:') @@ -28,16 +28,16 @@ def show_settings(ini_path: str = None) -> None: pprint(om.get_option('session_options')) -def set_paths(driver_path: str = None, - chrome_path: str = None, - local_port: Union[int, str] = None, - debugger_address: str = None, - tmp_path: str = None, - download_path: str = None, - user_data_path: str = None, - cache_path: str = None, - ini_path: str = None, - check_version: bool = False) -> None: +def set_paths(driver_path=None, + chrome_path=None, + local_port=None, + debugger_address=None, + tmp_path=None, + download_path=None, + user_data_path=None, + cache_path=None, + ini_path=None, + check_version=False): """快捷的路径设置函数 \n :param driver_path: chromedriver.exe路径 :param chrome_path: chrome.exe路径 @@ -88,7 +88,7 @@ def set_paths(driver_path: str = None, check_driver_version(format_path(driver_path), format_path(chrome_path)) -def set_argument(arg: str, value: Union[bool, str], ini_path: str = None) -> None: +def set_argument(arg, value, ini_path=None): """设置浏览器配置argument属性 \n :param arg: 属性名 :param value: 属性值,有值的属性传入值,没有的传入bool @@ -105,7 +105,7 @@ def set_argument(arg: str, value: Union[bool, str], ini_path: str = None) -> Non do.save() -def set_headless(on_off: bool = True, ini_path: str = None) -> None: +def set_headless(on_off=True, ini_path=None): """设置是否隐藏浏览器界面 \n :param on_off: 开或关 :param ini_path: 要修改的ini文件路径 @@ -115,7 +115,7 @@ def set_headless(on_off: bool = True, ini_path: str = None) -> None: set_argument('--headless', on_off, ini_path) -def set_no_imgs(on_off: bool = True, ini_path: str = None) -> None: +def set_no_imgs(on_off=True, ini_path=None): """设置是否禁止加载图片 \n :param on_off: 开或关 :param ini_path: 要修改的ini文件路径 @@ -125,7 +125,7 @@ def set_no_imgs(on_off: bool = True, ini_path: str = None) -> None: set_argument('--blink-settings=imagesEnabled=false', on_off, ini_path) -def set_no_js(on_off: bool = True, ini_path: str = None) -> None: +def set_no_js(on_off=True, ini_path=None): """设置是否禁用js \n :param on_off: 开或关 :param ini_path: 要修改的ini文件路径 @@ -135,7 +135,7 @@ def set_no_js(on_off: bool = True, ini_path: str = None) -> None: set_argument('--disable-javascript', on_off, ini_path) -def set_mute(on_off: bool = True, ini_path: str = None) -> None: +def set_mute(on_off=True, ini_path=None): """设置是否静音 \n :param on_off: 开或关 :param ini_path: 要修改的ini文件路径 @@ -145,7 +145,7 @@ def set_mute(on_off: bool = True, ini_path: str = None) -> None: set_argument('--mute-audio', on_off, ini_path) -def set_user_agent(user_agent: str, ini_path: str = None) -> None: +def set_user_agent(user_agent, ini_path=None): """设置user agent \n :param user_agent: user agent文本 :param ini_path: 要修改的ini文件路径 @@ -154,7 +154,7 @@ def set_user_agent(user_agent: str, ini_path: str = None) -> None: set_argument('user-agent', user_agent, ini_path) -def set_proxy(proxy: str, ini_path: str = None) -> None: +def set_proxy(proxy, ini_path=None): """设置代理 \n :param proxy: 代理网址和端口 :param ini_path: 要修改的ini文件路径 @@ -163,7 +163,7 @@ def set_proxy(proxy: str, ini_path: str = None) -> None: set_argument('--proxy-server', proxy, ini_path) -def check_driver_version(driver_path: str = None, chrome_path: str = None) -> bool: +def check_driver_version(driver_path=None, chrome_path=None): """检查传入的chrome和chromedriver是否匹配 \n :param driver_path: chromedriver.exe路径 :param chrome_path: chrome.exe路径 @@ -194,11 +194,11 @@ def check_driver_version(driver_path: str = None, chrome_path: str = None) -> bo # -------------------------自动识别chrome版本号并下载对应driver------------------------ -def get_match_driver(ini_path: Union[str, None] = 'default', - save_path: str = None, - chrome_path: str = None, - show_msg: bool = True, - check_version: bool = True) -> Union[str, None]: +def get_match_driver(ini_path='default', + save_path=None, + chrome_path=None, + show_msg=True, + check_version=True): """自动识别chrome版本并下载匹配的driver \n :param ini_path: 要读取和修改的ini文件路径 :param save_path: chromedriver保存路径 @@ -209,7 +209,7 @@ def get_match_driver(ini_path: Union[str, None] = 'default', """ save_path = save_path or str(Path(__file__).parent) - chrome_path = chrome_path or _get_chrome_path(ini_path, show_msg) + chrome_path = chrome_path or get_chrome_path(ini_path, show_msg) chrome_path = Path(chrome_path).absolute() if chrome_path else None if show_msg: print('chrome.exe路径', chrome_path) @@ -246,13 +246,17 @@ def get_match_driver(ini_path: Union[str, None] = 'default', return driver_path -def _get_chrome_path(ini_path: str = None, - show_msg: bool = True, - from_ini: bool = True, - from_regedit: bool = True, - from_system_path: bool = True, ) -> Union[str, None]: +def get_chrome_path(ini_path=None, + show_msg=True, + from_ini=True, + from_regedit=True, + from_system_path=True): """从ini文件或系统变量中获取chrome.exe的路径 \n :param ini_path: ini文件路径 + :param show_msg: 是否打印信息 + :param from_ini: 是否从ini文件获取 + :param from_regedit: 是否从注册表获取 + :param from_system_path: 是否从系统路径获取 :return: chrome.exe路径 """ # -----------从ini文件中获取-------------- diff --git a/DrissionPage/easy_set.pyi b/DrissionPage/easy_set.pyi new file mode 100644 index 0000000..6008faf --- /dev/null +++ b/DrissionPage/easy_set.pyi @@ -0,0 +1,60 @@ +# -*- coding:utf-8 -*- +""" +@Author : g1879 +@Contact : g1879@qq.com +""" +from typing import Union + + +def show_settings(ini_path: str = None) -> None: ... + + +def set_paths(driver_path: str = None, + chrome_path: str = None, + local_port: Union[int, str] = None, + debugger_address: str = None, + tmp_path: str = None, + download_path: str = None, + user_data_path: str = None, + cache_path: str = None, + ini_path: str = None, + check_version: bool = False) -> None: ... + + +def set_argument(arg: str, value: Union[bool, str], ini_path: str = None) -> None: ... + + +def set_headless(on_off: bool = True, ini_path: str = None) -> None: ... + + +def set_no_imgs(on_off: bool = True, ini_path: str = None) -> None: ... + + +def set_no_js(on_off: bool = True, ini_path: str = None) -> None: ... + + +def set_mute(on_off: bool = True, ini_path: str = None) -> None: ... + + +def set_user_agent(user_agent: str, ini_path: str = None) -> None: ... + + +def set_proxy(proxy: str, ini_path: str = None) -> None: ... + + +def check_driver_version(driver_path: str = None, chrome_path: str = None) -> bool: ... + + +# -------------------------自动识别chrome版本号并下载对应driver------------------------ +def get_match_driver(ini_path: Union[str, None] = 'default', + save_path: str = None, + chrome_path: str = None, + show_msg: bool = True, + check_version: bool = True) -> Union[str, None]: ... + + +def get_chrome_path(ini_path: str = None, + show_msg: bool = True, + from_ini: bool = True, + from_regedit: bool = True, + from_system_path: bool = True, ) -> Union[str, None]: ... diff --git a/DrissionPage/mix_page.py b/DrissionPage/mix_page.py index e8cddcc..1e65f8c 100644 --- a/DrissionPage/mix_page.py +++ b/DrissionPage/mix_page.py @@ -136,7 +136,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=None): + def s_eles(self, loc_or_str): """查找所有符合条件的元素以SessionElement形式返回,d模式处理复杂页面时效率很高 \n :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 :return: SessionElement对象或属性、文本组成的列表 diff --git a/DrissionPage/mix_page.pyi b/DrissionPage/mix_page.pyi index 3b4af17..cb26b8f 100644 --- a/DrissionPage/mix_page.pyi +++ b/DrissionPage/mix_page.pyi @@ -24,17 +24,17 @@ from .session_page import SessionPage class MixPage(SessionPage, DriverPage, BasePage): def __init__(self, - mode: str = ..., - drission: Union[Drission, str] = ..., - timeout: float = ..., - driver_options: Union[Options, DriverOptions, bool] = ..., - session_options: Union[dict, SessionOptions, bool] = ...) -> None: + mode: str = 'd', + drission: Union[Drission, str] = None, + timeout: float = None, + driver_options: Union[Options, DriverOptions, bool] = None, + session_options: Union[dict, SessionOptions, bool] = None) -> None: self._mode: str = ... self._drission: Drission = ... def __call__(self, loc_or_str: Union[Tuple[str, str], str, DriverElement, SessionElement, WebElement], - timeout: float = ...) -> Union[DriverElement, SessionElement, str, None]: ... + timeout: float = None) -> Union[DriverElement, SessionElement, str, None]: ... # -----------------共有属性和方法------------------- @property @@ -51,9 +51,9 @@ class MixPage(SessionPage, DriverPage, BasePage): def get(self, url: str, - show_errmsg: bool | None = ..., - retry: int | None = ..., - interval: float | None = ..., + show_errmsg: bool | None = False, + retry: int | None = None, + interval: float | None = None, timeout: float | None = ..., params: dict | None = ..., data: Union[dict, str, None] = ..., @@ -71,24 +71,24 @@ class MixPage(SessionPage, DriverPage, BasePage): def ele(self, loc_or_ele: Union[Tuple[str, str], str, DriverElement, SessionElement, WebElement], - timeout: float = ...) -> Union[DriverElement, SessionElement, str, None]: ... + timeout: float = None) -> Union[DriverElement, SessionElement, str, None]: ... def eles(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> List[Union[DriverElement, SessionElement, str]]: ... + timeout: float = None) -> List[Union[DriverElement, SessionElement, str]]: ... - def s_ele(self, loc_or_ele: Union[Tuple[str, str], str, DriverElement, SessionElement] = ...) \ + def s_ele(self, loc_or_ele: Union[Tuple[str, str], str, DriverElement, SessionElement] = None) \ -> Union[SessionElement, str, None]: ... - def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = ...) -> List[Union[SessionElement, str]]: ... + def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[Union[SessionElement, str]]: ... def _ele(self, loc_or_ele: Union[Tuple[str, str], str, DriverElement, SessionElement, WebElement], - timeout: float = ..., single: bool = ...) \ + timeout: float = None, single: bool = False) \ -> Union[DriverElement, SessionElement, str, None, List[Union[SessionElement, str]], List[ Union[DriverElement, str]]]: ... - def get_cookies(self, as_dict: bool = ..., all_domains: bool = ...) -> Union[dict, list]: ... + def get_cookies(self, as_dict: bool = False, all_domains: bool = False) -> Union[dict, list]: ... # ----------------MixPage独有属性和方法----------------------- @property @@ -109,15 +109,15 @@ class MixPage(SessionPage, DriverPage, BasePage): @property def _session_url(self) -> str: ... - def change_mode(self, mode: str = ..., go: bool = ..., copy_cookies: bool = ...) -> None: ... + def change_mode(self, mode: str = None, go: bool = True, copy_cookies: bool = True) -> None: ... - def set_cookies(self, cookies: Union[RequestsCookieJar, list, tuple, str, dict], refresh: bool = ...) -> None: ... + def set_cookies(self, cookies: Union[RequestsCookieJar, list, tuple, str, dict], refresh: bool = True) -> None: ... - def cookies_to_session(self, copy_user_agent: bool = ...) -> None: ... + def cookies_to_session(self, copy_user_agent: bool = False) -> None: ... - def cookies_to_driver(self, url: str = ...) -> None: ... + def cookies_to_driver(self, url: str = None) -> None: ... - def check_page(self, by_requests: bool = ...) -> Union[bool, None]: ... + def check_page(self, by_requests: bool = False) -> Union[bool, None]: ... def close_driver(self) -> None: ... @@ -126,12 +126,12 @@ class MixPage(SessionPage, DriverPage, BasePage): # ----------------重写SessionPage的函数----------------------- def post(self, url: str, - show_errmsg: bool | None = ..., - retry: int | None = ..., - interval: float | None = ..., + data: Union[dict, str, None] = None, + show_errmsg: bool = False, + retry: int | None = None, + interval: float | None = None, timeout: float | None = ..., params: dict | None = ..., - data: Union[dict, str, None] = ..., json: Union[dict, str, None] = ..., headers: dict | None = ..., cookies: Any | None = ..., @@ -147,7 +147,7 @@ class MixPage(SessionPage, DriverPage, BasePage): @property def download(self) -> DownloadKit: ... - def chrome_downloading(self, path: str = ...) -> list: ... + def chrome_downloading(self, path: str = None) -> list: ... # ----------------MixPage独有函数----------------------- def hide_browser(self) -> None: ... diff --git a/DrissionPage/session_element.py b/DrissionPage/session_element.py index 37a73ed..449793d 100644 --- a/DrissionPage/session_element.py +++ b/DrissionPage/session_element.py @@ -80,37 +80,37 @@ class SessionElement(DrissionElement): """ return super().parent(level_or_loc) - def prev(self, index=1, filter_loc='', timeout=0): + def prev(self, filter_loc='', index=1, timeout=0): """返回前面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 \n - :param index: 前面第几个查询结果元素 :param filter_loc: 用于筛选元素的查询语法 + :param index: 前面第几个查询结果元素 :param timeout: 查找元素的超时时间 :return: 兄弟元素 """ return super().prev(index, filter_loc, timeout) - def next(self, index=1, filter_loc='', timeout=0): + def next(self, filter_loc='', index=1, timeout=0): """返回后面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 \n - :param index: 后面第几个查询结果元素 :param filter_loc: 用于筛选元素的查询语法 + :param index: 后面第几个查询结果元素 :param timeout: 查找元素的超时时间 :return: 兄弟元素 """ return super().next(index, filter_loc, timeout) - def before(self, index=1, filter_loc='', timeout=None): + def before(self, filter_loc='', index=1, timeout=None): """返回前面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 \n - :param index: 前面第几个查询结果元素 :param filter_loc: 用于筛选元素的查询语法 + :param index: 前面第几个查询结果元素 :param timeout: 查找元素的超时时间 :return: 本元素前面的某个元素或节点 """ return super().before(index, filter_loc, timeout) - def after(self, index=1, filter_loc='', timeout=None): + def after(self, filter_loc='', index=1, timeout=None): """返回后面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 \n - :param index: 后面第几个查询结果元素 :param filter_loc: 用于筛选元素的查询语法 + :param index: 后面第几个查询结果元素 :param timeout: 查找元素的超时时间 :return: 本元素后面的某个元素或节点 """ @@ -204,7 +204,7 @@ class SessionElement(DrissionElement): """ return self._ele(loc_or_str) - def s_eles(self, loc_or_str=None): + def s_eles(self, loc_or_str): """返回当前元素下级所有符合条件的子元素、属性或节点文本 \n :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 :return: SessionElement对象或属性、文本组成的列表 diff --git a/DrissionPage/session_element.pyi b/DrissionPage/session_element.pyi index 6bfcbad..c33496b 100644 --- a/DrissionPage/session_element.pyi +++ b/DrissionPage/session_element.pyi @@ -7,6 +7,7 @@ from typing import Union, List, Tuple from lxml.html import HtmlElement +from .driver_page import DriverPage from .base import DrissionElement, BaseElement from .chromium_base import ChromiumBase from .chromium_element import ChromiumElement @@ -17,7 +18,7 @@ from .session_page import SessionPage class SessionElement(DrissionElement): - def __init__(self, ele: HtmlElement, page: Union[SessionPage, None] = ...): + def __init__(self, ele: HtmlElement, page: Union[SessionPage, None] = None): self._inner_ele: HtmlElement = ... self.page: SessionPage = ... @@ -28,7 +29,7 @@ class SessionElement(DrissionElement): def __call__(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> Union['SessionElement', str, None]: ... + timeout: float = None) -> Union['SessionElement', str, None]: ... @property def tag(self) -> str: ... @@ -48,70 +49,70 @@ class SessionElement(DrissionElement): @property def raw_text(self) -> str: ... - def parent(self, level_or_loc: Union[tuple, str, int] = ...) -> Union['SessionElement', None]: ... + def parent(self, level_or_loc: Union[tuple, str, int] = 1) -> Union['SessionElement', None]: ... def prev(self, - index: int = ..., - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> Union['SessionElement', str, None]: ... + filter_loc: Union[tuple, str] = '', + index: int = 1, + timeout: float = 0) -> Union['SessionElement', str, None]: ... def next(self, - index: int = ..., - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> Union['SessionElement', str, None]: ... + filter_loc: Union[tuple, str] = '', + index: int = 1, + timeout: float = 0) -> Union['SessionElement', str, None]: ... def before(self, - index: int = ..., - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> Union['SessionElement', str, None]: ... + filter_loc: Union[tuple, str] = '', + index: int = 1, + timeout: float = None) -> Union['SessionElement', str, None]: ... def after(self, - index: int = ..., - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> Union['SessionElement', str, None]: ... + filter_loc: Union[tuple, str] = '', + index: int = 1, + timeout: float = None) -> Union['SessionElement', str, None]: ... def prevs(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union['SessionElement', str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = 0) -> List[Union['SessionElement', str]]: ... def nexts(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union['SessionElement', str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = 0) -> List[Union['SessionElement', str]]: ... def befores(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union['SessionElement', str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = None) -> List[Union['SessionElement', str]]: ... def afters(self, - filter_loc: Union[tuple, str] = ..., - timeout: float = ...) -> List[Union['SessionElement', str]]: ... + filter_loc: Union[tuple, str] = '', + timeout: float = None) -> List[Union['SessionElement', str]]: ... def attr(self, attr: str) -> Union[str, None]: ... def ele(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> Union['SessionElement', str, None]: ... + timeout: float = None) -> Union['SessionElement', str, None]: ... def eles(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> List[Union['SessionElement', str]]: ... + timeout: float = None) -> List[Union['SessionElement', str]]: ... def s_ele(self, - loc_or_str: Union[Tuple[str, str], str] = ...) -> Union['SessionElement', str, None]: ... + loc_or_str: Union[Tuple[str, str], str] = None) -> Union['SessionElement', str, None]: ... def s_eles(self, - loc_or_str: Union[Tuple[str, str], str] = ...) -> List[Union['SessionElement', str]]: ... + loc_or_str: Union[Tuple[str, str], str]) -> List[Union['SessionElement', str]]: ... def _ele(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ..., - single: bool = ..., - relative: bool = ...) -> Union['SessionElement', str, None, List[Union['SessionElement', str]]]: ... + timeout: float = None, + single: bool = True, + relative: bool = False) -> Union['SessionElement', str, None, List[Union['SessionElement', str]]]: ... def _get_ele_path(self, mode: str) -> str: ... def make_session_ele(html_or_ele: Union[str, SessionElement, SessionPage, ChromiumElement, DriverElement, BaseElement, - ChromiumFrame, ChromiumBase], - loc: Union[str, Tuple[str, str]] = ..., - single: bool = ...) -> Union[SessionElement, str, None, List[Union[SessionElement, str]]]: ... + ChromiumFrame, ChromiumBase, DriverPage], + loc: Union[str, Tuple[str, str]] = None, + single: bool = True) -> Union[SessionElement, str, None, List[Union[SessionElement, str]]]: ... diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index 8d43ba8..87a516a 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -145,7 +145,7 @@ class SessionPage(BasePage): """ 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=None): + def s_eles(self, loc_or_str): """返回页面中符合条件的所有元素、属性或节点文本 \n :param loc_or_str: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 :return: SessionElement对象或属性、文本 @@ -318,7 +318,7 @@ def set_charset(response) -> Response: """设置Response对象的编码""" # 在headers中获取编码 content_type = response.headers.get('content-type', '').lower() - charset = search(r'charset[=: ]*(.*)?[;]', content_type) + charset = search(r'charset[=: ]*(.*)?;', content_type) if charset: response.encoding = charset.group(1) diff --git a/DrissionPage/session_page.pyi b/DrissionPage/session_page.pyi index 35e70f8..fcb3382 100644 --- a/DrissionPage/session_page.pyi +++ b/DrissionPage/session_page.pyi @@ -17,8 +17,8 @@ from .config import SessionOptions class SessionPage(BasePage): def __init__(self, - session_or_options: Union[Session, SessionOptions] = ..., - timeout: float = ...): + session_or_options: Union[Session, SessionOptions] = None, + timeout: float = 10): self._session: Session = ... self._url: str = ... self._response: Response = ... @@ -38,7 +38,7 @@ class SessionPage(BasePage): def __call__(self, loc_or_str: Union[Tuple[str, str], str, SessionElement], - timeout: float = ...) -> Union[SessionElement, str, None]: ... + timeout: float = None) -> Union[SessionElement, str, None]: ... # -----------------共有属性和方法------------------- @@ -53,10 +53,10 @@ class SessionPage(BasePage): def get(self, url: str, - show_errmsg: bool | None = ..., - retry: int | None = ..., - interval: float | None = ..., - timeout: float | None = ..., + show_errmsg: bool | None = False, + retry: int | None = None, + interval: float | None = None, + timeout: float | None = None, params: dict | None = ..., data: Union[dict, str, None] = ..., json: Union[dict, str, None] = ..., @@ -73,25 +73,25 @@ class SessionPage(BasePage): def ele(self, loc_or_ele: Union[Tuple[str, str], str, SessionElement], - timeout: float = ...) -> Union[SessionElement, str, None]: ... + timeout: float = None) -> Union[SessionElement, str, None]: ... def eles(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> List[Union[SessionElement, str]]: ... + timeout: float = None) -> List[Union[SessionElement, str]]: ... def s_ele(self, - loc_or_ele: Union[Tuple[str, str], str, SessionElement] = ...) -> Union[SessionElement, str, None]: ... + loc_or_ele: Union[Tuple[str, str], str, SessionElement] = None) -> Union[SessionElement, str, None]: ... - def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = ...) -> List[Union[SessionElement, str]]: ... + def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[Union[SessionElement, str]]: ... def _ele(self, loc_or_ele: Union[Tuple[str, str], str, SessionElement], - timeout: float = ..., - single: bool = ...) -> Union[SessionElement, str, None, List[Union[SessionElement, str]]]: ... + timeout: float = None, + single: bool = True) -> Union[SessionElement, str, None, List[Union[SessionElement, str]]]: ... def get_cookies(self, - as_dict: bool = ..., - all_domains: bool = ...) -> Union[dict, list]: ... + as_dict: bool = False, + all_domains: bool = False) -> Union[dict, list]: ... # ----------------session独有属性和方法----------------------- @property @@ -105,12 +105,12 @@ class SessionPage(BasePage): def post(self, url: str, - show_errmsg: bool | None = ..., - retry: int | None = ..., - interval: float | None = ..., + data: Union[dict, str, None] = ..., + show_errmsg: bool = False, + retry: int | None = None, + interval: float | None = None, timeout: float | None = ..., params: dict | None = ..., - data: Union[dict, str, None] = ..., json: Union[dict, str, None] = ..., headers: dict | None = ..., cookies: Any | None = ..., @@ -126,19 +126,19 @@ class SessionPage(BasePage): def _s_connect(self, url: str, mode: str, - data: Union[dict, str, None] = ..., - show_errmsg: bool = ..., - retry: int = ..., - interval: float = ..., + data: Union[dict, str, None] = None, + show_errmsg: bool = False, + retry: int = None, + interval: float = None, **kwargs) -> bool: ... def _make_response(self, url: str, - mode: str = ..., - data: Union[dict, str] = ..., - retry: int = ..., - interval: float = ..., - show_errmsg: bool = ..., + mode: str = 'get', + data: Union[dict, str] = None, + retry: int = None, + interval: float = None, + show_errmsg: bool = False, **kwargs) -> tuple: ... diff --git a/DrissionPage/shadow_root_element.pyi b/DrissionPage/shadow_root_element.pyi index cd0cf53..8647391 100644 --- a/DrissionPage/shadow_root_element.pyi +++ b/DrissionPage/shadow_root_element.pyi @@ -28,7 +28,7 @@ class ShadowRootElement(BaseElement): def __call__(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> Union[DriverElement, str, None]: ... + timeout: float = None) -> Union[DriverElement, str, None]: ... @property def tag(self) -> str: ... @@ -39,34 +39,35 @@ class ShadowRootElement(BaseElement): @property def inner_html(self) -> str: ... - def parent(self, level_or_loc: Union[str, int] = ...) -> DriverElement: ... + def parent(self, level_or_loc: Union[str, int] = 1) -> DriverElement: ... def next(self, - index: int = ..., - filter_loc: Union[tuple, str] = ...) -> Union[DriverElement, str, None]: ... + index: int = 1, + filter_loc: Union[tuple, str] = '') -> Union[DriverElement, str, None]: ... def before(self, - index: int = ..., - filter_loc: Union[tuple, str] = ...) -> Union[DriverElement, str, None]: ... + index: int = 1, + filter_loc: Union[tuple, str] = '') -> Union[DriverElement, str, None]: ... - def after(self, index: int = ..., - filter_loc: Union[tuple, str] = ...) -> Union[DriverElement, str, None]: ... + def after(self, + index: int = 1, + filter_loc: Union[tuple, str] = '') -> Union[DriverElement, str, None]: ... - def nexts(self, filter_loc: Union[tuple, str] = ...) -> List[Union[DriverElement, str]]: ... + def nexts(self, filter_loc: Union[tuple, str] = '') -> List[Union[DriverElement, str]]: ... - def befores(self, filter_loc: Union[tuple, str] = ...) -> List[Union[DriverElement, str]]: ... + def befores(self, filter_loc: Union[tuple, str] = '') -> List[Union[DriverElement, str]]: ... - def afters(self, filter_loc: Union[tuple, str] = ...) -> List[Union[DriverElement, str]]: ... + def afters(self, filter_loc: Union[tuple, str] = '') -> List[Union[DriverElement, str]]: ... def ele(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> Union[DriverElement, str, None]: ... + timeout: float = None) -> Union[DriverElement, str, None]: ... def eles(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> List[Union[DriverElement, str]]: ... + timeout: float = None) -> List[Union[DriverElement, str]]: ... - def s_ele(self, loc_or_str: Union[Tuple[str, str], str]=...) -> Union[SessionElement, str, None]: ... + def s_ele(self, loc_or_str: Union[Tuple[str, str], str] = None) -> Union[SessionElement, str, None]: ... def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[Union[SessionElement, str]]: ... diff --git a/DrissionPage/web_page.py b/DrissionPage/web_page.py index 2951f7a..e8cb2e4 100644 --- a/DrissionPage/web_page.py +++ b/DrissionPage/web_page.py @@ -187,7 +187,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): elif self._mode == 'd': return super(SessionPage, self).s_ele(loc_or_ele) - def s_eles(self, loc_or_str=None): + def s_eles(self, loc_or_str): """查找所有符合条件的元素以SessionElement形式返回,d模式处理复杂页面时效率很高 \n :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 :return: SessionElement对象或属性、文本组成的列表 @@ -315,21 +315,6 @@ class WebPage(SessionPage, ChromiumPage, BasePage): if self._has_driver: super(SessionPage, self).set_headers(headers) - def check_page(self, by_requests=False): - """d模式时检查网页是否符合预期 \n - 默认由response状态检查,可重载实现针对性检查 \n - :param by_requests: 是否用内置response检查 - :return: bool或None,None代表不知道结果 - """ - if self._session_url and self._session_url == self.url: - return self._response.ok - - # 使用requests访问url并判断可用性 - if by_requests: - self.cookies_to_session() - r = self._make_response(self.url, retry=0)[0] - return r.ok if r else False - def close_driver(self): """关闭driver及浏览器""" if self._has_driver: diff --git a/DrissionPage/web_page.pyi b/DrissionPage/web_page.pyi index cdd72f8..c7a7edd 100644 --- a/DrissionPage/web_page.pyi +++ b/DrissionPage/web_page.pyi @@ -21,11 +21,11 @@ from .chromium_driver import ChromiumDriver class WebPage(SessionPage, ChromiumPage, BasePage): def __init__(self, - mode: str = ..., - timeout: float = ..., - tab_id: str = ..., - driver_or_options: Union[ChromiumDriver, DriverOptions, bool] = ..., - session_or_options: Union[Session, SessionOptions, bool] = ...) -> None: + mode: str = 'd', + timeout: float = 10, + tab_id: str = None, + driver_or_options: Union[ChromiumDriver, DriverOptions, bool] = None, + session_or_options: Union[Session, SessionOptions, bool] = None) -> None: self._mode: str = ... self._has_driver: bool = ... self._has_session: bool = ... @@ -35,7 +35,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def __call__(self, loc_or_str: Union[Tuple[str, str], str, ChromiumElement, SessionElement], - timeout: float = ...) -> Union[ChromiumElement, SessionElement, ChromiumFrame, None]: ... + timeout: float = None) -> Union[ChromiumElement, SessionElement, ChromiumFrame, None]: ... # -----------------共有属性和方法------------------- @property @@ -76,10 +76,10 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def get(self, url: str, - show_errmsg: bool | None = ..., - retry: int | None = ..., - interval: float | None = ..., - timeout: float | None = ..., + show_errmsg: bool = False, + retry: int | None = None, + interval: float | None = None, + timeout: float | None = None, params: dict | None = ..., data: Union[dict, str, None] = ..., json: Union[dict, str, None] = ..., @@ -96,30 +96,28 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def ele(self, loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, SessionElement], - timeout: float = ...) -> Union[ChromiumElement, SessionElement, ChromiumFrame, str, None]: ... + timeout: float = None) -> Union[ChromiumElement, SessionElement, ChromiumFrame, str, None]: ... def eles(self, loc_or_str: Union[Tuple[str, str], str], - timeout: float = ...) -> List[Union[ChromiumElement, SessionElement, ChromiumFrame, str]]: ... + timeout: float = None) -> List[Union[ChromiumElement, SessionElement, ChromiumFrame, str]]: ... - def s_ele(self, loc_or_ele: Union[Tuple[str, str], str] = ...) \ + def s_ele(self, loc_or_ele: Union[Tuple[str, str], str] = None) \ -> Union[SessionElement, str, None]: ... - def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = ...) -> List[Union[SessionElement, str]]: ... + def s_eles(self, loc_or_str: Union[Tuple[str, str], str]) -> List[Union[SessionElement, str]]: ... - def change_mode(self, mode: str = ..., go: bool = ..., copy_cookies: bool = ...) -> None: ... + def change_mode(self, mode: str = None, go: bool = True, copy_cookies: bool = True) -> None: ... - def cookies_to_session(self, copy_user_agent: bool = ...) -> None: ... + def cookies_to_session(self, copy_user_agent: bool = True) -> None: ... def cookies_to_driver(self) -> None: ... - def get_cookies(self, as_dict: bool = ..., all_domains: bool = ...) -> Union[dict, list]: ... + def get_cookies(self, as_dict: bool = False, all_domains: bool = False) -> Union[dict, list]: ... - def _get_driver_cookies(self, as_dict: bool = ...)->dict: ... + def _get_driver_cookies(self, as_dict: bool = False) -> dict: ... - def set_cookies(self, cookies, set_session: bool = ..., set_driver: bool = ...) -> None: ... - - def check_page(self, by_requests: bool = ...) -> Union[bool, None]: ... + def set_cookies(self, cookies, set_session: bool = False, set_driver: bool = False) -> None: ... def close_driver(self) -> None: ... @@ -128,12 +126,12 @@ class WebPage(SessionPage, ChromiumPage, BasePage): # ----------------重写SessionPage的函数----------------------- def post(self, url: str, - show_errmsg: bool | None = ..., - retry: int | None = ..., - interval: float | None = ..., + data: Union[dict, str, None] = None, + show_errmsg: bool = False, + retry: int | None = None, + interval: float | None = None, timeout: float | None = ..., params: dict | None = ..., - data: Union[dict, str, None] = ..., json: Union[dict, str, None] = ..., headers: dict | None = ..., cookies: Any | None = ..., @@ -151,7 +149,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def _ele(self, loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, SessionElement, ChromiumFrame], - timeout: float = ..., single: bool = ..., relative: bool = ...) \ + timeout: float = None, single: bool = True, relative: bool = False) \ -> Union[ChromiumElement, SessionElement, ChromiumFrame, str, None, List[Union[SessionElement, str]], List[ Union[ChromiumElement, str, ChromiumFrame]]]: ... diff --git a/docs/WebPage使用方法/3.10动作链.md b/docs/WebPage使用方法/3.10动作链.md index 89ec92c..5eb950d 100644 --- a/docs/WebPage使用方法/3.10动作链.md +++ b/docs/WebPage使用方法/3.10动作链.md @@ -55,11 +55,11 @@ ac = ActionChains(page) **参数:** -| 名称 | 数据类型 | 说明 | -| ------------ | ----------------------------------------- | --------------------------------------- | -| `ele_or_loc` | `ChrmoiumElement`、`str`、`Tuple[int, int]` | 元素对象、文本定位符或绝对坐标,坐标为`tuple`(int, int) 形式 | -| `offset_x` | `int` | 偏移量 x | -| `offset_y` | `int` | 偏移量 y | +| 名称 | 数据类型 | 默认值 | 说明 | +| ------------ | ----------------------------------------- | --- | --------------------------------------- | +| `ele_or_loc` | `ChrmoiumElement`、`str`、`Tuple[int, int]` | 无 | 元素对象、文本定位符或绝对坐标,坐标为`tuple`(int, int) 形式 | +| `offset_x` | `int` | 0 | x 轴偏移量,向右为正,向左为负 | +| `offset_y` | `int` | 0 | y 轴偏移量,向下为正,向上为负 | **返回:**