ChromiumPage基本完成

This commit is contained in:
g1879 2022-11-17 13:25:51 +08:00
parent a27e4c8fb6
commit 06877903cd
2 changed files with 21 additions and 9 deletions

View File

@ -90,6 +90,8 @@ class ChromiumPage(BasePage):
else: else:
raise TypeError('只能接收Tab或DriverOptions类型参数。') raise TypeError('只能接收Tab或DriverOptions类型参数。')
self._main_tab = self.tab_id
def _init_page(self, tab_id: str = None) -> None: def _init_page(self, tab_id: str = None) -> None:
"""新建页面、页面刷新、切换标签页后要进行的cdp参数初始化 """新建页面、页面刷新、切换标签页后要进行的cdp参数初始化
:param tab_id: 要跳转到的标签页id :param tab_id: 要跳转到的标签页id
@ -113,7 +115,7 @@ class ChromiumPage(BasePage):
def _get_document(self) -> None: def _get_document(self) -> None:
"""刷新cdp使用的document数据""" """刷新cdp使用的document数据"""
print('get doc') # print('get doc')
self._wait_loading() self._wait_loading()
root_id = self._driver.DOM.getDocument()['root']['nodeId'] root_id = self._driver.DOM.getDocument()['root']['nodeId']
self._root_id = self._driver.DOM.resolveNode(nodeId=root_id)['object']['objectId'] self._root_id = self._driver.DOM.resolveNode(nodeId=root_id)['object']['objectId']
@ -124,8 +126,7 @@ class ChromiumPage(BasePage):
:param timeout: 超时时间 :param timeout: 超时时间
:return: 是否成功超时返回False :return: 是否成功超时返回False
""" """
# timeout = timeout if timeout is not None else self.timeouts.page_load timeout = timeout if timeout is not None else self.timeouts.page_load
timeout = 100
end_time = perf_counter() + timeout end_time = perf_counter() + timeout
while perf_counter() < end_time: while perf_counter() < end_time:
@ -143,12 +144,12 @@ class ChromiumPage(BasePage):
def _onLoadEventFired(self, **kwargs): def _onLoadEventFired(self, **kwargs):
"""在页面刷新、变化后重新读取页面内容""" """在页面刷新、变化后重新读取页面内容"""
print('load complete') # print('load complete')
if self._first_run is False: if self._first_run is False:
self._get_document() self._get_document()
def _onFrameNavigated(self, **kwargs): def _onFrameNavigated(self, **kwargs):
print('nav') # print('nav')
# todo: 考虑frame的情况修改别的判断方式 # todo: 考虑frame的情况修改别的判断方式
if not kwargs['frame'].get('parentId', None): if not kwargs['frame'].get('parentId', None):
self._is_loading = True self._is_loading = True
@ -582,25 +583,37 @@ class ChromiumPage(BasePage):
"""激活当前标签页使其处于最前面""" """激活当前标签页使其处于最前面"""
self._ss.get(f'http://{self.address}/json/activate/{self.tab_id}') self._ss.get(f'http://{self.address}/json/activate/{self.tab_id}')
def set_main_tab(self, tab_id: str = None) -> None:
"""设置某个标签页为住标签页"""
self._main_tab = tab_id or self.tab_id
def new_tab(self, url: str = None) -> None: def new_tab(self, url: str = None) -> None:
"""新建并定位到一个标签页,该标签页在最后面 \n """新建并定位到一个标签页,该标签页在最后面 \n
:param url: 新标签页跳转到的网址 :param url: 新标签页跳转到的网址
:return: None :return: None
""" """
d = self.driver begin_len = len(self.tabs)
url = f'?{url}' if url else '' url = f'?{url}' if url else ''
self._ss.get(f'http://{self.address}/json/new{url}') self._ss.get(f'http://{self.address}/json/new{url}')
while len(self.tabs) < begin_len:
pass
self.to_tab() self.to_tab()
def to_main_tab(self) -> None:
"""跳转到主标签页"""
self.to_tab('main')
def to_tab(self, tab_id: str = None, activate: bool = True) -> None: def to_tab(self, tab_id: str = None, activate: bool = True) -> None:
"""跳转到标签页 \n """跳转到标签页 \n
:param tab_id: 标签页id字符串默认跳转到活动状态的 :param tab_id: 标签页id字符串默认跳转到main_tab
:param activate: 切换后是否变为活动状态 :param activate: 切换后是否变为活动状态
:return: None :return: None
""" """
tabs = self.tabs tabs = self.tabs
if not tab_id: if not tab_id:
tab_id = tabs[0] tab_id = tabs[0]
elif tab_id == 'main':
tab_id = self._main_tab
if tab_id == self.tab_id or tab_id not in tabs: if tab_id == self.tab_id or tab_id not in tabs:
return return
@ -677,7 +690,6 @@ class ChromiumPage(BasePage):
timeout = timeout or self.timeout timeout = timeout or self.timeout
end_time = perf_counter() + timeout end_time = perf_counter() + timeout
while not self._alert.activated and perf_counter() < end_time: while not self._alert.activated and perf_counter() < end_time:
print('vvv')
sleep(.1) sleep(.1)
if not self._alert.activated: if not self._alert.activated:
return None return None

View File

@ -21,7 +21,7 @@ DrissionPage即 driver 和 session 组合而成的 page。
除了合并两者,本库还以网页为单位封装了常用功能,提供非常简便的操作和语句,在用于网页自动化操作时,减少考虑细节,专注功能实现,使用更方便。 除了合并两者,本库还以网页为单位封装了常用功能,提供非常简便的操作和语句,在用于网页自动化操作时,减少考虑细节,专注功能实现,使用更方便。
一切从简,尽量提供简单直接的使用方法,对新手更友好。 一切从简,尽量提供简单直接的使用方法,对新手更友好。
# 🔆 3.0 版隆重推出 # 🔆 3.0 版即将推出
以前的版本是对 selenium 进行重新封装实现的。从 3.0 开始,作者另起炉灶,对底层进行了重新开发,摆脱对 selenium 的依赖,增强了功能,提升了运行效率。 以前的版本是对 selenium 进行重新封装实现的。从 3.0 开始,作者另起炉灶,对底层进行了重新开发,摆脱对 selenium 的依赖,增强了功能,提升了运行效率。
3.0 全新开发的页面对象是`WebPage`,支持 chromium 内核的浏览器(如 chrome 和 edge。除了保持之前的功能比依赖 selenium 的`MixPage`有以下优点: 3.0 全新开发的页面对象是`WebPage`,支持 chromium 内核的浏览器(如 chrome 和 edge。除了保持之前的功能比依赖 selenium 的`MixPage`有以下优点: