diff --git a/DrissionPage/__init__.py b/DrissionPage/__init__.py index 027baa7..b29f1e8 100644 --- a/DrissionPage/__init__.py +++ b/DrissionPage/__init__.py @@ -5,7 +5,7 @@ @Copyright: (c) 2024 by g1879, Inc. All Rights Reserved. @License : BSD 3-Clause. """ -from ._base.browser import Browser +from ._base.browser import Chromium from ._configs.chromium_options import ChromiumOptions from ._configs.session_options import SessionOptions # 即将废弃 diff --git a/DrissionPage/__init__.pyi b/DrissionPage/__init__.pyi index 437adc7..055e4fd 100644 --- a/DrissionPage/__init__.pyi +++ b/DrissionPage/__init__.pyi @@ -5,7 +5,7 @@ @Copyright: (c) 2024 by g1879, Inc. All Rights Reserved. @License : BSD 3-Clause. """ -from ._base.browser import Browser +from ._base.browser import Chromium from ._configs.chromium_options import ChromiumOptions from ._configs.session_options import SessionOptions from ._pages.session_page import SessionPage @@ -13,5 +13,5 @@ from ._pages.session_page import SessionPage from ._pages.chromium_page import ChromiumPage from ._pages.web_page import WebPage -__all__ = ['WebPage', 'ChromiumPage', 'Browser', 'ChromiumOptions', 'SessionOptions', 'SessionPage', '__version__'] +__all__ = ['WebPage', 'ChromiumPage', 'Chromium', 'ChromiumOptions', 'SessionOptions', 'SessionPage', '__version__'] __version__: str = ... diff --git a/DrissionPage/_base/browser.py b/DrissionPage/_base/browser.py index 912e652..f3c2a86 100644 --- a/DrissionPage/_base/browser.py +++ b/DrissionPage/_base/browser.py @@ -32,7 +32,7 @@ from ..errors import PageDisconnectedError __ERROR__ = 'error' -class Browser(object): +class Chromium(object): _BROWSERS = {} def __new__(cls, addr_or_opts=None, session_options=None): @@ -61,7 +61,7 @@ class Browser(object): return self._created = True - self._type = 'Browser' + self._type = 'Chromium' self._frames = {} self._drivers = {} @@ -491,7 +491,7 @@ class Browser(object): return r if __ERROR__ not in r else raise_error(r, ignore) def _on_disconnect(self): - Browser._BROWSERS.pop(self.id, None) + Chromium._BROWSERS.pop(self.id, None) if self._chromium_options.is_auto_port and self._chromium_options.user_data_path: path = Path(self._chromium_options.user_data_path) end_time = perf_counter() + 7 diff --git a/DrissionPage/_base/browser.pyi b/DrissionPage/_base/browser.pyi index b8fcf75..d772f9e 100644 --- a/DrissionPage/_base/browser.pyi +++ b/DrissionPage/_base/browser.pyi @@ -19,7 +19,7 @@ from .._units.setter import BrowserSetter from .._units.waiter import BrowserWaiter -class Browser(object): +class Chromium(object): id: str = ... address: str = ... version: str = ... diff --git a/DrissionPage/_base/driver.pyi b/DrissionPage/_base/driver.pyi index b3f44f9..b3e09f0 100644 --- a/DrissionPage/_base/driver.pyi +++ b/DrissionPage/_base/driver.pyi @@ -12,7 +12,7 @@ from typing import Union, Callable, Dict, Optional from requests import Response, Session from websocket import WebSocket -from .browser import Browser +from .browser import Chromium class GenericAttr(object): @@ -67,11 +67,11 @@ class Driver(object): class BrowserDriver(Driver): BROWSERS: Dict[str, Driver] = ... - owner: Browser = ... + owner: Chromium = ... _control_session: Session = ... - def __new__(cls, tab_id: str, tab_type: str, address: str, owner: Browser): ... + def __new__(cls, tab_id: str, tab_type: str, address: str, owner: Chromium): ... - def __init__(self, tab_id: str, tab_type: str, address: str, owner: Browser): ... + def __init__(self, tab_id: str, tab_type: str, address: str, owner: Chromium): ... def get(self, url) -> Response: ... diff --git a/DrissionPage/_functions/cookies.pyi b/DrissionPage/_functions/cookies.pyi index fca7a77..cfee879 100644 --- a/DrissionPage/_functions/cookies.pyi +++ b/DrissionPage/_functions/cookies.pyi @@ -11,7 +11,7 @@ from typing import Union from requests import Session from requests.cookies import RequestsCookieJar -from .._base.browser import Browser +from .._base.browser import Chromium from .._pages.chromium_base import ChromiumBase @@ -24,7 +24,7 @@ def cookies_to_tuple(cookies: Union[RequestsCookieJar, list, tuple, str, dict, C def set_session_cookies(session: Session, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ... -def set_browser_cookies(browser: Browser, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ... +def set_browser_cookies(browser: Chromium, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ... def set_tab_cookies(page: ChromiumBase, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ... diff --git a/DrissionPage/_functions/web.py b/DrissionPage/_functions/web.py index 8527bb4..c4c6242 100644 --- a/DrissionPage/_functions/web.py +++ b/DrissionPage/_functions/web.py @@ -346,7 +346,9 @@ def format_headers(txt): :param txt: 从浏览器复制的原始文本格式headers :return: dict格式headers """ - if not isinstance(txt, str): + if isinstance(txt, dict): + for k, v in txt.items(): + txt[k] = str(v) return txt headers = {} for header in txt.split('\n'): diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 01e908a..6aea14a 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -41,7 +41,7 @@ class ChromiumBase(BasePage): def __init__(self, browser, tab_id=None): """ - :param browser: Browser + :param browser: Chromium :param tab_id: 要控制的标签页id,不指定默认为激活的 """ super().__init__() diff --git a/DrissionPage/_pages/chromium_base.pyi b/DrissionPage/_pages/chromium_base.pyi index 4d3f739..3f946c1 100644 --- a/DrissionPage/_pages/chromium_base.pyi +++ b/DrissionPage/_pages/chromium_base.pyi @@ -10,7 +10,7 @@ from typing import Union, Tuple, List, Any, Optional, Literal from .chromium_tab import ChromiumTab, MixTab from .._base.base import BasePage -from .._base.browser import Browser +from .._base.browser import Chromium from .._base.driver import Driver from .._elements.chromium_element import ChromiumElement from .._elements.session_element import SessionElement @@ -32,10 +32,10 @@ PIC_TYPE = Literal['jpg', 'jpeg', 'png', 'webp', True] class ChromiumBase(BasePage): def __init__(self, - browser: Browser, + browser: Chromium, tab_id: str = None): self._tab: Union[ChromiumTab, MixTab, ChromiumFrame] = ... - self._browser: Browser = ... + self._browser: Chromium = ... self._driver: Driver = ... self._frame_id: str = ... self._is_reading: bool = ... @@ -102,7 +102,7 @@ class ChromiumBase(BasePage): def _js_ready_state(self) -> str: ... @property - def browser(self) -> Browser: ... + def browser(self) -> Chromium: ... @property def title(self) -> str: ... diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index 3aca524..7552aea 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -7,7 +7,7 @@ """ from time import sleep -from .._base.browser import Browser +from .._base.browser import Chromium from .._functions.web import save_page from .._pages.chromium_base import ChromiumBase from .._units.setter import ChromiumPageSetter @@ -24,7 +24,7 @@ class ChromiumPage(ChromiumBase): :param tab_id: 要控制的标签页id,不指定默认为激活的 :param timeout: 超时时间(秒) """ - browser = Browser(addr_or_opts=addr_or_opts) + browser = Chromium(addr_or_opts=addr_or_opts) if browser.id in cls._PAGES: r = cls._PAGES[browser.id] while not hasattr(r, '_frame_id'): diff --git a/DrissionPage/_pages/chromium_page.pyi b/DrissionPage/_pages/chromium_page.pyi index 77e4924..241a30b 100644 --- a/DrissionPage/_pages/chromium_page.pyi +++ b/DrissionPage/_pages/chromium_page.pyi @@ -8,7 +8,7 @@ from pathlib import Path from typing import Union, Tuple, List, Optional -from .._base.browser import Browser +from .._base.browser import Chromium from .._configs.chromium_options import ChromiumOptions from .._pages.chromium_base import ChromiumBase from .._pages.chromium_tab import ChromiumTab @@ -20,7 +20,7 @@ from .._units.waiter import PageWaiter class ChromiumPage(ChromiumBase): _PAGES: dict = ... tab: ChromiumPage = ... - _browser: Browser = ... + _browser: Chromium = ... _rect: Optional[TabRect] = ... _is_exist: bool = ... @@ -41,7 +41,7 @@ class ChromiumPage(ChromiumBase): def _page_init(self) -> None: ... @property - def browser(self) -> Browser: ... + def browser(self) -> Chromium: ... @property def tabs_count(self) -> int: ... diff --git a/DrissionPage/_pages/chromium_tab.py b/DrissionPage/_pages/chromium_tab.py index 5b42498..f41ea7e 100644 --- a/DrissionPage/_pages/chromium_tab.py +++ b/DrissionPage/_pages/chromium_tab.py @@ -52,6 +52,7 @@ class ChromiumTab(ChromiumBase): def _d_set_runtime_settings(self): """重写设置浏览器运行参数方法""" + print('a') self._timeouts = copy(self.browser.timeouts) self.retry_times = self.browser.retry_times self.retry_interval = self.browser.retry_interval @@ -96,7 +97,7 @@ class ChromiumTab(ChromiumBase): class MixTab(SessionPage, ChromiumTab, BasePage): def __init__(self, browser, tab_id): """ - :param browser: Browser对象 + :param browser: Chromium对象 :param tab_id: 标签页id """ if Settings.singleton_tab_obj and hasattr(self, '_created'): diff --git a/DrissionPage/_pages/chromium_tab.pyi b/DrissionPage/_pages/chromium_tab.pyi index 288fdfe..7f0a234 100644 --- a/DrissionPage/_pages/chromium_tab.pyi +++ b/DrissionPage/_pages/chromium_tab.pyi @@ -13,7 +13,7 @@ from requests import Session, Response from .chromium_base import ChromiumBase from .chromium_frame import ChromiumFrame from .session_page import SessionPage -from .._base.browser import Browser +from .._base.browser import Chromium from .._elements.chromium_element import ChromiumElement from .._elements.session_element import SessionElement from .._functions.cookies import CookiesList @@ -26,9 +26,9 @@ from .._units.waiter import TabWaiter class ChromiumTab(ChromiumBase): _TABS: dict = ... - def __new__(cls, browser: Browser, tab_id: str): ... + def __new__(cls, browser: Chromium, tab_id: str): ... - def __init__(self, browser: Browser, tab_id: str): + def __init__(self, browser: Chromium, tab_id: str): self._tab: ChromiumTab = ... self._rect: Optional[TabRect] = ... @@ -70,7 +70,7 @@ class MixTab(SessionPage, ChromiumTab): _has_driver: bool = ... _has_session: bool = ... - def __init__(self, browser: Browser, tab_id: str): ... + def __init__(self, browser: Chromium, tab_id: str): ... def __call__(self, locator: Union[Tuple[str, str], str, ChromiumElement, SessionElement], diff --git a/DrissionPage/_units/cookies_setter.pyi b/DrissionPage/_units/cookies_setter.pyi index 1b467b1..dcc4215 100644 --- a/DrissionPage/_units/cookies_setter.pyi +++ b/DrissionPage/_units/cookies_setter.pyi @@ -8,7 +8,7 @@ from http.cookiejar import Cookie, CookieJar from typing import Union -from .._base.browser import Browser +from .._base.browser import Chromium from .._pages.chromium_base import ChromiumBase from .._pages.chromium_tab import MixTab from .._pages.session_page import SessionPage @@ -28,9 +28,9 @@ class CookiesSetter(object): class BrowserCookiesSetter(CookiesSetter): - _owner: Browser = ... + _owner: Chromium = ... - def __init__(self, page: Browser): ... + def __init__(self, page: Chromium): ... class SessionCookiesSetter(object): diff --git a/DrissionPage/_units/downloader.pyi b/DrissionPage/_units/downloader.pyi index 10fd5e3..d9768c4 100644 --- a/DrissionPage/_units/downloader.pyi +++ b/DrissionPage/_units/downloader.pyi @@ -7,13 +7,13 @@ """ from typing import Dict, Optional, Union, Literal -from .._base.browser import Browser +from .._base.browser import Chromium from .._pages.chromium_base import ChromiumBase # from .._pages.chromium_page import ChromiumPage FILE_EXISTS = Literal['skip', 'rename', 'overwrite', 's', 'r', 'o'] class DownloadManager(object): - _browser: Browser = ... + _browser: Chromium = ... # _page: ChromiumPage = ... _missions: Dict[str, DownloadMission] = ... _tab_missions: dict = ... @@ -21,7 +21,7 @@ class DownloadManager(object): _running: bool = ... # _save_path: Optional[str] = ... - def __init__(self, browser: Browser): ... + def __init__(self, browser: Chromium): ... @property def missions(self) -> Dict[str, DownloadMission]: ... diff --git a/DrissionPage/_units/setter.pyi b/DrissionPage/_units/setter.pyi index f39540f..1653ae7 100644 --- a/DrissionPage/_units/setter.pyi +++ b/DrissionPage/_units/setter.pyi @@ -14,7 +14,7 @@ from requests.auth import HTTPBasicAuth from .cookies_setter import SessionCookiesSetter, CookiesSetter, WebPageCookiesSetter, BrowserCookiesSetter from .scroller import PageScroller from .._base.base import BasePage -from .._base.browser import Browser +from .._base.browser import Chromium from .._elements.chromium_element import ChromiumElement from .._pages.chromium_base import ChromiumBase from .._pages.chromium_frame import ChromiumFrame @@ -27,8 +27,8 @@ FILE_EXISTS = Literal['skip', 'rename', 'overwrite', 's', 'r', 'o'] class BasePageSetter(object): - def __init__(self, owner: Union[Browser, BasePage]): - self._owner: Union[Browser, BasePage] = ... + def __init__(self, owner: Union[Chromium, BasePage]): + self._owner: Union[Chromium, BasePage] = ... def NoneElement_value(self, value: Any = None, on_off: bool = True) -> None: ... @@ -48,8 +48,8 @@ class BrowserBaseSetter(BasePageSetter): def timeouts(self, base=None, page_load=None, script=None) -> None: ... -class BrowserSetter(BasePageSetter): - _owner: Browser = ... +class BrowserSetter(BrowserBaseSetter): + _owner: Chromium = ... _cookies_setter: BrowserCookiesSetter = ... def tab_to_front(self, tab_or_id: Union[str, ChromiumTab]) -> None: ... @@ -221,9 +221,9 @@ class ChromiumFrameSetter(ChromiumBaseSetter): class LoadMode(object): - _owner: Union[Browser, ChromiumBase] = ... + _owner: Union[Chromium, ChromiumBase] = ... - def __init__(self, owner: Union[Browser, ChromiumBase]): ... + def __init__(self, owner: Union[Chromium, ChromiumBase]): ... def __call__(self, value: str) -> None: ... diff --git a/DrissionPage/_units/waiter.pyi b/DrissionPage/_units/waiter.pyi index f4c3966..243a014 100644 --- a/DrissionPage/_units/waiter.pyi +++ b/DrissionPage/_units/waiter.pyi @@ -8,7 +8,7 @@ from typing import Union, Tuple, Literal, List from .downloader import DownloadMission -from .._base.browser import Browser +from .._base.browser import Chromium from .._elements.chromium_element import ChromiumElement from .._pages.chromium_base import ChromiumBase from .._pages.chromium_frame import ChromiumFrame @@ -20,7 +20,7 @@ class OriginWaiter(object): class BrowserWaiter(OriginWaiter): - def __init__(self, owner: Browser): + def __init__(self, owner: Chromium): self._owner = owner def download_begin(self, timeout: float = None, cancel_it: bool = False) -> DownloadMission: ... diff --git a/DrissionPage/common.py b/DrissionPage/common.py index eb9adac..e7a0717 100644 --- a/DrissionPage/common.py +++ b/DrissionPage/common.py @@ -5,6 +5,7 @@ @Copyright: (c) 2024 by g1879, Inc. All Rights Reserved. @License : BSD 3-Clause. """ +from ._base.browser import Chromium from ._elements.session_element import make_session_ele from ._functions.by import By from ._functions.elements import get_eles @@ -12,7 +13,7 @@ from ._functions.keys import Keys from ._functions.settings import Settings from ._functions.tools import wait_until, configs_to_here from ._functions.web import get_blob, tree -from ._pages.chromium_page import ChromiumPage + from ._units.actions import Actions __all__ = ['make_session_ele', 'Actions', 'Keys', 'By', 'Settings', 'wait_until', 'configs_to_here', 'get_blob', @@ -20,15 +21,15 @@ __all__ = ['make_session_ele', 'Actions', 'Keys', 'By', 'Settings', 'wait_until' def from_selenium(driver): - """从selenium的WebDriver对象生成ChromiumPage对象""" + """从selenium的WebDriver对象生成Chromium对象""" address, port = driver.caps.get('goog:chromeOptions', {}).get('debuggerAddress', ':').split(':') if not address: raise RuntimeError('获取失败。') - return ChromiumPage(f'{address}:{port}') + return Chromium(f'{address}:{port}') def from_playwright(page_or_browser): - """从playwright的Page或Browser对象生成ChromiumPage对象""" + """从playwright的Page或Browser对象生成Chromium对象""" if hasattr(page_or_browser, 'context'): page_or_browser = page_or_browser.context.browser try: @@ -49,4 +50,4 @@ def from_playwright(page_or_browser): break else: raise RuntimeError('获取失败。') - return ChromiumPage(f'127.0.0.1:{port}') + return Chromium(f'127.0.0.1:{port}')