4.0.0b21run_js()等待环境;

This commit is contained in:
g1879 2023-12-14 23:25:11 +08:00
parent 0cc81621de
commit cd9439be5c
4 changed files with 19 additions and 8 deletions

View File

@ -13,4 +13,4 @@ from ._configs.chromium_options import ChromiumOptions
from ._configs.session_options import SessionOptions
__all__ = ['ChromiumPage', 'ChromiumOptions', 'SessionOptions', 'SessionPage', 'WebPage', '__version__']
__version__ = '4.0.0b20'
__version__ = '4.0.0b21'

View File

@ -1279,13 +1279,19 @@ def run_js(page_or_ele, script, as_expr=False, timeout=None, args=None):
:return: js执行结果
"""
if isinstance(page_or_ele, (ChromiumElement, ShadowRoot)):
is_page = False
page = page_or_ele.page
obj_id = page_or_ele._obj_id
is_page = False
else:
page = page_or_ele
obj_id = page_or_ele._root_id
is_page = True
page = page_or_ele
end_time = perf_counter() + 5
while perf_counter() < end_time:
obj_id = page_or_ele._root_id
if obj_id is not None:
break
else:
raise RuntimeError('js运行环境出错。')
if page.states.has_alert:
raise AlertExistsError

View File

@ -155,9 +155,12 @@ class ChromiumBase(BasePage):
return
timeout = timeout if timeout >= .5 else .5
self._is_reading = True
end_time = perf_counter() + timeout
try:
b_id = self.run_cdp('DOM.getDocument', _timeout=timeout)['root']['backendNodeId']
self._root_id = self.run_cdp('DOM.resolveNode', backendNodeId=b_id, _timeout=1)['object']['objectId']
timeout = end_time - perf_counter()
timeout = .5 if timeout < 0 else timeout
self._root_id = self.run_cdp('DOM.resolveNode', backendNodeId=b_id, _timeout=timeout)['object']['objectId']
r = self.run_cdp('Page.getFrameTree')
for i in findall(r"'id': '(.*?)'", str(r)):
@ -168,8 +171,10 @@ class ChromiumBase(BasePage):
except:
if self._debug:
print('获取文档失败')
return False
print('获取文档失败。')
print('请把报错信息和重现方法告知作者,感谢。\nhttps://gitee.com/g1879/DrissionPage/issues/new')
raise
# return False
finally:
self._is_loading = False

View File

@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
setup(
name="DrissionPage",
version="4.0.0b20",
version="4.0.0b21",
author="g1879",
author_email="g1879@qq.com",
description="Python based web automation tool. It can control the browser and send and receive data packets.",