不再使用传递ChromiumDriver方式创建ChromiumPageWebPage

This commit is contained in:
g1879 2023-10-26 23:15:21 +08:00
parent cd1369e33a
commit 341591edc6
7 changed files with 29 additions and 35 deletions

View File

@ -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)

View File

@ -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: ...

View File

@ -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'ChromiumOptionsChromiumDriver
:param addr_or_opts: 'ip:port'ChromiumOptionsChromiumDriver
: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()

View File

@ -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: ...

View File

@ -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):

View File

@ -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 = ...

View File

@ -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.",