From 19f99b4d62302e7d9c6c682837b94df658738c4d Mon Sep 17 00:00:00 2001 From: g1879 Date: Fri, 20 Oct 2023 00:13:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E9=A1=B5=E9=9D=A2=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E5=88=9D=E5=A7=8B=E5=8C=96=E9=87=8D=E6=9E=84=EF=BC=9B?= =?UTF-8?q?Page=E6=81=A2=E5=A4=8D=E6=8E=A5=E6=94=B6Driver?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_base.py | 3 ++- DrissionPage/chromium_page.py | 32 ++++++++++++++++++-------------- DrissionPage/chromium_page.pyi | 5 +++-- DrissionPage/session_page.py | 2 +- DrissionPage/web_page.py | 9 ++++++--- 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index 8b6d0c3..d820918 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -43,7 +43,6 @@ class ChromiumBase(BasePage): self._root_id = None # object id self._debug = False self._debug_recorder = None - self._driver = None self._set = None self._screencast = None self._actions = None @@ -100,6 +99,8 @@ class ChromiumBase(BasePage): :return: None """ self._is_loading = True + if hasattr(self, '_driver'): + return self._driver = ChromiumDriver(tab_id=tab_id, tab_type='page', address=self.address) self._driver.call_method('DOM.enable') diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index 3ff1b95..bbee74b 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -10,6 +10,7 @@ from requests import get from .browser import Browser from .chromium_base import ChromiumBase, Timeout +from .chromium_driver import ChromiumDriver from .chromium_tab import ChromiumTab from .commons.browser import connect_browser from .configs.chromium_options import ChromiumOptions @@ -20,39 +21,42 @@ from .waiter import ChromiumPageWaiter class ChromiumPage(ChromiumBase): """用于管理浏览器的类""" - def __init__(self, addr_or_opts=None, tab_id=None, timeout=None, addr_driver_opts=None): + def __init__(self, addr_driver_opts=None, tab_id=None, timeout=None): """ - :param addr_or_opts: 浏览器地址:端口或ChromiumOptions对象 + :param addr_driver_opts: 浏览器地址:端口或ChromiumOptions对象 :param tab_id: 要控制的标签页id,不指定默认为激活的 :param timeout: 超时时间 """ - if addr_driver_opts: - addr_or_opts = addr_driver_opts self._page = self - address = self._handle_options(addr_or_opts) + address = self._handle_options(addr_driver_opts) self._run_browser() super().__init__(address, tab_id) self.set.timeouts(implicit=timeout) self._page_init() - def _handle_options(self, addr_or_opts): + def _handle_options(self, addr_driver_opts): """设置浏览器启动属性 - :param addr_or_opts: 'ip:port'、ChromiumOptions + :param addr_driver_opts: 'ip:port'、ChromiumOptions、ChromiumDriver :return: 返回浏览器地址 """ - if not addr_or_opts: - self._driver_options = ChromiumOptions(addr_or_opts) + if not addr_driver_opts: + self._driver_options = ChromiumOptions(addr_driver_opts) - elif isinstance(addr_or_opts, ChromiumOptions): - self._driver_options = addr_or_opts + elif isinstance(addr_driver_opts, ChromiumOptions): + self._driver_options = addr_driver_opts # 接收浏览器地址和端口 - elif isinstance(addr_or_opts, str): + elif isinstance(addr_driver_opts, str): self._driver_options = ChromiumOptions() - self._driver_options.debugger_address = addr_or_opts + self._driver_options.debugger_address = addr_driver_opts + + elif isinstance(addr_driver_opts, ChromiumDriver): + self._driver_options = ChromiumOptions(False) + self._driver_options.debugger_address = addr_driver_opts.address + self._driver = addr_driver_opts else: - raise TypeError('只能接收ip:port格式或ChromiumOptions类型参数。') + raise TypeError('只能接收ip:port格式、ChromiumOptions或ChromiumDriver类型参数。') return self._driver_options.debugger_address diff --git a/DrissionPage/chromium_page.pyi b/DrissionPage/chromium_page.pyi index 5b05ae5..a152caa 100644 --- a/DrissionPage/chromium_page.pyi +++ b/DrissionPage/chromium_page.pyi @@ -7,6 +7,7 @@ from typing import Union, Tuple, List, Optional from .browser import Browser from .chromium_base import ChromiumBase +from .chromium_driver import ChromiumDriver from .chromium_tab import ChromiumTab from .configs.chromium_options import ChromiumOptions from .setter import ChromiumPageSetter @@ -16,7 +17,7 @@ from .waiter import ChromiumPageWaiter class ChromiumPage(ChromiumBase): def __init__(self, - addr_or_opts: Union[str, int, ChromiumOptions] = None, + addr_driver_opts: Union[str, int, ChromiumOptions, ChromiumDriver] = None, tab_id: str = None, timeout: float = None): self._driver_options: ChromiumOptions = ... @@ -25,7 +26,7 @@ class ChromiumPage(ChromiumBase): self._browser: Browser = ... self._rect: ChromiumTabRect = ... - def _handle_options(self, addr_or_opts) -> str: ... + def _handle_options(self, addr_driver_opts: Union[str, ChromiumDriver, ChromiumOptions]) -> str: ... def _run_browser(self) -> None: ... diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index fdad403..9bf1e1a 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -26,7 +26,7 @@ class SessionPage(BasePage): :param session_or_options: Session对象或SessionOptions对象 :param timeout: 连接超时时间,为None时从ini文件读取 """ - super().__init__() + super(SessionPage, SessionPage).__init__(self) self._response = None self._session = None self._set = None diff --git a/DrissionPage/web_page.py b/DrissionPage/web_page.py index ce1aabf..05dd9f5 100644 --- a/DrissionPage/web_page.py +++ b/DrissionPage/web_page.py @@ -3,11 +3,11 @@ @Author : g1879 @Contact : g1879@qq.com """ - from .base import BasePage 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 .session_page import SessionPage from .setter import WebPageSetter @@ -28,8 +28,11 @@ class WebPage(SessionPage, ChromiumPage, BasePage): self._has_driver = True self._has_session = True - super().__init__(session_or_options=session_or_options, timeout=timeout) - super(SessionPage, self).__init__(addr_or_opts=driver_or_options, timeout=timeout) + super().__init__(session_or_options=session_or_options) + if not driver_or_options: + driver_or_options = ChromiumOptions(read_file=driver_or_options) + driver_or_options.set_timeouts(implicit=self._timeout).set_paths(download_path=self.download_path) + super(SessionPage, self).__init__(addr_driver_opts=driver_or_options, timeout=timeout) self.change_mode(self._mode, go=False, copy_cookies=False) def __call__(self, loc_or_str, timeout=None):