diff --git a/DrissionPage/action_chains.pyi b/DrissionPage/action_chains.pyi new file mode 100644 index 0000000..32f1832 --- /dev/null +++ b/DrissionPage/action_chains.pyi @@ -0,0 +1,47 @@ +# -*- coding:utf-8 -*- +from time import sleep +from typing import Union, Tuple + +from .common import _location_in_viewport +from .base import DrissionElement +from .keys import _modifierBit, _keyDescriptionForString + + +class ActionChains: + """用于实现动作链的类""" + + def __init__(self, page):... + + def move_to(self, ele_or_loc: Union[DrissionElement, Tuple[int, int]], + offset_x: int = ..., offset_y: int = ...) -> 'ActionChains': ... + + def move(self, offset_x: int = ..., offset_y: int = ...) -> 'ActionChains': ... + + def hold(self, on_ele=...) -> 'ActionChains': ... + + def click(self, on_ele=...) -> 'ActionChains': ... + + def r_click(self, on_ele=...) -> 'ActionChains': ... + + def release(self, on_ele=...) -> 'ActionChains': ... + + def scroll(self, delta_x: int = ..., delta_y: int = ..., on_ele=...) -> 'ActionChains': ... + + def up(self, pixel: int) -> 'ActionChains': ... + + def down(self, pixel: int) -> 'ActionChains': ... + + def left(self, pixel: int) -> 'ActionChains': ... + + def right(self, pixel: int) -> 'ActionChains': ... + + def key_down(self, key) -> 'ActionChains': ... + + def key_up(self, key) -> 'ActionChains': ... + + def wait(self, second: float) -> 'ActionChains': ... + + def _get_key_data(self, key, action: str) -> dict: ... + + +def _location_to_client(page, lx: int, ly: int) -> tuple: ... diff --git a/DrissionPage/base.pyi b/DrissionPage/base.pyi new file mode 100644 index 0000000..6c9ffa6 --- /dev/null +++ b/DrissionPage/base.pyi @@ -0,0 +1,219 @@ +# -*- coding:utf-8 -*- +""" +@Author : g1879 +@Contact : g1879@qq.com +@File : base.py +""" +from abc import abstractmethod +from typing import Union, Tuple, List + + +class BaseParser(object): + """所有页面、元素类的基类""" + + def __call__(self, loc_or_str): ... + + def ele(self, loc_or_ele, timeout=...): ... + + def eles(self, loc_or_str: Union[Tuple[str, str], str], timeout=...): ... + + # ----------------以下属性或方法待后代实现---------------- + @property + def html(self) -> str: ... + + def s_ele(self, loc_or_ele): ... + + def s_eles(self, loc_or_str): ... + + @abstractmethod + def _ele(self, loc_or_ele, timeout=..., single=...): ... + + +class BaseElement(BaseParser): + """各元素类的基类""" + + def __init__(self): + self.page: BasePage = ... + + # ----------------以下属性或方法由后代实现---------------- + @property + def tag(self): + ... + + @property + def is_valid(self): + ... + + @abstractmethod + def _ele(self, loc_or_ele, timeout=..., single=..., relative=...): + ... + + def parent(self, level_or_loc: Union[tuple, str, int] = ...): + ... + + def prev(self, index: int = ...) -> None: + ... + + def prevs(self) -> None: + ... + + def next(self, index: int = ...): + ... + + def nexts(self): + ... + + +class DrissionElement(BaseElement): + """DriverElement 和 SessionElement的基类,但不是ShadowRootElement的基类""" + + def __init__(self): + self.page: BasePage = ... + + @property + def link(self) -> str: + ... + + @property + def css_path(self) -> str: + ... + + @property + def xpath(self) -> str: + ... + + @property + def comments(self) -> list: + ... + + def texts(self, text_node_only: bool = ...) -> list: + ... + + def parent(self, level_or_loc: Union[tuple, str, int] = ...) -> Union['DrissionElement', None]: + ... + + def prev(self, + index: int = ..., + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> Union['DrissionElement', str, None]: + ... + + def next(self, + index: int = ..., + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> Union['DrissionElement', str, None]: + ... + + def before(self, + index: int = ..., + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> Union['DrissionElement', str, None]: + ... + + def after(self, + index: int = ..., + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> Union['DrissionElement', str, None]: + ... + + def prevs(self, + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> List[Union['DrissionElement', str]]: + ... + + def nexts(self, + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> List[Union['DrissionElement', str]]: + ... + + def befores(self, + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> List[Union['DrissionElement', str]]: + ... + + def afters(self, + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> 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]]: + ... + + # ----------------以下属性或方法由后代实现---------------- + @property + def attrs(self) -> dict: + ... + + @property + def text(self) -> str: + ... + + @property + def raw_text(self) -> str: + ... + + @abstractmethod + def attr(self, attr: str) -> str: + ... + + def _get_ele_path(self, mode) -> str: + ... + + +class BasePage(BaseParser): + """页面类的基类""" + + def __init__(self, timeout: float = ...): + self._url_available: bool = ... + self.retry_times: int = ... + self.retry_interval: float = ... + self._timeout = float = ... + + @property + def title(self) -> Union[str, None]: + ... + + @property + def timeout(self) -> float: + ... + + @timeout.setter + def timeout(self, second: float) -> None: + ... + + @property + def cookies(self) -> dict: + ... + + @property + def url_available(self) -> bool: + ... + + def _before_connect(self, url: str, retry: int, interval: float) -> tuple: + ... + + # ----------------以下属性或方法由后代实现---------------- + @property + def url(self): + ... + + @property + def json(self): + ... + + @abstractmethod + def get_cookies(self, as_dict: bool = ...): + ... + + @abstractmethod + def get(self, + url: str, + show_errmsg: bool = ..., + retry: int = ..., + interval: float = ...): + ... diff --git a/DrissionPage/chromium_base.pyi b/DrissionPage/chromium_base.pyi new file mode 100644 index 0000000..8efbf5c --- /dev/null +++ b/DrissionPage/chromium_base.pyi @@ -0,0 +1,275 @@ +# -*- coding:utf-8 -*- +from typing import Union, Tuple, List, Any + +from requests import Session +from requests.cookies import RequestsCookieJar + +from .chromium_element import ChromiumElementWaiter, ChromeScroll +from .session_element import SessionElement +from .chromium_element import ChromiumElement +from .config import DriverOptions +from .base import BasePage +from .tab import Tab + + +class ChromiumBase(BasePage): + """标签页、frame、页面基类""" + + def __init__(self, + address: str, + tab_id: str = ..., + timeout: float = ...): + self._control_session: Session = ... + self.address: str = ... + self._tab_obj: Tab = ... + self._is_reading: bool = ... + self._debug: bool = ... + self.timeouts: Timeout = ... + self._first_run: bool = ... + self._is_loading: bool = ... + self._page_load_strategy: str = ... + self._scroll: ChromeScroll = ... + self._url: str = ... + + def _connect_browser(self, + addr_tab_opts: Union[str, Tab, DriverOptions] = ..., + tab_id: str = ...) -> None: ... + + def _init_page(self, tab_id: str = ...) -> None: ... + + def _get_document(self) -> None: ... + + def _wait_loading(self, timeout: float = ...) -> bool: ... + + def _onFrameStartedLoading(self, **kwargs): ... + + def _onFrameStoppedLoading(self, **kwargs): ... + + def _onLoadEventFired(self, **kwargs): ... + + def _onDocumentUpdated(self, **kwargs): ... + + def _onFrameNavigated(self, **kwargs): ... + + def _set_options(self) -> None: ... + + def __call__(self, loc_or_str: Union[Tuple[str, str], str, 'ChromiumElement'], + timeout: float = ...) -> Union['ChromiumElement', 'ChromiumFrame', None]: ... + + @property + def driver(self) -> Tab: ... + + @property + def _driver(self): ... + + @property + def _wait_driver(self) -> Tab: ... + + @property + def is_loading(self) -> bool: ... + + @property + def url(self) -> str: ... + + @property + def html(self) -> str: ... + + @property + def json(self) -> dict: ... + + @property + def tab_id(self) -> str: ... + + @property + def ready_state(self) -> str: ... + + @property + def size(self) -> dict: ... + + @property + def active_ele(self) -> ChromiumElement: ... + + @property + def page_load_strategy(self) -> str: ... + + @property + def scroll(self) -> 'ChromeScroll': ... + + def set_page_load_strategy(self, value: str) -> None: ... + + def set_timeouts(self, implicit: float = ..., page_load: float = ..., script: float = ...) -> None: ... + + def run_script(self, script: str, as_expr: bool = ..., *args: Any) -> Any: ... + + def run_async_script(self, script: str, as_expr: bool = ..., *args: Any) -> None: ... + + def get(self, + url: str, + show_errmsg: bool = ..., + retry: int = ..., + interval: float = ..., + timeout: float = ...) -> Union[None, bool]: ... + + def _get(self, + url: str, + show_errmsg: bool = ..., + retry: int = ..., + interval: float = ..., + timeout: float = ..., + frame_id: str = ...) -> Union[None, bool]: ... + + def get_cookies(self, as_dict: bool = ...) -> Union[list, dict]: ... + + def set_cookies(self, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ... + + def ele(self, + loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, 'ChromiumFrame'], + timeout: float = ...) -> Union[ChromiumElement, 'ChromiumFrame', None]: ... + + def eles(self, + loc_or_ele: Union[Tuple[str, str], str], + timeout: float = ...) -> List[Union[ChromiumElement, 'ChromiumFrame']]: ... + + def s_ele(self, loc_or_ele: Union[Tuple[str, str], str, ChromiumElement] = ...) \ + -> 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_ele: Union[Tuple[str, str], str, ChromiumElement, 'ChromiumFrame'], + timeout: float = ..., single: bool = ..., relative: bool = ...) \ + -> Union[ChromiumElement, 'ChromiumFrame', None, List[Union[ChromiumElement, 'ChromiumFrame']]]: ... + + def wait_ele(self, + loc_or_ele: Union[str, tuple, ChromiumElement], + timeout: float = ...) -> 'ChromiumElementWaiter': ... + + def scroll_to_see(self, loc_or_ele: Union[str, tuple, ChromiumElement]) -> None: ... + + def refresh(self, ignore_cache: bool = ...) -> None: ... + + def forward(self, steps: int = ...) -> None: ... + + def back(self, steps: int = ...) -> None: ... + + def _forward_or_back(self, steps: int) -> None: ... + + def stop_loading(self) -> None: ... + + def run_cdp(self, cmd: str, **cmd_args) -> dict: ... + + def set_user_agent(self, ua: str) -> None: ... + + def get_session_storage(self, item: str = None) -> 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 clear_cache(self, + session_storage: bool = True, + local_storage: bool = True, + cache: bool = True, + cookies: bool = True) -> None: ... + + def _d_connect(self, + to_url: str, + times: int = ..., + interval: float = ..., + show_errmsg: bool = ..., + timeout: float = ..., + frame_id: str = ...) -> Union[bool, None]: ... + + +class ChromiumFrame(ChromiumBase): + """实现浏览器frame的类""" + + def __init__(self, page, + ele: ChromiumElement): + self._inner_ele: ChromiumElement = ... + self.page: ChromiumBase = ... + + def __repr__(self) -> str: ... + + @property + def tag(self) -> str: ... + + @property + def html(self) -> str: ... + + @property + def inner_html(self) -> str: ... + + @property + def attrs(self) -> dict: ... + + @property + def frame_size(self) -> dict: ... + + def _set_options(self) -> None: ... + + @property + def obj_id(self) -> str: ... + + @property + def node_id(self) -> str: ... + + @property + def location(self) -> dict: ... + + @property + def is_displayed(self) -> bool: ... + + def attr(self, attr: str) -> Union[str, None]: ... + + def set_attr(self, attr: str, value: str) -> None: ... + + def remove_attr(self, attr: str) -> None: ... + + def parent(self, level_or_loc: Union[tuple, str, int] = ...) -> Union['ChromiumElement', None]: ... + + def prev(self, + index: int = ..., + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> Union['ChromiumElement', str, None]: ... + + def next(self, + index: int = ..., + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> Union['ChromiumElement', str, None]: ... + + def before(self, + index: int = ..., + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> Union['ChromiumElement', str, None]: ... + + def after(self, + index: int = ..., + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> Union['ChromiumElement', str, None]: ... + + def prevs(self, + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> List[Union['ChromiumElement', str]]: ... + + def nexts(self, + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> List[Union['ChromiumElement', str]]: ... + + def befores(self, + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> List[Union['ChromiumElement', str]]: ... + + +class Timeout(object): + """用于保存d模式timeout信息的类""" + + def __init__(self, page: ChromiumBase): + self.page: ChromiumBase = ... + self.page_load: float = ... + self.script: float = ... + + @property + def implicit(self) -> float: ... diff --git a/DrissionPage/chromium_element.pyi b/DrissionPage/chromium_element.pyi new file mode 100644 index 0000000..d7be738 --- /dev/null +++ b/DrissionPage/chromium_element.pyi @@ -0,0 +1,457 @@ +# -*- coding:utf-8 -*- +""" +@Author : g1879 +@Contact : g1879@qq.com +@File : chromium_element.py +""" +from pathlib import Path +from typing import Union, Tuple, List, Any + +from .chromium_page import ChromiumPage +from .web_page import WebPage +from .base import DrissionElement, BaseElement +from .session_element import SessionElement + + +class ChromiumElement(DrissionElement): + """ChromePage页面对象中的元素对象""" + + def __init__(self, page, node_id: str = None, obj_id: str = None): + self._tag :str=... + self.page: Union[ChromiumPage, WebPage]=... + self._node_id:str=... + self._obj_id:str=... + self._scroll:ChromeScroll=... + self._select:ChromeSelect=... + + def __repr__(self) -> str: ... + + def __call__(self, + loc_or_str: Union[Tuple[str, str], str], + timeout: float = ...) -> Union['ChromiumElement', str, None]: ... + + @property + def tag(self) -> str: ... + + @property + def html(self) -> str: ... + + @property + def inner_html(self) -> str: ... + + @property + def attrs(self) -> dict: ... + + @property + def text(self) -> str: ... + + @property + def raw_text(self) -> str: ... + + # -----------------d模式独有属性------------------- + @property + def obj_id(self) -> str: ... + + @property + def node_id(self) -> str: ... + + @property + def size(self) -> dict: ... + + @property + def client_location(self) -> Union[dict, None]: ... + + @property + def client_midpoint(self) -> Union[dict, None]: ... + + @property + def location(self) -> Union[dict, None]: ... + + @property + def midpoint(self) -> dict: ... + + @property + def _client_click_point(self) -> Union[dict, None]: ... + + @property + def _click_point(self) -> Union[dict, None]: ... + + @property + def shadow_root(self) -> Union[None, 'ChromiumShadowRootElement']: ... + + @property + def sr(self): ... + + @property + def pseudo_before(self) -> str: ... + + @property + def pseudo_after(self) -> str: ... + + @property + def scroll(self) -> 'ChromeScroll': ... + + def parent(self, level_or_loc: Union[tuple, str, int] = ...) -> Union['ChromiumElement', None]: ... + + def prev(self, + index: int = ..., + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> Union['ChromiumElement', str, None]: ... + + def next(self, + index: int = ..., + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> Union['ChromiumElement', str, None]: ... + + def before(self, + index: int = ..., + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> Union['ChromiumElement', str, None]: ... + + def after(self, + index: int = ..., + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> Union['ChromiumElement', str, None]: ... + + def prevs(self, + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> List[Union['ChromiumElement', str]]: ... + + def nexts(self, + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> List[Union['ChromiumElement', str]]: ... + + def befores(self, + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> List[Union['ChromiumElement', str]]: ... + + def wait_ele(self, + loc_or_ele: Union[str, tuple, 'ChromiumElement'], + timeout: float = ...) -> 'ChromiumElementWaiter': ... + + @property + def select(self) -> 'ChromeSelect': ... + + @property + def is_selected(self) -> bool: ... + + @property + def is_displayed(self) -> bool: ... + + @property + def is_enabled(self) -> bool: ... + + @property + def is_alive(self) -> bool: ... + + @property + def is_in_viewport(self) -> bool: ... + + def attr(self, attr: str) -> Union[str, None]: ... + + def set_attr(self, attr: str, value: str) -> None: ... + + def remove_attr(self, attr: str) -> None: ... + + def prop(self, prop: str) -> Union[str, int, None]: ... + + def set_prop(self, prop: str, value: str) -> None: ... + + def set_innerHTML(self, html: str) -> None: ... + + def run_script(self, script: str, as_expr: bool = ..., *args: Any) -> Any: ... + + def run_async_script(self, script: str, as_expr: bool = ..., *args: Any) -> None: ... + + def ele(self, + loc_or_str: Union[Tuple[str, str], str], + timeout: float = ...) -> Union['ChromiumElement', str, None]: ... + + def eles(self, + loc_or_str: Union[Tuple[str, str], str], + timeout: float = ...) -> List[Union['ChromiumElement', str]]: ... + + def s_ele(self, loc_or_str: Union[Tuple[str, str], str] = ...) -> 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=...) \ + -> Union['ChromiumElement', str, None, + List[Union['ChromiumElement', 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 get_screenshot(self, path: [str, Path] = ..., as_bytes: [bool, str] = ...) -> Union[str, bytes]: ... + + def input(self, vals: Union[str, tuple, list], clear: bool = ...) -> None: ... + + def _set_file_input(self, files: Union[str, list, tuple]) -> None: ... + + def clear(self, by_js: bool = ...) -> None: ... + + def click(self, by_js: bool = ..., retry: bool = ..., timeout: float = ...) -> bool: ... + + def click_at(self, offset_x: Union[int, str] = ..., offset_y: Union[int, str] = ..., button: str = ...) -> None: ... + + def r_click(self) -> None: ... + + def r_click_at(self, offset_x: Union[int, str] = ..., offset_y: Union[int, str] = ...) -> None: ... + + def _click(self, client_x: int, client_y: int, button: str = ...) -> None: ... + + def hover(self, offset_x: int = ..., offset_y: int = ...) -> None: ... + + def drag(self, offset_x: int = ..., offset_y: int = ..., speed: int = ..., shake: bool = ...) -> None: ... + + def drag_to(self, + ele_or_loc: Union[tuple, 'ChromiumElement'], + speed: int = ..., + shake: bool = ...) -> None: ... + + def _get_obj_id(self, node_id) -> str: ... + + def _get_node_id(self, obj_id) -> str: ... + + def _get_ele_path(self, mode) -> str: ... + + def _get_client_rect(self, quad: str) -> Union[dict, None]: ... + + def _get_absolute_rect(self, x, y) -> dict: ... + + +class ChromiumShadowRootElement(BaseElement): + """ChromiumShadowRootElement是用于处理ShadowRoot的类,使用方法和ChromiumElement基本一致""" + + def __init__(self, + parent_ele: ChromiumElement, + obj_id: str = ..., + backend_id: str = ...): + self._obj_id:str=... + self._node_id:str=... + self.page:ChromiumPage=... + self.parent_ele:ChromiumElement=... + + + def __repr__(self) -> str: ... + + def __call__(self, + loc_or_str: Union[Tuple[str, str], str], + timeout: float = ...) -> Union[ChromiumElement, None]: ... + + @property + def is_enabled(self) -> bool: ... + + @property + def is_alive(self) -> bool: ... + + @property + def node_id(self) -> str: ... + + @property + def obj_id(self) -> str: ... + + @property + def tag(self) -> str: ... + + @property + def html(self) -> str: ... + + @property + def inner_html(self) -> str: ... + + def run_script(self, script: str, as_expr: bool = ..., *args: Any) -> Any: ... + + def run_async_script(self, script: str, as_expr: bool = ..., *args: Any) -> None: ... + + def parent(self, level_or_loc: Union[str, int] = ...) -> ChromiumElement: ... + + def next(self, + index: int = ..., + filter_loc: Union[tuple, str] = ...) -> Union[ChromiumElement, str, None]: ... + + def before(self, + index: int = ..., + filter_loc: Union[tuple, str] = ...) -> Union[ChromiumElement, str, None]: ... + + def after(self, index: int = ..., + filter_loc: Union[tuple, str] = ...) -> Union[ChromiumElement, str, None]: ... + + 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 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, None]: ... + + def eles(self, + loc_or_str: Union[Tuple[str, str], str], + timeout: float = ...) -> List[ChromiumElement]: ... + + def s_ele(self, loc_or_ele=...) -> Union[SessionElement, str, None]: ... + + def s_eles(self, loc_or_ele) -> List[SessionElement]: ... + + def _ele(self, + loc_or_str: Union[Tuple[str, str], str], + timeout: float = ..., + single: bool = ..., relative=...) -> Union['ChromiumElement', None, List[ChromiumElement]]: ... + + def _get_node_id(self, obj_id) -> str: ... + + def _get_obj_id(self, back_id) -> str: ... + + def _get_backend_id(self, node_id) -> str: ... + + +def make_chromium_ele(ele: ChromiumElement, + loc: Union[str, Tuple[str, str]], + single: bool = ..., + timeout: float = ..., + relative=...) -> Union[ChromiumElement, str, None, List[Union[ChromiumElement, str]]]: ... + + +def _find_by_xpath(ele: ChromiumElement, + xpath: str, + single: bool, + timeout: float, + relative=...) -> Union[ChromiumElement, List[ChromiumElement], None]: ... + + +def _find_by_css(ele: ChromiumElement, + selector: str, + single: bool, + timeout: float) -> Union[ChromiumElement, List[ChromiumElement], None]: ... + + +def _make_chromium_ele(page, node_id: str = ..., obj_id: str = ...): ... + + +def _make_js_for_find_ele_by_xpath(xpath: str, type_txt: str, node_txt: str) -> str: ... + + +def _run_script(page_or_ele, script: str, as_expr: bool = ..., timeout: float = ..., args: tuple = ...) -> Any: ... + + +def _parse_js_result(page, ele, result: dict): ... + + +def _convert_argument(arg: Any) -> dict: ... + + +def _send_enter(ele: ChromiumElement) -> None: ... + + +def _send_key(ele: ChromiumElement, modifier: int, key: str) -> None: ... + + +def _offset_scroll(ele: ChromiumElement, offset_x: int, offset_y: int) -> tuple: ... + + +class ChromeScroll(object): + """用于滚动的对象""" + + def __init__(self, page_or_ele): + self.t1 :str=... + self.t2 :str=... + self.obj_id:str=... + self.page:ChromiumPage=... + + def _run_script(self, js: str): ... + + def to_top(self) -> None: ... + + def to_bottom(self) -> None: ... + + def to_half(self) -> None: ... + + def to_rightmost(self) -> None: ... + + def to_leftmost(self) -> None: ... + + def to_location(self, x: int, y: int) -> None: ... + + def up(self, pixel: int = ...) -> None: ... + + def down(self, pixel: int = ...) -> None: ... + + def left(self, pixel: int = ...) -> None: ... + + def right(self, pixel: int = ...) -> None: ... + + +class ChromeSelect(object): + """ChromeSelect 类专门用于处理 d 模式下 select 标签""" + + def __init__(self, ele: ChromiumElement): + self._ele:ChromiumElement=... + + + def __call__(self, text_or_index: Union[str, int, list, tuple], timeout=None) -> bool: ... + + @property + def is_multi(self) -> bool: ... + + @property + def options(self) -> List[ChromiumElement]: ... + + @property + def selected_option(self) -> Union[ChromiumElement, None]: ... + + @property + def selected_options(self) -> List[ChromiumElement]: ... + + def clear(self) -> None: ... + + def by_text(self, text: Union[str, list, tuple], timeout=...) -> bool: ... + + def by_value(self, value: Union[str, list, tuple], timeout=...) -> bool: ... + + def by_index(self, index: Union[int, list, tuple], timeout=...) -> bool: ... + + def cancel_by_text(self, text: Union[str, list, tuple], timeout=...) -> bool: ... + + def cancel_by_value(self, value: Union[str, list, tuple], timeout=...) -> bool: ... + + def cancel_by_index(self, index: Union[int, list, tuple], timeout=...) -> bool: ... + + def invert(self) -> None: ... + + def _select(self, + text_value_index: Union[str, int, list, tuple] = ..., + para_type: str = ..., + deselect: bool = ..., + timeout=...) -> bool: ... + + def _select_multi(self, + text_value_index: Union[list, tuple] = ..., + para_type: str = ..., + deselect: bool = ...) -> bool: ... + + +class ChromiumElementWaiter(object): + """等待元素在dom中某种状态,如删除、显示、隐藏""" + + def __init__(self, + page_or_ele, + loc_or_ele: Union[str, tuple, ChromiumElement], + timeout: float = ...): + self.loc_or_ele: Union[str, tuple, ChromiumElement]=... + self.timeout:float=... + self.driver:Union[ChromiumPage, ChromiumPage]=... + + def delete(self) -> bool: ... + + def display(self) -> bool: ... + + def hidden(self) -> bool: ... + + def _wait_ele(self, mode: str) -> Union[None, bool]: ... diff --git a/DrissionPage/chromium_page.pyi b/DrissionPage/chromium_page.pyi new file mode 100644 index 0000000..49b2a4e --- /dev/null +++ b/DrissionPage/chromium_page.pyi @@ -0,0 +1,120 @@ +# -*- coding:utf-8 -*- +from pathlib import Path +from typing import Union, Tuple, List + +from .chromium_base import ChromiumBase +from .chromium_tab import ChromiumTab +from .config import DriverOptions +from .tab import Tab + + +class ChromiumPage(ChromiumBase): + """用于管理浏览器的类""" + + def __init__(self, + addr_tab_opts: Union[str, Tab, DriverOptions] = ..., + tab_id: str = ..., + timeout: float = ...): + self.options: DriverOptions = ... + self._window_setter: WindowSizeSetter = ... + self.main_tab: str = ... + self._alert: Alert = ... + + def _connect_browser(self, + addr_tab_opts: Union[str, Tab, DriverOptions] = ..., + tab_id: str = ...) -> None: ... + + def _init_page(self, tab_id: str = ...) -> None: ... + + def _set_options(self) -> None: ... + + @property + def tabs_count(self) -> int: ... + + @property + def tabs(self) -> list: ... + + @property + def process_id(self) -> Union[None, int]: ... + + @property + def set_window(self) -> 'WindowSizeSetter': ... + + def get_tab(self, tab_id: str = ...) -> 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 to_front(self) -> None: ... + + def new_tab(self, url: str = ..., switch_to: bool = ...) -> None: ... + + def to_main_tab(self) -> None: ... + + def to_tab(self, tab_id: str = ..., activate: bool = ...) -> None: ... + + def _to_tab(self, tab_id: str = ..., activate: bool = ..., read_doc: bool = ...) -> None: ... + + def close_tabs(self, tab_ids: Union[str, List[str], Tuple[str]] = ..., others: bool = ...) -> None: ... + + def close_other_tabs(self, tab_ids: Union[str, List[str], Tuple[str]] = ...) -> None: ... + + def handle_alert(self, accept: bool = ..., send: str = ..., timeout: float = ...) -> Union[str, None]: ... + + def hide_browser(self) -> None: ... + + def show_browser(self) -> None: ... + + def quit(self) -> None: ... + + def _on_alert_close(self, **kwargs): ... + + def _on_alert_open(self, **kwargs): ... + + +class Alert(object): + """用于保存alert信息的类""" + + def __init__(self): + self.activated: bool = ... + self.text: str = ... + self.type: str = ... + self.defaultPrompt: str = ... + self.response_accept: str = ... + self.response_text: str = ... + + +class WindowSizeSetter(object): + """用于设置窗口大小的类""" + + def __init__(self, page: ChromiumPage): + self.driver: Tab = ... + self.window_id: str = ... + + def maximized(self) -> None: ... + + def minimized(self) -> None: ... + + def fullscreen(self) -> None: ... + + def normal(self) -> None: ... + + def new_size(self, width: int = ..., height: int = ...) -> None: ... + + def to_location(self, x: int = ..., y: int = ...) -> None: ... + + def _get_info(self) -> dict: ... + + def _perform(self, bounds: dict) -> None: ... + + +def _show_or_hide_browser(page: ChromiumPage, hide: bool = ...) -> None: ... + + +def _get_browser_progress_id(progress, address: str) -> Union[str, None]: ... + + +def _get_chrome_hwnds_from_pid(pid, title) -> list: ... diff --git a/DrissionPage/chromium_tab.py b/DrissionPage/chromium_tab.py index 64462c4..b96ce53 100644 --- a/DrissionPage/chromium_tab.py +++ b/DrissionPage/chromium_tab.py @@ -5,10 +5,11 @@ from .chromium_base import ChromiumBase class ChromiumTab(ChromiumBase): """实现浏览器标签页的类""" - def __init__(self, page, - tab_id: str = None): + def __init__(self, + page, + tab_id=None): """初始化 \n - :param page: 浏览器地址:端口、Tab对象或DriverOptions对象 + :param page: ChromiumPage对象 :param tab_id: 要控制的标签页id,不指定默认为激活的 """ self.page = page @@ -19,16 +20,3 @@ class ChromiumTab(ChromiumBase): script=self.page.timeouts.script, implicit=self.page.timeouts.implicit if self.timeout is None else self.timeout) self._page_load_strategy = self.page.page_load_strategy - - -class Timeout(object): - """用于保存d模式timeout信息的类""" - - def __init__(self, page): - self.page = page - self.page_load = 30 - self.script = 30 - - @property - def implicit(self): - return self.page.timeout diff --git a/DrissionPage/chromium_tab.pyi b/DrissionPage/chromium_tab.pyi new file mode 100644 index 0000000..5b89452 --- /dev/null +++ b/DrissionPage/chromium_tab.pyi @@ -0,0 +1,15 @@ +# -*- coding:utf-8 -*- +from typing import Union + +from .chromium_page import ChromiumPage +from .tab import Tab +from .chromium_base import ChromiumBase + + +class ChromiumTab(ChromiumBase): + """实现浏览器标签页的类""" + + def __init__(self, page:ChromiumPage, tab_id: str = ...): + self.page: ChromiumPage = ... + + def _set_options(self) -> None: ... diff --git a/DrissionPage/session_element.pyi b/DrissionPage/session_element.pyi new file mode 100644 index 0000000..f1b02a3 --- /dev/null +++ b/DrissionPage/session_element.pyi @@ -0,0 +1,124 @@ +# -*- coding:utf-8 -*- +""" +@Author : g1879 +@Contact : g1879@qq.com +@File : session_element.py +""" +from typing import Union, List, Tuple + +from lxml.html import HtmlElement + +from .base import DrissionElement, BasePage, BaseElement +from .session_page import SessionPage + + +class SessionElement(DrissionElement): + """session模式的元素对象,包装了一个lxml的Element对象,并封装了常用功能""" + + def __init__(self, ele: HtmlElement, page=...): + self._inner_ele: HtmlElement = ... + self.page: SessionPage = ... + + @property + def inner_ele(self) -> HtmlElement: ... + + def __repr__(self) -> str: ... + + def __call__(self, + loc_or_str: Union[Tuple[str, str], str], + timeout=...) -> Union['SessionElement', str, None]: ... + + @property + def tag(self) -> str: ... + + @property + def html(self) -> str: ... + + @property + def inner_html(self) -> str: ... + + @property + def attrs(self) -> dict: ... + + @property + def text(self) -> str: ... + + @property + def raw_text(self) -> str: ... + + def parent(self, level_or_loc: Union[tuple, str, int] = ...) -> Union['SessionElement', None]: ... + + def prev(self, + index: int = ..., + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> Union['SessionElement', str, None]: ... + + def next(self, + index: int = ..., + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> Union['SessionElement', str, None]: ... + + def before(self, + index: int = ..., + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> Union['SessionElement', str, None]: ... + + def after(self, + index: int = ..., + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> Union['SessionElement', str, None]: ... + + def prevs(self, + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> List[Union['SessionElement', str]]: ... + + def nexts(self, + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> List[Union['SessionElement', str]]: ... + + def befores(self, + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> List[Union['SessionElement', str]]: + ... + + def afters(self, + filter_loc: Union[tuple, str] = ..., + timeout: float = ...) -> 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]: + ... + + def eles(self, + loc_or_str: Union[Tuple[str, str], str], + timeout: float = ...) -> List[Union['SessionElement', str]]: + ... + + def s_ele(self, + loc_or_str: Union[Tuple[str, str], str] = ...) -> 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 = ...) -> 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, BaseElement, BasePage], + loc: Union[str, Tuple[str, str]] = ..., + single: bool = ...) -> Union[SessionElement, str, None, List[Union[SessionElement, str]]]: + ... diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index d3779eb..d67b035 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -36,7 +36,7 @@ class SessionPage(BasePage): elif isinstance(Session_or_Options, Session): self._session = Session_or_Options - def _set_session(self, data: dict) -> None: + def _set_session(self, data) -> None: """根据传入字典对session进行设置 \n :param data: session配置字典 :return: None diff --git a/DrissionPage/session_page.pyi b/DrissionPage/session_page.pyi index b1b2fb7..f48d81d 100644 --- a/DrissionPage/session_page.pyi +++ b/DrissionPage/session_page.pyi @@ -1,11 +1,61 @@ -from typing import Any, Union +from typing import Any, Union, Tuple, List -from requests import Session +from DownloadKit import DownloadKit +from requests import Session, Response +from requests.cookies import RequestsCookieJar +from .session_element import SessionElement from .config import SessionOptions class SessionPage: + def __init__(self, + session_or_options: Union[Session, SessionOptions] = ..., + timeout: float = ...): + self._session: Session = ... + self._url: str = ... + self._response: Response = ... + self._download_kit: DownloadKit = ... + self._url_available: bool = ... + self.timeout: float = ... + self.retry_times: int = ... + self.retry_interval: float = ... + + def _create_session(self, + Session_or_Options: Union[Session, SessionOptions]) -> None: + ... + + def _set_session(self, data: dict) -> None: + ... + + def set_cookies(self, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: + ... + + def __call__(self, + loc_or_str: Union[Tuple[str, str], str, SessionElement], + timeout=...) -> Union[SessionElement, str, None]: + """在内部查找元素 \n + 例:ele2 = ele1('@id=ele_id') \n + :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 + :param timeout: 不起实际作用,用于和DriverElement对应,便于无差别调用 + :return: SessionElement对象或属性文本 + """ + return self.ele(loc_or_str) + + # -----------------共有属性和方法------------------- + + @property + def url(self) -> str: + ... + + @property + def html(self) -> str: + ... + + @property + def json(self) -> dict: + ... + def get(self, url: str, show_errmsg: bool | None = ..., @@ -25,12 +75,79 @@ class SessionPage: verify: Any | None = ..., cert: Any | None = ..., json: Union[dict, str, None] = ..., - ) -> bool: ... + ) -> bool: + ... + + def ele(self, + loc_or_ele: Union[Tuple[str, str], str, SessionElement], + timeout: float = ...) -> Union[SessionElement, str, None]: + ... + + def eles(self, + loc_or_str: Union[Tuple[str, str], str], + timeout: float = ...) -> List[Union[SessionElement, str]]: + ... + + def s_ele(self, + loc_or_ele: Union[Tuple[str, str], str, SessionElement] = ...) -> Union[SessionElement, str, None]: + ... + + def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = ...) -> List[Union[SessionElement, str]]: + """返回页面中符合条件的所有元素、属性或节点文本 \n + :param loc_or_str: 元素的定位信息,可以是元素对象,loc元组,或查询字符串 + :return: SessionElement对象或属性、文本 + """ + return self._ele(loc_or_str, single=False) + + def _ele(self, + loc_or_ele: Union[Tuple[str, str], str, SessionElement], + timeout: float = ..., + single: bool = ...) -> Union[SessionElement, str, None, List[Union[SessionElement, str]]]: + ... + + def get_cookies(self, + as_dict: bool = ..., + all_domains: bool = ...) -> Union[dict, list]: + ... + + # ----------------session独有属性和方法----------------------- + @property + def session(self) -> Session: + ... @property - def url(self) -> str: ... + def response(self) -> Response: + ... @property - def html(self) -> str: ... + def download(self) -> DownloadKit: + ... - def _set_session(self, Session_or_Options: Union[Session, SessionOptions]) -> None: ... + def post(self, + url: str, + data: Union[dict, str] = ..., + show_errmsg: bool = ..., + retry: int = ..., + interval: float = ..., + **kwargs) -> bool: + ... + + def _s_connect(self, + url: str, + mode: str, + data: Union[dict, str, None] = ..., + show_errmsg: bool = ..., + retry: int = ..., + interval: float = ..., + **kwargs) -> bool: + ... + + def _make_response(self, + url: str, + mode: str = ..., + data: Union[dict, str] = ..., + retry: int = ..., + interval: float = ..., + show_errmsg: bool = ..., + **kwargs) -> tuple: + ... diff --git a/DrissionPage/web_page.pyi b/DrissionPage/web_page.pyi new file mode 100644 index 0000000..5f786fe --- /dev/null +++ b/DrissionPage/web_page.pyi @@ -0,0 +1,131 @@ +# -*- coding:utf-8 -*- +from time import sleep +from typing import Union, Tuple, List + +from DownloadKit import DownloadKit +from requests import Session, Response +from tldextract import extract + +from .chromium_base import ChromiumBase, ChromiumFrame +from .base import BasePage +from .chromium_element import ChromiumElement # , ChromiumBase +from .chromium_page import ChromiumPage +from .config import DriverOptions, SessionOptions, _cookies_to_tuple +from .session_element import SessionElement +from .session_page import SessionPage +from .tab import Tab + + +class WebPage(SessionPage, ChromiumPage, BasePage): + """整合浏览器和request的页面类""" + + def __init__(self, + mode: str = ..., + timeout: float = ..., + tab_id: str = ..., + driver_or_options: Union[Tab, DriverOptions, bool] = ..., + session_or_options: Union[Session, SessionOptions, bool] = ...) -> None: ... + + def __call__(self, + loc_or_str: Union[Tuple[str, str], str, ChromiumElement, SessionElement], + timeout: float = ...) -> Union[ChromiumElement, SessionElement, ChromiumFrame, None]: ... + + # -----------------共有属性和方法------------------- + @property + def url(self) -> Union[str, None]: ... + + @property + def html(self) -> str: ... + + @property + def json(self) -> dict: ... + + @property + def response(self) -> Response: ... + + @property + def mode(self) -> str: ... + + @property + def cookies(self): ... + + @property + def session(self) -> Session: ... + + @property + def driver(self) -> Tab: ... + + @property + def _wait_driver(self) -> Tab: ... + + @property + def _driver(self) -> Tab: ... + + @_driver.setter + def _driver(self, tab): ... + + @property + def _session_url(self) -> str: ... + + def get(self, + url: str, + show_errmsg: bool = ..., + retry: int = ..., + interval: float = ..., + timeout: float = ..., + **kwargs) -> Union[bool, None]: ... + + def ele(self, + loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, SessionElement], + timeout: float = ...) -> 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]]: ... + + def s_ele(self, loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, SessionElement] = ...) \ + -> Union[SessionElement, str, None]: ... + + 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 cookies_to_session(self, copy_user_agent: bool = ...) -> None: ... + + def cookies_to_driver(self) -> None: ... + + def get_cookies(self, as_dict: bool = ..., all_domains: bool = ...) -> Union[dict, list]: ... + + def _get_driver_cookies(self, as_dict: bool = ...): ... + + def set_cookies(self, cookies, set_session: bool = ..., set_driver: bool = ...): ... + + def check_page(self, by_requests: bool = ...) -> Union[bool, None]: ... + + def close_driver(self) -> None: ... + + def close_session(self) -> None: ... + + # ----------------重写SessionPage的函数----------------------- + def post(self, + url: str, + data: Union[dict, str] = ..., + show_errmsg: bool = ..., + retry: int = ..., + interval: float = ..., + **kwargs) -> bool: ... + + @property + def download(self) -> DownloadKit: ... + + def _ele(self, + loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, SessionElement], + timeout: float = ..., single: bool = ..., relative: bool = ...) \ + -> Union[ChromiumElement, SessionElement, ChromiumFrame, str, None, List[Union[SessionElement, str]], List[ + Union[ChromiumElement, str, ChromiumFrame]]]: ... + + def _set_driver_options(self, Tab_or_Options): ... + + def _set_session_options(self, Session_or_Options): ... + + def quit(self) -> None: ...