mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
完善ChromiumFrame,未完成
This commit is contained in:
parent
3a2d0c9f18
commit
4d41c3b993
@ -1,8 +1,6 @@
|
||||
# -*- coding:utf-8 -*-
|
||||
from json import loads
|
||||
from re import search
|
||||
from time import perf_counter, sleep
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from requests import Session
|
||||
|
||||
@ -12,7 +10,6 @@ from .common import get_loc
|
||||
from .config import cookies_to_tuple
|
||||
from .session_element import make_session_ele
|
||||
from .tab import Tab
|
||||
from .chromium_frame import ChromiumFrame
|
||||
|
||||
|
||||
class ChromiumBase(BasePage):
|
||||
@ -51,6 +48,7 @@ class ChromiumBase(BasePage):
|
||||
self._set_options()
|
||||
self._init_page(tab_id)
|
||||
self._get_document()
|
||||
self._first_run = False
|
||||
|
||||
def _init_page(self, tab_id=None):
|
||||
"""新建页面、页面刷新、切换标签页后要进行的cdp参数初始化
|
||||
@ -90,7 +88,8 @@ class ChromiumBase(BasePage):
|
||||
self._debug_recorder.add_data((perf_counter(), '信息', f'root_id:{root_id}'))
|
||||
self._root_id = self._tab_obj.DOM.resolveNode(nodeId=root_id)['object']['objectId']
|
||||
break
|
||||
except:
|
||||
|
||||
except Exception:
|
||||
if self._debug_recorder:
|
||||
self._debug_recorder.add_data((perf_counter(), 'err', '读取root_id出错'))
|
||||
|
||||
@ -235,10 +234,10 @@ class ChromiumBase(BasePage):
|
||||
|
||||
@property
|
||||
def size(self):
|
||||
"""返回页面总长宽,{'height': int, 'width': int}"""
|
||||
"""返回页面总长高,格式:(长, 高)"""
|
||||
w = self.run_script('document.body.scrollWidth;', as_expr=True)
|
||||
h = self.run_script('document.body.scrollHeight;', as_expr=True)
|
||||
return {'height': h, 'width': w}
|
||||
return w, h
|
||||
|
||||
@property
|
||||
def active_ele(self):
|
||||
@ -306,25 +305,13 @@ class ChromiumBase(BasePage):
|
||||
:param timeout: 连接超时时间
|
||||
:return: 目标url是否可用
|
||||
"""
|
||||
self._url_available = self._get(url, show_errmsg, retry, interval, timeout)
|
||||
return self._url_available
|
||||
|
||||
def _get(self, url: str, show_errmsg=False, retry=None, interval=None, timeout=None, frame_id=None):
|
||||
"""访问url \n
|
||||
:param url: 目标url
|
||||
:param show_errmsg: 是否显示和抛出异常
|
||||
:param retry: 重试次数
|
||||
:param interval: 重试间隔(秒)
|
||||
:param timeout: 连接超时时间
|
||||
:return: 目标url是否可用,返回None表示不确定
|
||||
"""
|
||||
retry, interval = self._before_connect(url, retry, interval)
|
||||
return self._d_connect(self._url,
|
||||
times=retry,
|
||||
interval=interval,
|
||||
show_errmsg=show_errmsg,
|
||||
timeout=timeout,
|
||||
frame_id=frame_id)
|
||||
self._url_available = self._d_connect(self._url,
|
||||
times=retry,
|
||||
interval=interval,
|
||||
show_errmsg=show_errmsg,
|
||||
timeout=timeout)
|
||||
return self._url_available
|
||||
|
||||
def get_cookies(self, as_dict=False):
|
||||
"""获取cookies信息 \n
|
||||
@ -381,10 +368,7 @@ class ChromiumBase(BasePage):
|
||||
:param loc_or_ele: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:return: SessionElement对象或属性、文本
|
||||
"""
|
||||
if isinstance(loc_or_ele, ChromiumElement):
|
||||
return make_session_ele(loc_or_ele)
|
||||
else:
|
||||
return make_session_ele(self, loc_or_ele)
|
||||
return make_session_ele(self, loc_or_ele)
|
||||
|
||||
def s_eles(self, loc_or_str=None):
|
||||
"""查找所有符合条件的元素以SessionElement列表形式返回 \n
|
||||
@ -402,7 +386,7 @@ class ChromiumBase(BasePage):
|
||||
"""
|
||||
if isinstance(loc_or_ele, (str, tuple)):
|
||||
loc = get_loc(loc_or_ele)[1]
|
||||
elif isinstance(loc_or_ele, ChromiumElement):
|
||||
elif isinstance(loc_or_ele, ChromiumElement) or str(type(loc_or_ele)).endswith(".ChromiumFrame'>"):
|
||||
return loc_or_ele
|
||||
else:
|
||||
raise ValueError('loc_or_str参数只能是tuple、str、ChromiumElement类型。')
|
||||
@ -426,12 +410,8 @@ class ChromiumBase(BasePage):
|
||||
for i in nodeIds['nodeIds']:
|
||||
ele = ChromiumElement(self, node_id=i)
|
||||
if ele.tag in ('iframe', 'frame'):
|
||||
src = ele.attr('src')
|
||||
if src:
|
||||
netloc1 = urlparse(src).netloc
|
||||
netloc2 = urlparse(self.url).netloc
|
||||
if netloc1 != netloc2:
|
||||
ele = ChromiumFrame(self, ele)
|
||||
from .chromium_frame import ChromiumFrame
|
||||
ele = ChromiumFrame(self, ele)
|
||||
eles.append(ele)
|
||||
|
||||
return eles[0] if single else eles
|
||||
@ -520,8 +500,14 @@ class ChromiumBase(BasePage):
|
||||
:param cmd_args: 参数
|
||||
:return: 执行的结果
|
||||
"""
|
||||
if cmd_args.get('not_change', None):
|
||||
driver = self._tab_obj
|
||||
cmd_args.pop('not_change')
|
||||
else:
|
||||
driver = self._driver
|
||||
|
||||
try:
|
||||
return self._driver.call_method(cmd, **cmd_args)
|
||||
return driver.call_method(cmd, **cmd_args)
|
||||
except Exception as e:
|
||||
if 'Could not find node with given id' in str(e):
|
||||
raise RuntimeError('该元素已不在当前页面中。')
|
||||
@ -585,7 +571,7 @@ class ChromiumBase(BasePage):
|
||||
if cookies:
|
||||
self._wait_driver.Network.clearBrowserCookies()
|
||||
|
||||
def _d_connect(self, to_url, times=0, interval=1, show_errmsg=False, timeout=None, frame_id=None):
|
||||
def _d_connect(self, to_url, times=0, interval=1, show_errmsg=False, timeout=None):
|
||||
"""尝试连接,重试若干次 \n
|
||||
:param to_url: 要访问的url
|
||||
:param times: 重试次数
|
||||
@ -599,10 +585,7 @@ class ChromiumBase(BasePage):
|
||||
|
||||
for _ in range(times + 1):
|
||||
err = None
|
||||
if frame_id:
|
||||
result = self._driver.Page.navigate(url=to_url, frameId=frame_id)
|
||||
else:
|
||||
result = self._driver.Page.navigate(url=to_url)
|
||||
result = self._driver.Page.navigate(url=to_url)
|
||||
|
||||
is_timeout = not self._wait_loading(timeout)
|
||||
while self.is_loading:
|
||||
@ -633,168 +616,6 @@ class ChromiumBase(BasePage):
|
||||
return True
|
||||
|
||||
|
||||
class Chromiu1mFrame(ChromiumBase):
|
||||
"""实现浏览器frame的类"""
|
||||
|
||||
def __init__(self, page, ele):
|
||||
"""初始化 \n
|
||||
:param page: 页面对象
|
||||
:param ele: 页面上的frame元素
|
||||
"""
|
||||
self.page = page
|
||||
self._inner_ele = ele
|
||||
frame_id = page.run_cdp('DOM.describeNode', nodeId=ele.node_id)['node'].get('frameId', None)
|
||||
super().__init__(page.address, frame_id, page.timeout)
|
||||
|
||||
def __repr__(self):
|
||||
attrs = self.attrs
|
||||
attrs = [f"{attr}='{attrs[attr]}'" for attr in attrs]
|
||||
return f'<ChromiumFrame {self.tag} {" ".join(attrs)}>'
|
||||
|
||||
@property
|
||||
def tag(self):
|
||||
"""返回元素tag"""
|
||||
return self._inner_ele.tag
|
||||
|
||||
@property
|
||||
def html(self):
|
||||
"""返回元素outerHTML文本"""
|
||||
tag = self.tag
|
||||
out_html = self.page.run_cdp('DOM.getOuterHTML', nodeId=self._inner_ele.node_id)['outerHTML']
|
||||
in_html = super().html
|
||||
sign = search(rf'<{tag}.*?>', out_html).group(0)
|
||||
return f'{sign}{in_html}</{tag}>'
|
||||
|
||||
@property
|
||||
def inner_html(self):
|
||||
"""返回元素innerHTML文本"""
|
||||
return super().html
|
||||
|
||||
@property
|
||||
def attrs(self):
|
||||
return self._inner_ele.attrs
|
||||
|
||||
@property
|
||||
def frame_size(self):
|
||||
"""返回frame元素大小"""
|
||||
return self._inner_ele.size
|
||||
|
||||
def _set_options(self):
|
||||
self.set_timeouts(page_load=self.page.timeouts.page_load,
|
||||
script=self.page.timeouts.script,
|
||||
implicit=self.page.timeouts.implicit if self.timeout is None else self.timeout)
|
||||
self._page_load_strategy = self.page.page_load_strategy
|
||||
|
||||
@property
|
||||
def obj_id(self):
|
||||
"""返回js中的object id"""
|
||||
return self._inner_ele.obj_id
|
||||
|
||||
@property
|
||||
def node_id(self):
|
||||
"""返回cdp中的node id"""
|
||||
return self._inner_ele.node_id
|
||||
|
||||
@property
|
||||
def location(self):
|
||||
"""返回frame元素左上角的绝对坐标"""
|
||||
return self._inner_ele.location
|
||||
|
||||
@property
|
||||
def is_displayed(self):
|
||||
"""返回frame元素是否显示"""
|
||||
return self._inner_ele.is_displayed
|
||||
|
||||
def attr(self, attr):
|
||||
"""返回frame元素attribute属性值 \n
|
||||
:param attr: 属性名
|
||||
:return: 属性值文本,没有该属性返回None
|
||||
"""
|
||||
return self._inner_ele.attr(attr)
|
||||
|
||||
def set_attr(self, attr, value):
|
||||
"""设置frame元素attribute属性 \n
|
||||
:param attr: 属性名
|
||||
:param value: 属性值
|
||||
:return: None
|
||||
"""
|
||||
self._inner_ele.set_attr(attr, value)
|
||||
|
||||
def remove_attr(self, attr):
|
||||
"""删除frame元素attribute属性 \n
|
||||
:param attr: 属性名
|
||||
:return: None
|
||||
"""
|
||||
self._inner_ele.remove_attr(attr)
|
||||
|
||||
def parent(self, level_or_loc=1):
|
||||
"""返回上面某一级父元素,可指定层数或用查询语法定位 \n
|
||||
:param level_or_loc: 第几级父元素,或定位符
|
||||
:return: 上级元素对象
|
||||
"""
|
||||
return self._inner_ele.parent(level_or_loc)
|
||||
|
||||
def prev(self, filter_loc='', index=1, timeout=0):
|
||||
"""返回前面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 \n
|
||||
:param filter_loc: 用于筛选元素的查询语法
|
||||
:param index: 前面第几个查询结果元素
|
||||
:param timeout: 查找元素的超时时间
|
||||
:return: 兄弟元素
|
||||
"""
|
||||
return self._inner_ele.prev(filter_loc, index, timeout)
|
||||
|
||||
def next(self, filter_loc='', index=1, timeout=0):
|
||||
"""返回后面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 \n
|
||||
:param filter_loc: 用于筛选元素的查询语法
|
||||
:param index: 后面第几个查询结果元素
|
||||
:param timeout: 查找元素的超时时间
|
||||
:return: 兄弟元素
|
||||
"""
|
||||
return self._inner_ele.next(filter_loc, index, timeout)
|
||||
|
||||
def before(self, filter_loc='', index=1, timeout=None):
|
||||
"""返回当前元素前面的一个元素,可指定筛选条件和第几个。查找范围不限兄弟元素,而是整个DOM文档 \n
|
||||
:param filter_loc: 用于筛选元素的查询语法
|
||||
:param index: 前面第几个查询结果元素
|
||||
:param timeout: 查找元素的超时时间
|
||||
:return: 本元素前面的某个元素或节点
|
||||
"""
|
||||
return self._inner_ele.before(filter_loc, index, timeout)
|
||||
|
||||
def after(self, filter_loc='', index=1, timeout=None):
|
||||
"""返回当前元素后面的一个元素,可指定筛选条件和第几个。查找范围不限兄弟元素,而是整个DOM文档 \n
|
||||
:param filter_loc: 用于筛选元素的查询语法
|
||||
:param index: 后面第几个查询结果元素
|
||||
:param timeout: 查找元素的超时时间
|
||||
:return: 本元素后面的某个元素或节点
|
||||
"""
|
||||
return self._inner_ele.after(filter_loc, index, timeout)
|
||||
|
||||
def prevs(self, filter_loc='', timeout=0):
|
||||
"""返回前面全部兄弟元素或节点组成的列表,可用查询语法筛选 \n
|
||||
:param filter_loc: 用于筛选元素的查询语法
|
||||
:param timeout: 查找元素的超时时间
|
||||
:return: 兄弟元素或节点文本组成的列表
|
||||
"""
|
||||
return self._inner_ele.prevs(filter_loc, timeout)
|
||||
|
||||
def nexts(self, filter_loc='', timeout=0):
|
||||
"""返回后面全部兄弟元素或节点组成的列表,可用查询语法筛选 \n
|
||||
:param filter_loc: 用于筛选元素的查询语法
|
||||
:param timeout: 查找元素的超时时间
|
||||
:return: 兄弟元素或节点文本组成的列表
|
||||
"""
|
||||
return self._inner_ele.nexts(filter_loc, timeout)
|
||||
|
||||
def befores(self, filter_loc='', timeout=None):
|
||||
"""返回当前元素后面符合条件的全部兄弟元素或节点组成的列表,可用查询语法筛选。查找范围不限兄弟元素,而是整个DOM文档 \n
|
||||
:param filter_loc: 用于筛选元素的查询语法
|
||||
:param timeout: 查找元素的超时时间
|
||||
:return: 本元素前面的元素或节点组成的列表
|
||||
"""
|
||||
return self._inner_ele.befores(filter_loc, timeout)
|
||||
|
||||
|
||||
class Timeout(object):
|
||||
"""用于保存d模式timeout信息的类"""
|
||||
|
||||
|
@ -6,8 +6,8 @@ from requests import Session
|
||||
from requests.cookies import RequestsCookieJar
|
||||
|
||||
from .base import BasePage
|
||||
from .chromium_element import ChromiumElement
|
||||
from .chromium_element import ChromiumElementWaiter, ChromeScroll
|
||||
from .chromium_element import ChromiumElement, ChromiumElementWaiter, ChromeScroll
|
||||
from .chromium_frame import ChromiumFrame
|
||||
from .config import DriverOptions
|
||||
from .session_element import SessionElement
|
||||
from .tab import Tab
|
||||
@ -56,8 +56,8 @@ class ChromiumBase(BasePage):
|
||||
|
||||
def _set_options(self) -> None: ...
|
||||
|
||||
def __call__(self, loc_or_str: Union[Tuple[str, str], str, 'ChromiumElement'],
|
||||
timeout: float = ...) -> Union['ChromiumElement', 'ChromiumFrame', None]: ...
|
||||
def __call__(self, loc_or_str: Union[Tuple[str, str], str, ChromiumElement],
|
||||
timeout: float = ...) -> Union[ChromiumElement, ChromiumFrame, None]: ...
|
||||
|
||||
@property
|
||||
def title(self) -> str: ...
|
||||
@ -90,7 +90,7 @@ class ChromiumBase(BasePage):
|
||||
def ready_state(self) -> str: ...
|
||||
|
||||
@property
|
||||
def size(self) -> dict: ...
|
||||
def size(self) -> tuple: ...
|
||||
|
||||
@property
|
||||
def active_ele(self) -> ChromiumElement: ...
|
||||
@ -99,10 +99,10 @@ class ChromiumBase(BasePage):
|
||||
def page_load_strategy(self) -> str: ...
|
||||
|
||||
@property
|
||||
def scroll(self) -> 'ChromeScroll': ...
|
||||
def scroll(self) -> ChromeScroll: ...
|
||||
|
||||
@property
|
||||
def set_page_load_strategy(self) -> pageLoadStrategy: ...
|
||||
def set_page_load_strategy(self) -> PageLoadStrategy: ...
|
||||
|
||||
def set_timeouts(self, implicit: float = ..., page_load: float = ..., script: float = ...) -> None: ...
|
||||
|
||||
@ -130,22 +130,22 @@ class ChromiumBase(BasePage):
|
||||
def set_cookies(self, cookies: Union[RequestsCookieJar, list, tuple, str, dict]) -> None: ...
|
||||
|
||||
def ele(self,
|
||||
loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, 'ChromiumFrame'],
|
||||
timeout: float = ...) -> Union[ChromiumElement, 'ChromiumFrame', None]: ...
|
||||
loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame],
|
||||
timeout: float = ...) -> Union[ChromiumElement, ChromiumFrame, None]: ...
|
||||
|
||||
def eles(self,
|
||||
loc_or_str: Union[Tuple[str, str], str],
|
||||
timeout: float = ...) -> List[Union[ChromiumElement, 'ChromiumFrame']]: ...
|
||||
timeout: float = ...) -> List[Union[ChromiumElement, ChromiumFrame]]: ...
|
||||
|
||||
def s_ele(self, loc_or_ele: Union[Tuple[str, str], str, ChromiumElement] = ...) \
|
||||
def s_ele(self, loc_or_ele: Union[Tuple[str, str], str] = ...) \
|
||||
-> Union[SessionElement, str, None]: ...
|
||||
|
||||
def s_eles(self, loc_or_str: Union[Tuple[str, str], str] = ...) -> List[Union[SessionElement, str]]: ...
|
||||
|
||||
def _ele(self,
|
||||
loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, 'ChromiumFrame'],
|
||||
loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, ChromiumFrame],
|
||||
timeout: float = ..., single: bool = ..., relative: bool = ...) \
|
||||
-> Union[ChromiumElement, 'ChromiumFrame', None, List[Union[ChromiumElement, 'ChromiumFrame']]]: ...
|
||||
-> Union[ChromiumElement, ChromiumFrame, None, List[Union[ChromiumElement, ChromiumFrame]]]: ...
|
||||
|
||||
def wait_ele(self,
|
||||
loc_or_ele: Union[str, tuple, ChromiumElement],
|
||||
@ -190,86 +190,6 @@ class ChromiumBase(BasePage):
|
||||
frame_id: str = ...) -> Union[bool, None]: ...
|
||||
|
||||
|
||||
class ChromiumFrame(ChromiumBase):
|
||||
"""实现浏览器frame的类"""
|
||||
|
||||
def __init__(self, page: ChromiumBase,
|
||||
ele: ChromiumElement):
|
||||
self._inner_ele: ChromiumElement = ...
|
||||
self.page: ChromiumBase = ...
|
||||
|
||||
def __repr__(self) -> str: ...
|
||||
|
||||
@property
|
||||
def tag(self) -> str: ...
|
||||
|
||||
@property
|
||||
def html(self) -> str: ...
|
||||
|
||||
@property
|
||||
def inner_html(self) -> str: ...
|
||||
|
||||
@property
|
||||
def attrs(self) -> dict: ...
|
||||
|
||||
@property
|
||||
def frame_size(self) -> dict: ...
|
||||
|
||||
def _set_options(self) -> None: ...
|
||||
|
||||
@property
|
||||
def obj_id(self) -> str: ...
|
||||
|
||||
@property
|
||||
def node_id(self) -> str: ...
|
||||
|
||||
@property
|
||||
def location(self) -> dict: ...
|
||||
|
||||
@property
|
||||
def is_displayed(self) -> bool: ...
|
||||
|
||||
def attr(self, attr: str) -> Union[str, None]: ...
|
||||
|
||||
def set_attr(self, attr: str, value: str) -> None: ...
|
||||
|
||||
def remove_attr(self, attr: str) -> None: ...
|
||||
|
||||
def parent(self, level_or_loc: Union[tuple, str, int] = ...) -> Union['ChromiumElement', None]: ...
|
||||
|
||||
def prev(self,
|
||||
filter_loc: Union[tuple, str] = ...,
|
||||
index: int = ...,
|
||||
timeout: float = ...) -> Union['ChromiumElement', str, None]: ...
|
||||
|
||||
def next(self,
|
||||
filter_loc: Union[tuple, str] = ...,
|
||||
index: int = ...,
|
||||
timeout: float = ...) -> Union['ChromiumElement', str, None]: ...
|
||||
|
||||
def before(self,
|
||||
filter_loc: Union[tuple, str] = ...,
|
||||
index: int = ...,
|
||||
timeout: float = ...) -> Union['ChromiumElement', str, None]: ...
|
||||
|
||||
def after(self,
|
||||
filter_loc: Union[tuple, str] = ...,
|
||||
index: int = ...,
|
||||
timeout: float = ...) -> Union['ChromiumElement', str, None]: ...
|
||||
|
||||
def prevs(self,
|
||||
filter_loc: Union[tuple, str] = ...,
|
||||
timeout: float = ...) -> List[Union['ChromiumElement', str]]: ...
|
||||
|
||||
def nexts(self,
|
||||
filter_loc: Union[tuple, str] = ...,
|
||||
timeout: float = ...) -> List[Union['ChromiumElement', str]]: ...
|
||||
|
||||
def befores(self,
|
||||
filter_loc: Union[tuple, str] = ...,
|
||||
timeout: float = ...) -> List[Union['ChromiumElement', str]]: ...
|
||||
|
||||
|
||||
class Timeout(object):
|
||||
"""用于保存d模式timeout信息的类"""
|
||||
|
||||
|
@ -2,8 +2,9 @@
|
||||
from re import search
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from .chromium_element import ChromiumElement
|
||||
from .chromium_base import ChromiumBase
|
||||
from .chromium_element import ChromiumElement
|
||||
from .session_element import make_session_ele
|
||||
|
||||
|
||||
class ChromiumFrame(object):
|
||||
@ -17,35 +18,48 @@ class ChromiumFrame(object):
|
||||
:param ele: frame容器元素对象
|
||||
"""
|
||||
self.page = page
|
||||
self._inner_ele = ele
|
||||
self._is_diff_domain = False
|
||||
self.frame_ele = ele
|
||||
self.frame_id = page.run_cdp('DOM.describeNode', nodeId=ele.node_id)['node'].get('frameId', None)
|
||||
|
||||
# 有src属性,且域名和主框架不一样,为异域frame
|
||||
src = ele.attr('src')
|
||||
if src and urlparse(src).netloc != urlparse(page.url).netloc:
|
||||
self._is_diff_domain = True
|
||||
self.inner_page = ChromiumBase(page.address, self.frame_id, page.timeout)
|
||||
self.inner_page.set_page_load_strategy(self.page.page_load_strategy)
|
||||
self.inner_page.timeouts = self.page.timeouts
|
||||
self.frame_page = ChromiumBase(page.address, self.frame_id)
|
||||
self.frame_page.set_page_load_strategy(self.page.page_load_strategy)
|
||||
self.frame_page.timeouts = self.page.timeouts
|
||||
self.frame_page._debug = True
|
||||
|
||||
else:
|
||||
self.frame_page = None
|
||||
self._is_diff_domain = False
|
||||
|
||||
def __call__(self, loc_or_str, timeout=None):
|
||||
"""在内部查找元素 \n
|
||||
例:ele2 = ele1('@id=ele_id') \n
|
||||
:param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param timeout: 超时时间
|
||||
:return: ChromiumElement对象或属性、文本
|
||||
"""
|
||||
return self.ele(loc_or_str, timeout)
|
||||
|
||||
def __repr__(self):
|
||||
attrs = self._inner_ele.attrs
|
||||
attrs = self.frame_ele.attrs
|
||||
attrs = [f"{attr}='{attrs[attr]}'" for attr in attrs]
|
||||
return f'<ChromiumFrame {self._inner_ele.tag} {" ".join(attrs)}>'
|
||||
return f'<ChromiumFrame {self.frame_ele.tag} {" ".join(attrs)}>'
|
||||
|
||||
@property
|
||||
def tag(self):
|
||||
"""返回元素tag"""
|
||||
return self._inner_ele.tag
|
||||
return self.frame_ele.tag
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
""""""
|
||||
if self._is_diff_domain:
|
||||
return self.inner_page.url
|
||||
return self.frame_page.url
|
||||
else:
|
||||
r = self.page.run_cdp('DOM.describeNode', nodeId=self._inner_ele.node_id)
|
||||
r = self.page.run_cdp('DOM.describeNode', nodeId=self.frame_ele.node_id)
|
||||
return r['node']['contentDocument']['documentURL']
|
||||
|
||||
@property
|
||||
@ -53,87 +67,115 @@ class ChromiumFrame(object):
|
||||
"""返回元素outerHTML文本"""
|
||||
if self._is_diff_domain:
|
||||
tag = self.tag
|
||||
out_html = self.page.run_cdp('DOM.getOuterHTML', nodeId=self._inner_ele.node_id)['outerHTML']
|
||||
in_html = self.inner_page.html
|
||||
out_html = self.page.run_cdp('DOM.getOuterHTML', nodeId=self.frame_ele.node_id)['outerHTML']
|
||||
in_html = self.frame_page.html
|
||||
sign = search(rf'<{tag}.*?>', out_html).group(0)
|
||||
return f'{sign}{in_html}</{tag}>'
|
||||
|
||||
else:
|
||||
return self._inner_ele.html
|
||||
return self.frame_ele.html
|
||||
|
||||
@property
|
||||
def title(self):
|
||||
d = self.inner_page if self._is_diff_domain else self._inner_ele
|
||||
"""返回frame内网页title"""
|
||||
d = self.frame_page if self._is_diff_domain else self.frame_ele
|
||||
ele = d.ele('xpath://title')
|
||||
return ele.text if ele else None
|
||||
|
||||
@property
|
||||
def cookies(self):
|
||||
return self.inner_page.cookies if self._is_diff_domain else self.page.cookies
|
||||
return self.frame_page.cookies if self._is_diff_domain else self.page.cookies
|
||||
|
||||
@property
|
||||
def inner_html(self):
|
||||
"""返回元素innerHTML文本"""
|
||||
return self.inner_page.html if self._is_diff_domain else self._inner_ele.inner_html
|
||||
return self.frame_page.html if self._is_diff_domain else self.frame_ele.inner_html
|
||||
|
||||
@property
|
||||
def attrs(self):
|
||||
return self._inner_ele.attrs
|
||||
"""返回frame元素所有attribute属性"""
|
||||
return self.frame_ele.attrs
|
||||
|
||||
@property
|
||||
def frame_size(self):
|
||||
"""返回frame内页面尺寸,格式:(长, 高)"""
|
||||
if self._is_diff_domain:
|
||||
return self.inner_page.size
|
||||
return self.frame_page.size
|
||||
else:
|
||||
h = self._inner_ele.run_script('return this.contentDocument.body.scrollHeight;')
|
||||
w = self._inner_ele.run_script('return this.contentDocument.body.scrollWidth;')
|
||||
return {'height': h, 'width': w}
|
||||
h = self.frame_ele.run_script('return this.contentDocument.body.scrollHeight;')
|
||||
w = self.frame_ele.run_script('return this.contentDocument.body.scrollWidth;')
|
||||
return w, h
|
||||
|
||||
@property
|
||||
def size(self):
|
||||
"""返回frame元素大小"""
|
||||
return self._inner_ele.size
|
||||
return self.frame_ele.size
|
||||
|
||||
@property
|
||||
def obj_id(self):
|
||||
"""返回js中的object id"""
|
||||
return self._inner_ele.obj_id
|
||||
"""返回frame元素的object id"""
|
||||
return self.frame_ele.obj_id
|
||||
|
||||
@property
|
||||
def node_id(self):
|
||||
"""返回cdp中的node id"""
|
||||
return self._inner_ele.node_id
|
||||
return self.frame_ele.node_id
|
||||
|
||||
@property
|
||||
def location(self):
|
||||
"""返回frame元素左上角的绝对坐标"""
|
||||
return self._inner_ele.location
|
||||
return self.frame_ele.location
|
||||
|
||||
@property
|
||||
def is_displayed(self):
|
||||
"""返回frame元素是否显示"""
|
||||
return self._inner_ele.is_displayed
|
||||
return self.frame_ele.is_displayed
|
||||
|
||||
def get(self, url):
|
||||
self.page._get(url, False, None, None, None, self.frame_id)
|
||||
def get(self, url, show_errmsg=False, retry=None, interval=None, timeout=None):
|
||||
"""访问目标网页 \n
|
||||
:param url: 目标url
|
||||
:param show_errmsg: 是否显示和抛出异常
|
||||
:param retry: 重试次数
|
||||
:param interval: 重试间隔(秒)
|
||||
:param timeout: 连接超时时间
|
||||
:return: 目标url是否可用
|
||||
"""
|
||||
if self._is_diff_domain:
|
||||
return self.page.get(url, show_errmsg, retry, interval, timeout)
|
||||
else:
|
||||
# todo:
|
||||
pass
|
||||
|
||||
def ele(self, loc_or_ele, timeout=None):
|
||||
d = self.inner_page if self._is_diff_domain else self._inner_ele
|
||||
return d.ele(loc_or_ele, timeout)
|
||||
def refresh(self):
|
||||
"document.getElementById('some_frame_id').contentWindow.location.reload();"
|
||||
|
||||
def eles(self, loc_or_ele, timeout=None):
|
||||
d = self.inner_page if self._is_diff_domain else self._inner_ele
|
||||
return d.eles(loc_or_ele, timeout)
|
||||
def ele(self, loc_or_str, timeout=None):
|
||||
"""在frame内查找单个元素
|
||||
:param loc_or_str: 定位符或元素对象
|
||||
:param timeout: 查找超时时间
|
||||
:return: ChromiumElement对象
|
||||
"""
|
||||
d = self.frame_page if self._is_diff_domain else self.frame_ele
|
||||
return d.ele(loc_or_str, timeout)
|
||||
|
||||
def s_ele(self, loc_or_ele=None):
|
||||
def eles(self, loc_or_str, timeout=None):
|
||||
"""获取所有符合条件的元素对象 \n
|
||||
:param loc_or_str: 定位符或元素对象
|
||||
:param timeout: 查找超时时间
|
||||
:return: ChromiumElement对象组成的列表
|
||||
"""
|
||||
d = self.frame_page if self._is_diff_domain else self.frame_ele
|
||||
return d.eles(loc_or_str, timeout)
|
||||
|
||||
def s_ele(self, loc_or_str=None):
|
||||
"""查找第一个符合条件的元素以SessionElement形式返回,处理复杂页面时效率很高 \n
|
||||
:param loc_or_ele: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串
|
||||
:return: SessionElement对象或属性、文本
|
||||
"""
|
||||
if isinstance(loc_or_ele, ChromiumElement):
|
||||
return make_session_ele(loc_or_ele)
|
||||
if isinstance(loc_or_str, ChromiumElement):
|
||||
return make_session_ele(loc_or_str)
|
||||
else:
|
||||
return make_session_ele(self, loc_or_ele)
|
||||
return make_session_ele(self, loc_or_str)
|
||||
|
||||
def s_eles(self, loc_or_str=None):
|
||||
"""查找所有符合条件的元素以SessionElement列表形式返回 \n
|
||||
@ -147,7 +189,7 @@ class ChromiumFrame(object):
|
||||
:param attr: 属性名
|
||||
:return: 属性值文本,没有该属性返回None
|
||||
"""
|
||||
return self._inner_ele.attr(attr)
|
||||
return self.frame_ele.attr(attr)
|
||||
|
||||
def set_attr(self, attr, value):
|
||||
"""设置frame元素attribute属性 \n
|
||||
@ -155,21 +197,21 @@ class ChromiumFrame(object):
|
||||
:param value: 属性值
|
||||
:return: None
|
||||
"""
|
||||
self._inner_ele.set_attr(attr, value)
|
||||
self.frame_ele.set_attr(attr, value)
|
||||
|
||||
def remove_attr(self, attr):
|
||||
"""删除frame元素attribute属性 \n
|
||||
:param attr: 属性名
|
||||
:return: None
|
||||
"""
|
||||
self._inner_ele.remove_attr(attr)
|
||||
self.frame_ele.remove_attr(attr)
|
||||
|
||||
def parent(self, level_or_loc=1):
|
||||
"""返回上面某一级父元素,可指定层数或用查询语法定位 \n
|
||||
:param level_or_loc: 第几级父元素,或定位符
|
||||
:return: 上级元素对象
|
||||
"""
|
||||
return self._inner_ele.parent(level_or_loc)
|
||||
return self.frame_ele.parent(level_or_loc)
|
||||
|
||||
def prev(self, filter_loc='', index=1, timeout=0):
|
||||
"""返回前面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 \n
|
||||
@ -178,7 +220,7 @@ class ChromiumFrame(object):
|
||||
:param timeout: 查找元素的超时时间
|
||||
:return: 兄弟元素
|
||||
"""
|
||||
return self._inner_ele.prev(filter_loc, index, timeout)
|
||||
return self.frame_ele.prev(filter_loc, index, timeout)
|
||||
|
||||
def next(self, filter_loc='', index=1, timeout=0):
|
||||
"""返回后面的一个兄弟元素,可用查询语法筛选,可指定返回筛选结果的第几个 \n
|
||||
@ -187,7 +229,7 @@ class ChromiumFrame(object):
|
||||
:param timeout: 查找元素的超时时间
|
||||
:return: 兄弟元素
|
||||
"""
|
||||
return self._inner_ele.next(filter_loc, index, timeout)
|
||||
return self.frame_ele.next(filter_loc, index, timeout)
|
||||
|
||||
def before(self, filter_loc='', index=1, timeout=None):
|
||||
"""返回当前元素前面的一个元素,可指定筛选条件和第几个。查找范围不限兄弟元素,而是整个DOM文档 \n
|
||||
@ -196,7 +238,7 @@ class ChromiumFrame(object):
|
||||
:param timeout: 查找元素的超时时间
|
||||
:return: 本元素前面的某个元素或节点
|
||||
"""
|
||||
return self._inner_ele.before(filter_loc, index, timeout)
|
||||
return self.frame_ele.before(filter_loc, index, timeout)
|
||||
|
||||
def after(self, filter_loc='', index=1, timeout=None):
|
||||
"""返回当前元素后面的一个元素,可指定筛选条件和第几个。查找范围不限兄弟元素,而是整个DOM文档 \n
|
||||
@ -205,7 +247,7 @@ class ChromiumFrame(object):
|
||||
:param timeout: 查找元素的超时时间
|
||||
:return: 本元素后面的某个元素或节点
|
||||
"""
|
||||
return self._inner_ele.after(filter_loc, index, timeout)
|
||||
return self.frame_ele.after(filter_loc, index, timeout)
|
||||
|
||||
def prevs(self, filter_loc='', timeout=0):
|
||||
"""返回前面全部兄弟元素或节点组成的列表,可用查询语法筛选 \n
|
||||
@ -213,7 +255,7 @@ class ChromiumFrame(object):
|
||||
:param timeout: 查找元素的超时时间
|
||||
:return: 兄弟元素或节点文本组成的列表
|
||||
"""
|
||||
return self._inner_ele.prevs(filter_loc, timeout)
|
||||
return self.frame_ele.prevs(filter_loc, timeout)
|
||||
|
||||
def nexts(self, filter_loc='', timeout=0):
|
||||
"""返回后面全部兄弟元素或节点组成的列表,可用查询语法筛选 \n
|
||||
@ -221,7 +263,7 @@ class ChromiumFrame(object):
|
||||
:param timeout: 查找元素的超时时间
|
||||
:return: 兄弟元素或节点文本组成的列表
|
||||
"""
|
||||
return self._inner_ele.nexts(filter_loc, timeout)
|
||||
return self.frame_ele.nexts(filter_loc, timeout)
|
||||
|
||||
def befores(self, filter_loc='', timeout=None):
|
||||
"""返回当前元素后面符合条件的全部兄弟元素或节点组成的列表,可用查询语法筛选。查找范围不限兄弟元素,而是整个DOM文档 \n
|
||||
@ -229,4 +271,4 @@ class ChromiumFrame(object):
|
||||
:param timeout: 查找元素的超时时间
|
||||
:return: 本元素前面的元素或节点组成的列表
|
||||
"""
|
||||
return self._inner_ele.befores(filter_loc, timeout)
|
||||
return self.frame_ele.befores(filter_loc, timeout)
|
||||
|
@ -12,11 +12,16 @@ class ChromiumFrame(object):
|
||||
同域和异域的frame处理方式不一样,同域的当作元素看待,异域的当作页面看待。"""
|
||||
|
||||
def __init__(self, page: ChromiumBase, ele: ChromiumElement):
|
||||
self.inner_page: ChromiumBase = ...
|
||||
self._inner_ele: ChromiumElement = ...
|
||||
self.frame_ele: ChromiumElement = ...
|
||||
self.frame_page: ChromiumBase = ...
|
||||
self.page: ChromiumBase = ...
|
||||
self.frame_id: str = ...
|
||||
self._is_diff_domain: bool = ...
|
||||
self.is_loading: bool = ...
|
||||
|
||||
def __call__(self,
|
||||
loc_or_str: Union[Tuple[str, str], str],
|
||||
timeout: float = ...) -> Union[ChromiumElement, ChromiumFrame, str, None]: ...
|
||||
|
||||
def __repr__(self) -> str: ...
|
||||
|
||||
@ -42,10 +47,10 @@ class ChromiumFrame(object):
|
||||
def attrs(self) -> dict: ...
|
||||
|
||||
@property
|
||||
def frame_size(self) -> dict: ...
|
||||
def frame_size(self) -> tuple: ...
|
||||
|
||||
@property
|
||||
def size(self) -> dict: ...
|
||||
def size(self) -> tuple: ...
|
||||
|
||||
@property
|
||||
def obj_id(self) -> str: ...
|
||||
@ -59,17 +64,24 @@ class ChromiumFrame(object):
|
||||
@property
|
||||
def is_displayed(self) -> bool: ...
|
||||
|
||||
def get(self, url) -> bool: ...
|
||||
def get(self,
|
||||
url: str,
|
||||
show_errmsg: bool = ...,
|
||||
retry: int = ...,
|
||||
interval: float = ...,
|
||||
timeout: float = ...) -> Union[None, bool]: ...
|
||||
|
||||
def refresh(self) -> None: ...
|
||||
|
||||
def ele(self,
|
||||
loc_or_ele: Union[Tuple[str, str], str, ChromiumElement, 'ChromiumFrame'],
|
||||
loc_or_str: Union[Tuple[str, str], str, ChromiumElement, 'ChromiumFrame'],
|
||||
timeout: float = ...): ...
|
||||
|
||||
def eles(self,
|
||||
loc_or_ele: Union[Tuple[str, str], str],
|
||||
loc_or_str: Union[Tuple[str, str], str],
|
||||
timeout: float = ...): ...
|
||||
|
||||
def s_ele(self, loc_or_ele: Union[Tuple[str, str], str, ChromiumElement] = ...) -> Union[
|
||||
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]]: ...
|
||||
|
Loading…
x
Reference in New Issue
Block a user