From 341591edc64a62bb73cd7c8e950432750d0146c1 Mon Sep 17 00:00:00 2001 From: g1879 Date: Thu, 26 Oct 2023 23:15:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E5=86=8D=E4=BD=BF=E7=94=A8=E4=BC=A0?= =?UTF-8?q?=E9=80=92ChromiumDriver=E6=96=B9=E5=BC=8F=E5=88=9B=E5=BB=BA`Chr?= =?UTF-8?q?omiumPage`=E5=92=8C`WebPage`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_pages/chromium_base.py | 5 +--- DrissionPage/_pages/chromium_base.pyi | 2 +- DrissionPage/_pages/chromium_page.py | 34 ++++++++++++--------------- DrissionPage/_pages/chromium_page.pyi | 5 ++-- DrissionPage/_pages/web_page.py | 14 ++++++----- DrissionPage/_pages/web_page.pyi | 2 +- setup.py | 2 +- 7 files changed, 29 insertions(+), 35 deletions(-) diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 1159b17..0d6ceb6 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -93,15 +93,12 @@ class ChromiumBase(BasePage): self._get_document() self._first_run = False - def _driver_init(self, tab_id, is_init=True): + def _driver_init(self, tab_id): """新建页面、页面刷新、切换标签页后要进行的cdp参数初始化 :param tab_id: 要跳转到的标签页id - :param is_init: 是否初始化时执行本方法,用于判断是否to_tab()调用 :return: None """ self._is_loading = True - if is_init and hasattr(self, '_driver'): - return # ChromiumPage接收ChromiumDriver方式启动时 self._driver = ChromiumDriver(tab_id=tab_id, tab_type='page', address=self.address) self._alert = Alert() self._driver.set_listener('Page.javascriptDialogOpening', self._on_alert_open) diff --git a/DrissionPage/_pages/chromium_base.pyi b/DrissionPage/_pages/chromium_base.pyi index 4ff2964..fb8e148 100644 --- a/DrissionPage/_pages/chromium_base.pyi +++ b/DrissionPage/_pages/chromium_base.pyi @@ -53,7 +53,7 @@ class ChromiumBase(BasePage): def _connect_browser(self, tab_id: str = None) -> None: ... - def _driver_init(self, tab_id: str, is_init: bool = True) -> None: ... + def _driver_init(self, tab_id: str) -> None: ... def _get_document(self) -> None: ... diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index 98e67ef..30d0e71 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -9,7 +9,6 @@ from time import sleep from requests import get from .._base.browser import Browser -from .._base.chromium_driver import ChromiumDriver from .._commons.browser import connect_browser from .._configs.chromium_options import ChromiumOptions from .._pages.chromium_base import ChromiumBase, Timeout @@ -23,42 +22,39 @@ from ..errors import BrowserConnectError class ChromiumPage(ChromiumBase): """用于管理浏览器的类""" - def __init__(self, addr_driver_opts=None, tab_id=None, timeout=None): + def __init__(self, addr_or_opts=None, tab_id=None, timeout=None, addr_driver_opts=None): """ - :param addr_driver_opts: 浏览器地址:端口或ChromiumOptions对象 + :param addr_or_opts: 浏览器地址:端口或ChromiumOptions对象 :param tab_id: 要控制的标签页id,不指定默认为激活的 :param timeout: 超时时间 """ + if not addr_or_opts and addr_driver_opts: + addr_or_opts = addr_driver_opts self._page = self - address = self._handle_options(addr_driver_opts) + address = self._handle_options(addr_or_opts) self._run_browser() super().__init__(address, tab_id) self.set.timeouts(implicit=timeout) self._page_init() - def _handle_options(self, addr_driver_opts): + def _handle_options(self, addr_or_opts): """设置浏览器启动属性 - :param addr_driver_opts: 'ip:port'、ChromiumOptions、ChromiumDriver + :param addr_or_opts: 'ip:port'、ChromiumOptions、ChromiumDriver :return: 返回浏览器地址 """ - if not addr_driver_opts: - self._driver_options = ChromiumOptions(addr_driver_opts) + if not addr_or_opts: + self._driver_options = ChromiumOptions(addr_or_opts) - elif isinstance(addr_driver_opts, ChromiumOptions): - self._driver_options = addr_driver_opts + elif isinstance(addr_or_opts, ChromiumOptions): + self._driver_options = addr_or_opts # 接收浏览器地址和端口 - elif isinstance(addr_driver_opts, str): + elif isinstance(addr_or_opts, str): self._driver_options = ChromiumOptions() - self._driver_options.set_debugger_address(addr_driver_opts) - - elif isinstance(addr_driver_opts, ChromiumDriver): - self._driver_options = ChromiumOptions(False) - self._driver_options.set_debugger_address(addr_driver_opts.address) - self._driver = addr_driver_opts + self._driver_options.set_debugger_address(addr_or_opts) else: - raise TypeError('只能接收ip:port格式、ChromiumOptions或ChromiumDriver类型参数。') + raise TypeError('只能接收ip:port格式或ChromiumOptions类型参数。') return self._driver_options.debugger_address @@ -229,7 +225,7 @@ class ChromiumPage(ChromiumBase): return self.driver.stop() - self._driver_init(tab_id, False) + self._driver_init(tab_id) if read_doc and self.ready_state in ('complete', None): self._get_document() diff --git a/DrissionPage/_pages/chromium_page.pyi b/DrissionPage/_pages/chromium_page.pyi index bf82ca6..5295ba0 100644 --- a/DrissionPage/_pages/chromium_page.pyi +++ b/DrissionPage/_pages/chromium_page.pyi @@ -6,7 +6,6 @@ from typing import Union, Tuple, List, Optional from .._base.browser import Browser -from .._base.chromium_driver import ChromiumDriver from .._configs.chromium_options import ChromiumOptions from .._pages.chromium_base import ChromiumBase from .._pages.chromium_tab import ChromiumTab @@ -18,7 +17,7 @@ from .._units.waiter import ChromiumPageWaiter class ChromiumPage(ChromiumBase): def __init__(self, - addr_driver_opts: Union[str, int, ChromiumOptions, ChromiumDriver] = None, + addr_or_opts: Union[str, int, ChromiumOptions] = None, tab_id: str = None, timeout: float = None): self._driver_options: ChromiumOptions = ... @@ -26,7 +25,7 @@ class ChromiumPage(ChromiumBase): self._browser: Browser = ... self._rect: Optional[ChromiumTabRect] = ... - def _handle_options(self, addr_driver_opts: Union[str, ChromiumDriver, ChromiumOptions]) -> str: ... + def _handle_options(self, addr_or_opts: Union[str, ChromiumOptions]) -> str: ... def _run_browser(self) -> None: ... diff --git a/DrissionPage/_pages/web_page.py b/DrissionPage/_pages/web_page.py index bc940b2..698193d 100644 --- a/DrissionPage/_pages/web_page.py +++ b/DrissionPage/_pages/web_page.py @@ -15,13 +15,15 @@ from .._units.setter import WebPageSetter class WebPage(SessionPage, ChromiumPage, BasePage): """整合浏览器和request的页面类""" - def __init__(self, mode='d', timeout=None, driver_or_options=None, session_or_options=None): + def __init__(self, mode='d', timeout=None, driver_options=None, session_or_options=None, driver_or_options=None): """初始化函数 :param mode: 'd' 或 's',即driver模式和session模式 :param timeout: 超时时间,d模式时为寻找元素时间,s模式时为连接时间,默认10秒 - :param driver_or_options: ChromiumDriver对象,只使用s模式时应传入False + :param driver_options: ChromiumDriver对象,只使用s模式时应传入False :param session_or_options: Session对象或SessionOptions对象,只使用d模式时应传入False """ + if not driver_options and driver_or_options: + driver_options = driver_or_options self._mode = mode.lower() if self._mode not in ('s', 'd'): raise ValueError('mode参数只能是s或d。') @@ -29,10 +31,10 @@ class WebPage(SessionPage, ChromiumPage, BasePage): self._has_session = True 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) + if not driver_options: + driver_options = ChromiumOptions(read_file=driver_options) + driver_options.set_timeouts(implicit=self._timeout).set_paths(download_path=self.download_path) + super(SessionPage, self).__init__(addr_or_opts=driver_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/_pages/web_page.pyi b/DrissionPage/_pages/web_page.pyi index 7939f5a..6509692 100644 --- a/DrissionPage/_pages/web_page.pyi +++ b/DrissionPage/_pages/web_page.pyi @@ -25,7 +25,7 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def __init__(self, mode: str = 'd', timeout: float = None, - driver_or_options: Union[ChromiumDriver, ChromiumOptions, bool] = None, + driver_options: Union[ChromiumOptions, bool] = None, session_or_options: Union[Session, SessionOptions, bool] = None) -> None: self._mode: str = ... self._has_driver: bool = ... diff --git a/setup.py b/setup.py index ab09e66..473d567 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh: setup( name="DrissionPage", - version="3.3.0", + version="4.0.0b0", author="g1879", author_email="g1879@qq.com", description="Python based web automation tool. It can control the browser and send and receive data packets.",