From b822784fdc29273cc88e86d13f76990e979284f9 Mon Sep 17 00:00:00 2001 From: g1879 Date: Thu, 19 Oct 2023 18:02:13 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84WebPage=E5=92=8CWebPageTab?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E6=9C=AA=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_base.py | 23 ++++----- DrissionPage/chromium_base.pyi | 6 +-- DrissionPage/chromium_frame.py | 2 +- DrissionPage/chromium_frame.pyi | 2 +- DrissionPage/chromium_page.py | 2 +- DrissionPage/chromium_tab.py | 25 +++------ DrissionPage/chromium_tab.pyi | 2 +- DrissionPage/session_page.py | 8 +-- DrissionPage/session_page.pyi | 6 +-- DrissionPage/web_page.py | 92 ++------------------------------- DrissionPage/web_page.pyi | 5 -- 11 files changed, 33 insertions(+), 140 deletions(-) diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index a0c5c2b..8b6d0c3 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -54,13 +54,13 @@ class ChromiumBase(BasePage): if isinstance(address, int) or (isinstance(address, str) and address.isdigit()): address = f'127.0.0.1:{address}' - self._set_start_options(address, None) - self._set_runtime_settings() + self._d_set_start_options(address, None) + self._d_set_runtime_settings() self._connect_browser(tab_id) if timeout is not None: self.timeout = timeout - def _set_start_options(self, address, none): + def _d_set_start_options(self, address, none): """设置浏览器启动属性 :param address: 'ip:port' :param none: 用于后代继承 @@ -68,7 +68,7 @@ class ChromiumBase(BasePage): """ self.address = address.replace('localhost', '127.0.0.1').lstrip('http://').lstrip('https://') - def _set_runtime_settings(self): + def _d_set_runtime_settings(self): self._timeouts = Timeout(self) self._page_load_strategy = 'normal' @@ -77,7 +77,12 @@ class ChromiumBase(BasePage): :param tab_id: 要控制的标签页id,不指定默认为激活的 :return: None """ - self._chromium_init() + self._first_run = True + self._is_reading = False + self._upload_list = None + self._wait = None + self._scroll = None + if not tab_id: json = get(f'http://{self.address}/json', headers={'Connection': 'close'}).json() @@ -89,14 +94,6 @@ class ChromiumBase(BasePage): self._get_document() self._first_run = False - def _chromium_init(self): - """浏览器初始设置""" - self._first_run = True - self._is_reading = False - self._upload_list = None - self._wait = None - self._scroll = None - def _driver_init(self, tab_id): """新建页面、页面刷新、切换标签页后要进行的cdp参数初始化 :param tab_id: 要跳转到的标签页id diff --git a/DrissionPage/chromium_base.pyi b/DrissionPage/chromium_base.pyi index 9f6d569..3a90a70 100644 --- a/DrissionPage/chromium_base.pyi +++ b/DrissionPage/chromium_base.pyi @@ -50,8 +50,6 @@ class ChromiumBase(BasePage): def _connect_browser(self, tab_id: str = None) -> None: ... - def _chromium_init(self): ... - def _driver_init(self, tab_id: str) -> None: ... def _get_document(self) -> None: ... @@ -76,9 +74,9 @@ class ChromiumBase(BasePage): # def _onDownloadWillBegin(self, **kwargs): ... - def _set_start_options(self, address, none) -> None: ... + def _d_set_start_options(self, address, none) -> None: ... - def _set_runtime_settings(self) -> None: ... + def _d_set_runtime_settings(self) -> None: ... def __call__(self, loc_or_str: Union[Tuple[str, str], str, ChromiumElement], timeout: float = None) -> ChromiumElement: ... diff --git a/DrissionPage/chromium_frame.py b/DrissionPage/chromium_frame.py index ca42c25..71d3b0d 100644 --- a/DrissionPage/chromium_frame.py +++ b/DrissionPage/chromium_frame.py @@ -74,7 +74,7 @@ class ChromiumFrame(ChromiumBase): attrs = [f"{attr}='{attrs[attr]}'" for attr in attrs] return f'' - def _set_runtime_settings(self): + def _d_set_runtime_settings(self): """重写设置浏览器运行参数方法""" self._timeouts = copy(self._target_page.timeouts) self.retry_times = self._target_page.retry_times diff --git a/DrissionPage/chromium_frame.pyi b/DrissionPage/chromium_frame.pyi index c125fa4..086565a 100644 --- a/DrissionPage/chromium_frame.pyi +++ b/DrissionPage/chromium_frame.pyi @@ -38,7 +38,7 @@ class ChromiumFrame(ChromiumBase): def __repr__(self) -> str: ... - def _set_runtime_settings(self) -> None: ... + def _d_set_runtime_settings(self) -> None: ... def _driver_init(self, tab_id: str) -> None: ... diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index 9358f99..3ff1b95 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -63,7 +63,7 @@ class ChromiumPage(ChromiumBase): headers={'Connection': 'close'}).json()['webSocketDebuggerUrl'] self._browser = Browser(self._driver_options.debugger_address, ws.split('/')[-1], self) - def _set_runtime_settings(self): + def _d_set_runtime_settings(self): """设置运行时用到的属性""" self._timeouts = Timeout(self, page_load=self._driver_options.timeouts['pageLoad'], diff --git a/DrissionPage/chromium_tab.py b/DrissionPage/chromium_tab.py index 9b1d60d..556fff4 100644 --- a/DrissionPage/chromium_tab.py +++ b/DrissionPage/chromium_tab.py @@ -5,12 +5,13 @@ """ from copy import copy -from .waiter import ChromiumTabWaiter +from .base import BasePage from .chromium_base import ChromiumBase from .commons.web import set_session_cookies, set_browser_cookies from .session_page import SessionPage from .setter import TabSetter from .setter import WebPageTabSetter +from .waiter import ChromiumTabWaiter class ChromiumTab(ChromiumBase): @@ -25,7 +26,7 @@ class ChromiumTab(ChromiumBase): self._browser = page.browser super().__init__(page.address, tab_id, page.timeout) - def _set_runtime_settings(self): + def _d_set_runtime_settings(self): """重写设置浏览器运行参数方法""" self._timeouts = copy(self.page.timeouts) self.retry_times = self.page.retry_times @@ -62,29 +63,17 @@ class ChromiumTab(ChromiumBase): return self._wait -class WebPageTab(SessionPage, ChromiumTab): +class WebPageTab(SessionPage, ChromiumTab, BasePage): def __init__(self, page, tab_id): """ :param page: WebPage对象 :param tab_id: 要控制的标签页id """ - self._page = page - self._browser = page.browser - self.address = page.address - self._debug = page._debug - self._debug_recorder = page._debug_recorder self._mode = 'd' self._has_driver = True self._has_session = True - self._session = copy(page.session) - self._response = None - self._set = None - - self._download_set = None - self._download_path = page.download_path - self._DownloadKit = None - super(SessionPage, self)._set_runtime_settings() - self._connect_browser(tab_id) + super().__init__(session_or_options=copy(page.session)) + super(SessionPage, self).__init__(page=page, tab_id=tab_id) def __call__(self, loc_or_str, timeout=None): """在内部查找元素 @@ -351,4 +340,4 @@ class WebPageTab(SessionPage, ChromiumTab): return super()._find_elements(loc_or_ele, single=single) elif self._mode == 'd': return super(SessionPage, self)._find_elements(loc_or_ele, timeout=timeout, single=single, - relative=relative) + relative=relative) \ No newline at end of file diff --git a/DrissionPage/chromium_tab.pyi b/DrissionPage/chromium_tab.pyi index 15d04b4..8a6db8c 100644 --- a/DrissionPage/chromium_tab.pyi +++ b/DrissionPage/chromium_tab.pyi @@ -26,7 +26,7 @@ class ChromiumTab(ChromiumBase): self._page: ChromiumPage = ... self._browser: Browser = ... - def _set_runtime_settings(self) -> None: ... + def _d_set_runtime_settings(self) -> None: ... def close(self) -> None: ... diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index 0931de5..fdad403 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -30,13 +30,13 @@ class SessionPage(BasePage): self._response = None self._session = None self._set = None - self._set_start_options(session_or_options, None) - self._set_runtime_settings() + self._s_set_start_options(session_or_options, None) + self._s_set_runtime_settings() self._create_session() if timeout is not None: self.timeout = timeout - def _set_start_options(self, session_or_options, none): + def _s_set_start_options(self, session_or_options, none): """启动配置 :param session_or_options: Session、SessionOptions :param none: 用于后代继承 @@ -49,7 +49,7 @@ class SessionPage(BasePage): self._session_options = SessionOptions() self._session = session_or_options - def _set_runtime_settings(self): + def _s_set_runtime_settings(self): """设置运行时用到的属性""" self._timeout = self._session_options.timeout self._download_path = self._session_options.download_path diff --git a/DrissionPage/session_page.pyi b/DrissionPage/session_page.pyi index 5391a4a..76bba05 100644 --- a/DrissionPage/session_page.pyi +++ b/DrissionPage/session_page.pyi @@ -31,12 +31,12 @@ class SessionPage(BasePage): self.retry_interval: float = ... self._set: SessionPageSetter = ... - def _set_start_options(self, session_or_options, none) -> None: ... + def _s_set_start_options(self, session_or_options, none) -> None: ... + + def _s_set_runtime_settings(self) -> None: ... def _create_session(self) -> None: ... - def _set_runtime_settings(self) -> None: ... - def __call__(self, loc_or_str: Union[Tuple[str, str], str, SessionElement], timeout: float = None) -> Union[SessionElement, str, NoneElement]: ... diff --git a/DrissionPage/web_page.py b/DrissionPage/web_page.py index 426eae5..ce1aabf 100644 --- a/DrissionPage/web_page.py +++ b/DrissionPage/web_page.py @@ -3,16 +3,11 @@ @Author : g1879 @Contact : g1879@qq.com """ -from requests import Session from .base import BasePage -from .chromium_base import ChromiumBase, Timeout -from .chromium_driver import ChromiumDriver from .chromium_page import ChromiumPage from .chromium_tab import WebPageTab from .commons.web import set_session_cookies, set_browser_cookies -from .configs.chromium_options import ChromiumOptions -from .configs.session_options import SessionOptions from .session_page import SessionPage from .setter import WebPageSetter @@ -27,96 +22,15 @@ class WebPage(SessionPage, ChromiumPage, BasePage): :param driver_or_options: ChromiumDriver对象,只使用s模式时应传入False :param session_or_options: Session对象或SessionOptions对象,只使用d模式时应传入False """ - super(ChromiumBase, self).__init__() # 调用Base的__init__() self._mode = mode.lower() if self._mode not in ('s', 'd'): raise ValueError('mode参数只能是s或d。') self._has_driver = True self._has_session = True - self._debug = False - self._debug_recorder = None - self.address = None - - self._session = None - self._driver = None - self._driver_options = None - self._session_options = None - self._response = None - self._set = None - self._screencast = None - self._frames = {} - self._page = self - - self._set_start_options(driver_or_options, session_or_options) - self._set_runtime_settings() - self._connect_browser() - self._create_session() - self.set.timeouts(implicit=timeout) - - def _set_start_options(self, dr_opt, se_opt): - """处理两种模式的设置 - :param dr_opt: ChromiumDriver或ChromiumOptions对象,为None则从ini读取,为False用默认信息创建 - :param se_opt: Session、SessionOptions对象或配置信息,为None则从ini读取,为False用默认信息创建 - :return: None - """ - # 浏览器配置 - if isinstance(dr_opt, ChromiumDriver): - self._driver = dr_opt - self._driver_options = ChromiumOptions() - self._driver_options.debugger_address = dr_opt.address - dr_opt = False - - else: - if dr_opt is None: - self._driver_options = ChromiumOptions() - - elif dr_opt is False: - self._driver_options = ChromiumOptions(read_file=False) - - elif isinstance(dr_opt, ChromiumOptions): - self._driver_options = dr_opt - - else: - raise TypeError('driver_or_options参数只能接收ChromiumDriver, ChromiumOptions、None或False。') - - self.address = self._driver_options.debugger_address.replace('localhost', - '127.0.0.1').lstrip('http://').lstrip('https://') - - # Session配置 - if isinstance(se_opt, Session): - self._session = se_opt - self._session_options = SessionOptions() - se_opt = False - - else: - if se_opt is None: - self._session_options = SessionOptions() - - elif se_opt is False: - self._session_options = SessionOptions(read_file=False) - - elif isinstance(se_opt, SessionOptions): - self._session_options = se_opt - - else: - raise TypeError('session_or_options参数只能接收Session, SessionOptions、None或False。') - - self._timeouts = Timeout(self) - self._page_load_strategy = self._driver_options.page_load_strategy - - if se_opt is not False: - self.set.timeouts(implicit=self._session_options.timeout) - self._download_path = self._session_options.download_path - - if dr_opt is not False: - t = self._driver_options.timeouts - self.set.timeouts(t['implicit'], t['pageLoad'], t['script']) - self._download_path = self._driver_options.download_path - - def _set_runtime_settings(self): - """设置运行时用到的属性""" - pass + super().__init__(session_or_options=session_or_options, timeout=timeout) + super(SessionPage, self).__init__(addr_or_opts=driver_or_options, timeout=timeout) + self.change_mode(self._mode, go=False, copy_cookies=False) def __call__(self, loc_or_str, timeout=None): """在内部查找元素 diff --git a/DrissionPage/web_page.pyi b/DrissionPage/web_page.pyi index 460f2cf..a70dc0e 100644 --- a/DrissionPage/web_page.pyi +++ b/DrissionPage/web_page.pyi @@ -31,13 +31,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage): self._mode: str = ... self._has_driver: bool = ... self._has_session: bool = ... - self.address: str = ... self._session_options: Union[SessionOptions, None] = ... self._driver_options: Union[ChromiumOptions, None] = ... - self._DownloadKit: DownloadKit = ... - self._download_path: str = ... - self._driver: ChromiumDriver = ... - self._frames: dict = ... def __call__(self, loc_or_str: Union[Tuple[str, str], str, ChromiumElement, SessionElement],