mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
修复页面用css查找元素时会找到文本的问题
This commit is contained in:
parent
e2393b1bf9
commit
a5b4951b5b
@ -54,7 +54,7 @@ class BaseElement(BaseParser):
|
|||||||
|
|
||||||
def __init__(self, owner=None):
|
def __init__(self, owner=None):
|
||||||
self.owner = owner
|
self.owner = owner
|
||||||
self.page = owner._page
|
self.page = owner._page if owner else None
|
||||||
self._type = 'BaseElement'
|
self._type = 'BaseElement'
|
||||||
|
|
||||||
# ----------------以下属性或方法由后代实现----------------
|
# ----------------以下属性或方法由后代实现----------------
|
||||||
|
@ -1271,12 +1271,13 @@ def find_by_css(ele, selector, index, timeout):
|
|||||||
return NoneElement(ele.owner) if index is not None else []
|
return NoneElement(ele.owner) if index is not None else []
|
||||||
|
|
||||||
|
|
||||||
def make_chromium_eles(page, _ids, index=1, is_obj_id=True):
|
def make_chromium_eles(page, _ids, index=1, is_obj_id=True, ele_only=False):
|
||||||
"""根据node id或object id生成相应元素对象
|
"""根据node id或object id生成相应元素对象
|
||||||
:param page: ChromiumPage对象
|
:param page: ChromiumPage对象
|
||||||
:param _ids: 元素的id列表
|
:param _ids: 元素的id列表
|
||||||
:param index: 获取第几个,为None返回全部
|
:param index: 获取第几个,为None返回全部
|
||||||
:param is_obj_id: 传入的id是obj id还是node id
|
:param is_obj_id: 传入的id是obj id还是node id
|
||||||
|
:param ele_only: 是否只返回ele,在页面查找元素时生效
|
||||||
:return: 浏览器元素对象或它们组成的列表,生成失败返回False
|
:return: 浏览器元素对象或它们组成的列表,生成失败返回False
|
||||||
"""
|
"""
|
||||||
if is_obj_id:
|
if is_obj_id:
|
||||||
@ -1287,16 +1288,27 @@ def make_chromium_eles(page, _ids, index=1, is_obj_id=True):
|
|||||||
_ids = (_ids,)
|
_ids = (_ids,)
|
||||||
|
|
||||||
if index is not None: # 获取一个
|
if index is not None: # 获取一个
|
||||||
obj_id = _ids[index - 1]
|
if ele_only:
|
||||||
return get_node_func(page, obj_id)
|
for obj_id in _ids:
|
||||||
|
tmp = get_node_func(page, obj_id, ele_only)
|
||||||
|
if tmp is False:
|
||||||
|
return False
|
||||||
|
elif tmp is not None:
|
||||||
|
return tmp
|
||||||
|
return False
|
||||||
|
|
||||||
|
else:
|
||||||
|
obj_id = _ids[index - 1]
|
||||||
|
return get_node_func(page, obj_id, ele_only)
|
||||||
|
|
||||||
else: # 获取全部
|
else: # 获取全部
|
||||||
nodes = []
|
nodes = []
|
||||||
for obj_id in _ids:
|
for obj_id in _ids:
|
||||||
tmp = get_node_func(page, obj_id)
|
tmp = get_node_func(page, obj_id, ele_only)
|
||||||
if tmp is False:
|
if tmp is False:
|
||||||
return False
|
return False
|
||||||
nodes.append(tmp)
|
elif tmp is not None:
|
||||||
|
nodes.append(tmp)
|
||||||
return nodes
|
return nodes
|
||||||
|
|
||||||
|
|
||||||
@ -1310,22 +1322,24 @@ def _get_node_info(page, id_type, _id):
|
|||||||
return node
|
return node
|
||||||
|
|
||||||
|
|
||||||
def _get_node_by_obj_id(page, obj_id):
|
def _get_node_by_obj_id(page, obj_id, ele_only):
|
||||||
|
"""根据obj id返回元素对象或文本,ele_only时如果是文本返回None,出错返回False"""
|
||||||
node = _get_node_info(page, 'objectId', obj_id)
|
node = _get_node_info(page, 'objectId', obj_id)
|
||||||
if node is False:
|
if node is False:
|
||||||
return False
|
return False
|
||||||
if node['node']['nodeName'] in ('#text', '#comment'):
|
if node['node']['nodeName'] in ('#text', '#comment'):
|
||||||
return node['node']['nodeValue']
|
return None if ele_only else node['node']['nodeValue']
|
||||||
else:
|
else:
|
||||||
return _make_ele(page, obj_id, node)
|
return _make_ele(page, obj_id, node)
|
||||||
|
|
||||||
|
|
||||||
def _get_node_by_node_id(page, node_id):
|
def _get_node_by_node_id(page, node_id, ele_only):
|
||||||
|
"""根据node id返回元素对象或文本,ele_only时如果是文本返回None,出错返回False"""
|
||||||
node = _get_node_info(page, 'nodeId', node_id)
|
node = _get_node_info(page, 'nodeId', node_id)
|
||||||
if node is False:
|
if node is False:
|
||||||
return False
|
return False
|
||||||
if node['node']['nodeName'] in ('#text', '#comment'):
|
if node['node']['nodeName'] in ('#text', '#comment'):
|
||||||
return node['node']['nodeValue']
|
return None if ele_only else node['node']['nodeValue']
|
||||||
else:
|
else:
|
||||||
obj_id = page.driver.run('DOM.resolveNode', nodeId=node_id)
|
obj_id = page.driver.run('DOM.resolveNode', nodeId=node_id)
|
||||||
if 'error' in obj_id:
|
if 'error' in obj_id:
|
||||||
|
@ -356,7 +356,8 @@ def find_by_css(ele: ChromiumElement,
|
|||||||
def make_chromium_eles(page: Union[ChromiumBase, ChromiumPage, WebPage, ChromiumTab, ChromiumFrame],
|
def make_chromium_eles(page: Union[ChromiumBase, ChromiumPage, WebPage, ChromiumTab, ChromiumFrame],
|
||||||
_ids: Union[tuple, list, str, int],
|
_ids: Union[tuple, list, str, int],
|
||||||
index: Optional[int] = 1,
|
index: Optional[int] = 1,
|
||||||
is_obj_id: bool = True
|
is_obj_id: bool = True,
|
||||||
|
ele_only: bool = False
|
||||||
) -> Union[ChromiumElement, ChromiumFrame, List[Union[ChromiumElement, ChromiumFrame]]]: ...
|
) -> Union[ChromiumElement, ChromiumFrame, List[Union[ChromiumElement, ChromiumFrame]]]: ...
|
||||||
|
|
||||||
|
|
||||||
|
@ -576,7 +576,8 @@ class ChromiumBase(BasePage):
|
|||||||
fromIndex=from_index, toIndex=end_index)
|
fromIndex=from_index, toIndex=end_index)
|
||||||
if __ERROR__ not in nIds:
|
if __ERROR__ not in nIds:
|
||||||
if nIds['nodeIds'][0] != 0:
|
if nIds['nodeIds'][0] != 0:
|
||||||
r = make_chromium_eles(self, _ids=nIds['nodeIds'], index=index_arg, is_obj_id=False)
|
r = make_chromium_eles(self, _ids=nIds['nodeIds'], index=index_arg,
|
||||||
|
is_obj_id=False, ele_only=True)
|
||||||
if r is not False:
|
if r is not False:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
@ -162,6 +162,8 @@ class Listener(object):
|
|||||||
:param gap: 每接收到多少个数据包返回一次数据
|
:param gap: 每接收到多少个数据包返回一次数据
|
||||||
:return: 用于在接收到监听目标时触发动作的可迭代对象
|
:return: 用于在接收到监听目标时触发动作的可迭代对象
|
||||||
"""
|
"""
|
||||||
|
if not self.listening:
|
||||||
|
raise RuntimeError('监听未启动或已暂停。')
|
||||||
caught = 0
|
caught = 0
|
||||||
end = perf_counter() + timeout if timeout else None
|
end = perf_counter() + timeout if timeout else None
|
||||||
while True:
|
while True:
|
||||||
@ -221,7 +223,7 @@ class Listener(object):
|
|||||||
:return: 返回是否等待成功
|
:return: 返回是否等待成功
|
||||||
"""
|
"""
|
||||||
if not self.listening:
|
if not self.listening:
|
||||||
raise RuntimeError('监听未启动,用listen.start()启动。')
|
raise RuntimeError('监听未启动或已暂停。')
|
||||||
if timeout is None:
|
if timeout is None:
|
||||||
while (not targets_only and self._running_requests > 0) or (targets_only and self._running_targets > 0):
|
while (not targets_only and self._running_requests > 0) or (targets_only and self._running_targets > 0):
|
||||||
sleep(.1)
|
sleep(.1)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user