修复页面用css查找元素时会找到文本的问题

This commit is contained in:
g1879 2024-02-26 22:48:57 +08:00
parent e2393b1bf9
commit a5b4951b5b
5 changed files with 31 additions and 13 deletions

View File

@ -54,7 +54,7 @@ class BaseElement(BaseParser):
def __init__(self, owner=None):
self.owner = owner
self.page = owner._page
self.page = owner._page if owner else None
self._type = 'BaseElement'
# ----------------以下属性或方法由后代实现----------------

View File

@ -1271,12 +1271,13 @@ def find_by_css(ele, selector, index, timeout):
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生成相应元素对象
:param page: ChromiumPage对象
:param _ids: 元素的id列表
:param index: 获取第几个为None返回全部
:param is_obj_id: 传入的id是obj id还是node id
:param ele_only: 是否只返回ele在页面查找元素时生效
:return: 浏览器元素对象或它们组成的列表生成失败返回False
"""
if is_obj_id:
@ -1287,15 +1288,26 @@ def make_chromium_eles(page, _ids, index=1, is_obj_id=True):
_ids = (_ids,)
if index is not None: # 获取一个
if ele_only:
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)
return get_node_func(page, obj_id, ele_only)
else: # 获取全部
nodes = []
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:
return False
elif tmp is not None:
nodes.append(tmp)
return nodes
@ -1310,22 +1322,24 @@ def _get_node_info(page, id_type, _id):
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)
if node is False:
return False
if node['node']['nodeName'] in ('#text', '#comment'):
return node['node']['nodeValue']
return None if ele_only else node['node']['nodeValue']
else:
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)
if node is False:
return False
if node['node']['nodeName'] in ('#text', '#comment'):
return node['node']['nodeValue']
return None if ele_only else node['node']['nodeValue']
else:
obj_id = page.driver.run('DOM.resolveNode', nodeId=node_id)
if 'error' in obj_id:

View File

@ -356,7 +356,8 @@ def find_by_css(ele: ChromiumElement,
def make_chromium_eles(page: Union[ChromiumBase, ChromiumPage, WebPage, ChromiumTab, ChromiumFrame],
_ids: Union[tuple, list, str, int],
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]]]: ...

View File

@ -576,7 +576,8 @@ class ChromiumBase(BasePage):
fromIndex=from_index, toIndex=end_index)
if __ERROR__ not in nIds:
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:
break

View File

@ -162,6 +162,8 @@ class Listener(object):
:param gap: 每接收到多少个数据包返回一次数据
:return: 用于在接收到监听目标时触发动作的可迭代对象
"""
if not self.listening:
raise RuntimeError('监听未启动或已暂停。')
caught = 0
end = perf_counter() + timeout if timeout else None
while True:
@ -221,7 +223,7 @@ class Listener(object):
:return: 返回是否等待成功
"""
if not self.listening:
raise RuntimeError('监听未启动用listen.start()启动')
raise RuntimeError('监听未启动或已暂停')
if timeout is None:
while (not targets_only and self._running_requests > 0) or (targets_only and self._running_targets > 0):
sleep(.1)