3.0.23改进ChromiumFrame

This commit is contained in:
g1879 2022-12-27 18:18:07 +08:00
parent b1d9666fd8
commit 4a812d2793
3 changed files with 74 additions and 51 deletions

View File

@ -20,14 +20,17 @@ class ChromiumFrame(ChromiumBase):
if self._is_inner_frame():
self._is_diff_domain = False
super().__init__(page.address, page.tab_id, page.timeout)
self.doc_ele = ChromiumElement(self.page, backend_id=node['contentDocument']['backendNodeId'])
super().__init__(page.address, page.tab_id, page.timeout)
else:
self._is_diff_domain = True
super().__init__(page.address, self.frame_id, page.timeout)
obj_id = super().run_script('document;', as_expr=True)['objectId']
self.doc_ele = ChromiumElement(self, obj_id=obj_id)
self._tab_obj.Page.FrameAttached = self._onFrameAttached
self._tab_obj.Page.FrameDetached = self._onFrameDetached
def __call__(self, loc_or_str, timeout=None):
"""在内部查找元素 \n
ele2 = ele1('@id=ele_id') \n
@ -53,8 +56,8 @@ class ChromiumFrame(ChromiumBase):
while True:
try:
if self._is_inner_frame():
node = self.page.run_cdp('DOM.describeNode', backendNodeId=self.backend_id, not_change=True)[
'node']
node = self.page.run_cdp('DOM.describeNode',
backendNodeId=self.backend_id, not_change=True)['node']
self.doc_ele = ChromiumElement(self.page, backend_id=node['contentDocument']['backendNodeId'])
else:
b_id = self._tab_obj.DOM.getDocument()['root']['backendNodeId']
@ -88,6 +91,14 @@ class ChromiumFrame(ChromiumBase):
print('页面停止加载 FrameStoppedLoading')
self._get_new_document()
def _onFrameAttached(self, **kwargs):
if self._debug:
print(f'FrameAttached{[kwargs]}')
def _onFrameDetached(self, **kwargs):
if self._debug:
print(f'FrameDetached{[kwargs]}')
@property
def tab_id(self):
"""返回当前标签页id"""
@ -189,6 +200,14 @@ class ChromiumFrame(ChromiumBase):
"""返回frame的css selector绝对路径"""
return self.frame_ele.css_path
@property
def ready_state(self):
"""返回当前页面加载状态,'loading' 'interactive' 'complete'"""
if self._is_diff_domain:
return super().ready_state
else:
return self.doc_ele.run_script('return this.readyState;')
def refresh(self):
"""刷新frame页面"""
self.doc_ele.run_script('this.location.reload();')

View File

@ -5,7 +5,6 @@
"""
from typing import Union, Tuple, List, Any
from session_element import SessionElement
from .chromium_element import ChromiumElement
from .chromium_base import ChromiumBase
@ -32,7 +31,22 @@ class ChromiumFrame(ChromiumBase):
def __repr__(self) -> str: ...
def _is_inner_frame(self) -> bool: ...
def _get_new_document(self) -> None: ...
@property
def tab_id(self) -> str: ...
@property
def backend_id(self) -> str: ...
@property
def obj_id(self) -> str: ...
@property
def node_id(self) -> str: ...
@property
def frame_ele(self) -> ChromiumElement: ...
@property
def tag(self) -> str: ...
@ -43,15 +57,15 @@ class ChromiumFrame(ChromiumBase):
@property
def html(self) -> str: ...
@property
def inner_html(self) -> str: ...
@property
def title(self) -> str: ...
@property
def cookies(self) -> dict: ...
@property
def inner_html(self) -> str: ...
@property
def attrs(self) -> dict: ...
@ -64,18 +78,6 @@ class ChromiumFrame(ChromiumBase):
@property
def active_ele(self) -> ChromiumElement: ...
@property
def obj_id(self) -> str: ...
@property
def node_id(self) -> str: ...
@property
def backend_id(self) -> str: ...
@property
def frame_ele(self) -> ChromiumElement: ...
@property
def location(self) -> dict: ...
@ -88,32 +90,8 @@ class ChromiumFrame(ChromiumBase):
@property
def css_path(self) -> str: ...
def get(self,
url: str,
show_errmsg: bool = ...,
retry: int = ...,
interval: float = ...,
timeout: float = ...) -> Union[None, bool]: ...
def refresh(self) -> None: ...
def forward(self, steps: int = ...) -> None: ...
def back(self, steps: int = ...) -> None: ...
def ele(self,
loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, 'ChromiumFrame'],
timeout: float = ...): ...
def eles(self,
loc_or_str: Union[Tuple[str, str], str],
timeout: float = ...): ...
def s_ele(self, loc_or_str: Union[Tuple[str, str], str, ChromiumElement] = ...) -> Union[
SessionElement, str, None]: ...
def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = ...) -> List[Union[SessionElement, str]]: ...
def attr(self, attr: str) -> Union[str, None]: ...
def set_attr(self, attr: str, value: str) -> None: ...
@ -155,3 +133,17 @@ class ChromiumFrame(ChromiumBase):
def befores(self,
filter_loc: Union[tuple, str] = ...,
timeout: float = ...) -> List[Union[ChromiumElement, ChromiumFrame, str]]: ...
def _ele(self,
loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame],
timeout: float = ..., single: bool = ..., relative: bool = ...) \
-> Union[ChromiumElement, ChromiumFrame, None, List[Union[ChromiumElement, ChromiumFrame]]]: ...
def _d_connect(self,
to_url: str,
times: int = ...,
interval: float = ...,
show_errmsg: bool = ...,
timeout: float = ...) -> Union[bool, None]: ...
def _is_inner_frame(self) -> bool: ...

View File

@ -1,3 +1,15 @@
# v3.0.23
- 各种大小、位置信息从`dict`改为用`tuple`返回
- 改进`ChromiumFrame`
- 修复小窗时定位不准问题,修复 iframe 内元素无法获取 s_ele() 问题
- 增加`wait_loading`方法和参数
- 其它优化
# v3.0.22
- `change_mode()`增加`copy_cookies`参数
@ -53,10 +65,10 @@
# v2.7.1
- DriverPage
- 增加`get_session_storage()``get_local_storage()``set_session_storage()``set_local_storage()``clean_cache()`方法
- `run_cdp()``cmd_args`参数改为`**cmd_args`
- 增加`get_session_storage()``get_local_storage()``set_session_storage()``set_local_storage()``clean_cache()`方法
- `run_cdp()``cmd_args`参数改为`**cmd_args`
- 关闭 driver 时会主动关闭 chromedriver.exe 的进程
@ -70,9 +82,9 @@
# v2.6.0
- 新增`Listener`
- 可监听浏览器数据包
- 可异步监听
- 可实现每监听到若干数据包执行操作
- 可监听浏览器数据包
- 可异步监听
- 可实现每监听到若干数据包执行操作
- 放弃对selenium4.1以下的支持
- 解决使用新版浏览器时出现的一些问题