重构WebPage和WebPageTab逻辑,未完成

This commit is contained in:
g1879 2023-10-19 18:02:13 +08:00
parent 955f8c27ae
commit b822784fdc
11 changed files with 33 additions and 140 deletions

View File

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

View File

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

View File

@ -74,7 +74,7 @@ class ChromiumFrame(ChromiumBase):
attrs = [f"{attr}='{attrs[attr]}'" for attr in attrs]
return f'<ChromiumFrame {self.frame_ele.tag} {" ".join(attrs)}>'
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

View File

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

View File

@ -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'],

View File

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

View File

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

View File

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

View File

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

View File

@ -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: SessionSessionOptions对象或配置信息为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):
"""在内部查找元素

View File

@ -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],