完善页面对象初始化重构;Page恢复接收Driver

This commit is contained in:
g1879 2023-10-20 00:13:00 +08:00
parent b822784fdc
commit 19f99b4d62
5 changed files with 30 additions and 21 deletions

View File

@ -43,7 +43,6 @@ class ChromiumBase(BasePage):
self._root_id = None # object id self._root_id = None # object id
self._debug = False self._debug = False
self._debug_recorder = None self._debug_recorder = None
self._driver = None
self._set = None self._set = None
self._screencast = None self._screencast = None
self._actions = None self._actions = None
@ -100,6 +99,8 @@ class ChromiumBase(BasePage):
:return: None :return: None
""" """
self._is_loading = True self._is_loading = True
if hasattr(self, '_driver'):
return
self._driver = ChromiumDriver(tab_id=tab_id, tab_type='page', address=self.address) self._driver = ChromiumDriver(tab_id=tab_id, tab_type='page', address=self.address)
self._driver.call_method('DOM.enable') self._driver.call_method('DOM.enable')

View File

@ -10,6 +10,7 @@ from requests import get
from .browser import Browser from .browser import Browser
from .chromium_base import ChromiumBase, Timeout from .chromium_base import ChromiumBase, Timeout
from .chromium_driver import ChromiumDriver
from .chromium_tab import ChromiumTab from .chromium_tab import ChromiumTab
from .commons.browser import connect_browser from .commons.browser import connect_browser
from .configs.chromium_options import ChromiumOptions from .configs.chromium_options import ChromiumOptions
@ -20,39 +21,42 @@ from .waiter import ChromiumPageWaiter
class ChromiumPage(ChromiumBase): 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 tab_id: 要控制的标签页id不指定默认为激活的
:param timeout: 超时时间 :param timeout: 超时时间
""" """
if addr_driver_opts:
addr_or_opts = addr_driver_opts
self._page = self self._page = self
address = self._handle_options(addr_or_opts) address = self._handle_options(addr_driver_opts)
self._run_browser() self._run_browser()
super().__init__(address, tab_id) super().__init__(address, tab_id)
self.set.timeouts(implicit=timeout) self.set.timeouts(implicit=timeout)
self._page_init() 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'ChromiumOptionsChromiumDriver
:return: 返回浏览器地址 :return: 返回浏览器地址
""" """
if not addr_or_opts: if not addr_driver_opts:
self._driver_options = ChromiumOptions(addr_or_opts) self._driver_options = ChromiumOptions(addr_driver_opts)
elif isinstance(addr_or_opts, ChromiumOptions): elif isinstance(addr_driver_opts, ChromiumOptions):
self._driver_options = addr_or_opts 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 = 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: else:
raise TypeError('只能接收ip:port格式或ChromiumOptions类型参数。') raise TypeError('只能接收ip:port格式、ChromiumOptions或ChromiumDriver类型参数。')
return self._driver_options.debugger_address return self._driver_options.debugger_address

View File

@ -7,6 +7,7 @@ from typing import Union, Tuple, List, Optional
from .browser import Browser from .browser import Browser
from .chromium_base import ChromiumBase from .chromium_base import ChromiumBase
from .chromium_driver import ChromiumDriver
from .chromium_tab import ChromiumTab from .chromium_tab import ChromiumTab
from .configs.chromium_options import ChromiumOptions from .configs.chromium_options import ChromiumOptions
from .setter import ChromiumPageSetter from .setter import ChromiumPageSetter
@ -16,7 +17,7 @@ from .waiter import ChromiumPageWaiter
class ChromiumPage(ChromiumBase): class ChromiumPage(ChromiumBase):
def __init__(self, def __init__(self,
addr_or_opts: Union[str, int, ChromiumOptions] = None, addr_driver_opts: Union[str, int, ChromiumOptions, ChromiumDriver] = None,
tab_id: str = None, tab_id: str = None,
timeout: float = None): timeout: float = None):
self._driver_options: ChromiumOptions = ... self._driver_options: ChromiumOptions = ...
@ -25,7 +26,7 @@ class ChromiumPage(ChromiumBase):
self._browser: Browser = ... self._browser: Browser = ...
self._rect: ChromiumTabRect = ... 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: ... def _run_browser(self) -> None: ...

View File

@ -26,7 +26,7 @@ class SessionPage(BasePage):
:param session_or_options: Session对象或SessionOptions对象 :param session_or_options: Session对象或SessionOptions对象
:param timeout: 连接超时时间为None时从ini文件读取 :param timeout: 连接超时时间为None时从ini文件读取
""" """
super().__init__() super(SessionPage, SessionPage).__init__(self)
self._response = None self._response = None
self._session = None self._session = None
self._set = None self._set = None

View File

@ -3,11 +3,11 @@
@Author : g1879 @Author : g1879
@Contact : g1879@qq.com @Contact : g1879@qq.com
""" """
from .base import BasePage from .base import BasePage
from .chromium_page import ChromiumPage from .chromium_page import ChromiumPage
from .chromium_tab import WebPageTab from .chromium_tab import WebPageTab
from .commons.web import set_session_cookies, set_browser_cookies from .commons.web import set_session_cookies, set_browser_cookies
from .configs.chromium_options import ChromiumOptions
from .session_page import SessionPage from .session_page import SessionPage
from .setter import WebPageSetter from .setter import WebPageSetter
@ -28,8 +28,11 @@ class WebPage(SessionPage, ChromiumPage, BasePage):
self._has_driver = True self._has_driver = True
self._has_session = True self._has_session = True
super().__init__(session_or_options=session_or_options, timeout=timeout) super().__init__(session_or_options=session_or_options)
super(SessionPage, self).__init__(addr_or_opts=driver_or_options, timeout=timeout) 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) self.change_mode(self._mode, go=False, copy_cookies=False)
def __call__(self, loc_or_str, timeout=None): def __call__(self, loc_or_str, timeout=None):