diff --git a/DrissionPage/action_chains.py b/DrissionPage/action_chains.py index f914b04..1d74668 100644 --- a/DrissionPage/action_chains.py +++ b/DrissionPage/action_chains.py @@ -1,9 +1,7 @@ # -*- 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 @@ -12,7 +10,7 @@ class ActionChains: def __init__(self, page): """初始化 \n - :param page: ChromePage对象 + :param page: ChromiumPage对象 """ self.page = page self._dr = page.driver @@ -20,7 +18,7 @@ class ActionChains: self.curr_x = 0 # 视口坐标 self.curr_y = 0 - def move_to(self, ele_or_loc: Union[DrissionElement, Tuple[int, int]], + def move_to(self, ele_or_loc, offset_x: int = 0, offset_y: int = 0) -> 'ActionChains': """鼠标移动到元素中点,或页面上的某个绝对坐标。可设置偏移量 \n 当带偏移量时,偏移量相对于元素左上角坐标 @@ -32,7 +30,7 @@ class ActionChains: if isinstance(ele_or_loc, (tuple, list)): lx = ele_or_loc[0] + offset_x ly = ele_or_loc[1] + offset_y - elif isinstance(ele_or_loc, DrissionElement): + elif 'ChromiumElement' in str(type(ele_or_loc)): ele_loc = ele_or_loc.location if offset_x or offset_y else ele_or_loc.midpoint lx = ele_loc['x'] + offset_x ly = ele_loc['y'] + offset_y diff --git a/DrissionPage/action_chains.pyi b/DrissionPage/action_chains.pyi index 32f1832..a4417f0 100644 --- a/DrissionPage/action_chains.pyi +++ b/DrissionPage/action_chains.pyi @@ -1,18 +1,22 @@ # -*- 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 +from .tab import Tab +from .chromium_element import ChromiumElement +from .chromium_page import ChromiumPage class ActionChains: """用于实现动作链的类""" - def __init__(self, page):... + def __init__(self, page): + self.page: ChromiumPage = ... + self._dr: Tab = ... + self.curr_x: int = ... + self.curr_y: int = ... + self.modifier: int = ... - def move_to(self, ele_or_loc: Union[DrissionElement, Tuple[int, int]], + def move_to(self, ele_or_loc: Union[ChromiumElement, Tuple[int, int]], offset_x: int = ..., offset_y: int = ...) -> 'ActionChains': ... def move(self, offset_x: int = ..., offset_y: int = ...) -> 'ActionChains': ... diff --git a/DrissionPage/base.py b/DrissionPage/base.py index acb0322..e4c1911 100644 --- a/DrissionPage/base.py +++ b/DrissionPage/base.py @@ -76,7 +76,8 @@ class BaseElement(BaseParser): class DrissionElement(BaseElement): - """DriverElement 和 SessionElement的基类,但不是ShadowRootElement的基类""" + """DriverElement、ChromiumElement 和 SessionElement的基类 + 但不是ShadowRootElement的基类""" @property def link(self) -> str: diff --git a/DrissionPage/base.pyi b/DrissionPage/base.pyi index 6c9ffa6..7621e46 100644 --- a/DrissionPage/base.pyi +++ b/DrissionPage/base.pyi @@ -1,12 +1,12 @@ # -*- coding:utf-8 -*- -""" -@Author : g1879 -@Contact : g1879@qq.com -@File : base.py -""" from abc import abstractmethod from typing import Union, Tuple, List +from selenium.webdriver.remote.webelement import WebElement + +from .driver_page import DriverPage +from .mix_page import MixPage + class BaseParser(object): """所有页面、元素类的基类""" @@ -32,7 +32,7 @@ class BaseParser(object): class BaseElement(BaseParser): """各元素类的基类""" - def __init__(self): + def __init__(self, page: BasePage): self.page: BasePage = ... # ----------------以下属性或方法由后代实现---------------- @@ -67,7 +67,8 @@ class BaseElement(BaseParser): class DrissionElement(BaseElement): """DriverElement 和 SessionElement的基类,但不是ShadowRootElement的基类""" - def __init__(self): + def __init__(self, + page: BasePage = ...): self.page: BasePage = ... @property diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index 3544771..c922f51 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -8,12 +8,11 @@ from urllib.parse import urlparse from requests import Session from requests.cookies import RequestsCookieJar -from .chromium_element import ChromiumElementWaiter, ChromeScroll -from .common import get_loc -from .session_element import SessionElement, make_session_ele -from .chromium_element import ChromiumElement, _run_script -from .config import DriverOptions, _cookies_to_tuple from .base import BasePage +from .chromium_element import ChromiumElementWaiter, ChromeScroll, ChromiumElement, _run_script +from .common import get_loc +from .config import DriverOptions, _cookies_to_tuple +from .session_element import SessionElement, make_session_ele from .tab import Tab diff --git a/DrissionPage/chromium_element.pyi b/DrissionPage/chromium_element.pyi index d7be738..e61a36c 100644 --- a/DrissionPage/chromium_element.pyi +++ b/DrissionPage/chromium_element.pyi @@ -7,6 +7,7 @@ from pathlib import Path from typing import Union, Tuple, List, Any +from .chromium_base import ChromiumBase from .chromium_page import ChromiumPage from .web_page import WebPage from .base import DrissionElement, BaseElement @@ -16,13 +17,16 @@ 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 __init__(self, + page: ChromiumBase, + 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: ... diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index 4d93ca8..0a40b80 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -10,7 +10,7 @@ from requests import Session from .chromium_base import Timeout, ChromiumBase from .chromium_tab import ChromiumTab from .config import DriverOptions -from .drission import connect_chrome +from .common import connect_chrome from .tab import Tab diff --git a/DrissionPage/chromium_tab.pyi b/DrissionPage/chromium_tab.pyi index 5b89452..4a7f346 100644 --- a/DrissionPage/chromium_tab.pyi +++ b/DrissionPage/chromium_tab.pyi @@ -1,9 +1,7 @@ # -*- coding:utf-8 -*- -from typing import Union -from .chromium_page import ChromiumPage -from .tab import Tab from .chromium_base import ChromiumBase +from .chromium_page import ChromiumPage class ChromiumTab(ChromiumBase): diff --git a/DrissionPage/drission.pyi b/DrissionPage/drission.pyi new file mode 100644 index 0000000..2c14468 --- /dev/null +++ b/DrissionPage/drission.pyi @@ -0,0 +1,92 @@ +# -*- encoding: utf-8 -*- +from subprocess import Popen +from typing import Union + +from requests import Session +from requests.cookies import RequestsCookieJar +from selenium.webdriver.chrome.options import Options +from selenium.webdriver.chrome.webdriver import WebDriver +from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver + +from .config import SessionOptions, DriverOptions + + +class Drission(object): + """Drission类用于管理WebDriver对象和Session对象,是驱动器的角色""" + + def __init__(self, + driver_or_options: Union[RemoteWebDriver, Options, DriverOptions, bool] = ..., + session_or_options: Union[Session, dict, SessionOptions, bool] = ..., + ini_path: str = ..., + proxy: dict = ...): + self._session: Session = ... + self._session_options: dict = ... + self._proxy: dict = ... + self._driver: WebDriver = ... + self._debugger: Popen = ... + self._driver_options: DriverOptions = ... + + def __del__(self): ... + + @property + def session(self) -> Session: ... + + @property + def driver(self) -> WebDriver: ... + + @property + def driver_options(self) -> Union[DriverOptions, Options]: ... + + @property + def session_options(self) -> dict: ... + + @session_options.setter + def session_options(self, options: Union[dict, SessionOptions]) -> None: ... + + @property + def proxy(self) -> Union[None, dict]: ... + + @proxy.setter + def proxy(self, proxies: dict = ...) -> None: ... + + @property + def debugger_progress(self): ... + + def kill_browser(self) -> None: ... + + def get_browser_progress_id(self) -> Union[str, None]: ... + + def hide_browser(self) -> None: ... + + def show_browser(self) -> None: ... + + def _show_or_hide_browser(self, hide: bool = ...) -> None: ... + + def set_cookies(self, + cookies: Union[RequestsCookieJar, list, tuple, str, dict], + set_session: bool = ..., + set_driver: bool = ...) -> None: ... + + def _set_session(self, data: dict) -> None: ... + + def cookies_to_session(self, copy_user_agent: bool = False) -> None: ... + + def cookies_to_driver(self, url: str) -> None: ... + + def close_driver(self, kill: bool = ...) -> None: ... + + def close_session(self) -> None: ... + + def close(self) -> None: ... + + +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) -> list: ... + + +def _kill_progress(pid: str = ..., port: int = ...) -> bool: ... diff --git a/DrissionPage/driver_element.py b/DrissionPage/driver_element.py index c09bf2f..252f7b5 100644 --- a/DrissionPage/driver_element.py +++ b/DrissionPage/driver_element.py @@ -11,7 +11,6 @@ from typing import Union, List, Any, Tuple from selenium.common.exceptions import TimeoutException, JavascriptException, InvalidElementStateException, \ NoSuchElementException -# from selenium.webdriver.chrome.webdriver import WebDriver from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver from selenium.webdriver.remote.webelement import WebElement from selenium.webdriver.support import expected_conditions as ec @@ -979,9 +978,9 @@ class Select(object): if ele.tag != 'select': raise TypeError(f"select方法只能在