4.0.0b37完善js获取结果

This commit is contained in:
g1879 2024-01-11 23:44:56 +08:00
parent b19e62bac5
commit d2068e49b5
3 changed files with 14 additions and 6 deletions

View File

@ -5,6 +5,7 @@
@Copyright: (c) 2024 by g1879, Inc. All Rights Reserved.
@License : BSD 3-Clause.
"""
from json import loads
from os.path import basename, sep
from pathlib import Path
from re import search
@ -1458,7 +1459,6 @@ def parse_js_result(page, ele, result):
return result['unserializableValue']
the_type = result['type']
if the_type == 'object':
sub_type = result.get('subtype', None)
if sub_type == 'null':
@ -1484,8 +1484,14 @@ def parse_js_result(page, ele, result):
r = page.run_cdp('Runtime.getProperties', objectId=result['objectId'], ownProperties=True)['result']
return {i['name']: parse_js_result(page, ele, result=i['value']) for i in r}
elif 'objectId' in result:
js = 'function(){return JSON.stringify(this);}'
r = page.run_cdp('Runtime.callFunctionOn', functionDeclaration=js, objectId=result['objectId'],
returnByValue=False, awaitPromise=True, userGesture=True, _ignore=AlertExistsError)
return loads(parse_js_result(page, ele, r['result']))
else:
return result['value']
return result.get('value', result)
elif the_type == 'undefined':
return None

View File

@ -264,7 +264,7 @@ class ChromiumPage(ChromiumBase):
def _handle_options(addr_or_opts):
"""设置浏览器启动属性
:param addr_or_opts: 'ip:port'ChromiumOptionsDriver
:return: 返回浏览器地址
:return: 返回ChromiumOptions对象
"""
if not addr_or_opts:
_chromium_options = ChromiumOptions(addr_or_opts)
@ -291,11 +291,11 @@ def _handle_options(addr_or_opts):
return _chromium_options
def _run_browser(_chromium_options):
def _run_browser(chromium_options):
"""连接浏览器"""
is_exist = connect_browser(_chromium_options)
is_exist = connect_browser(chromium_options)
try:
ws = get(f'http://{_chromium_options.address}/json/version', headers={'Connection': 'close'})
ws = get(f'http://{chromium_options.address}/json/version', headers={'Connection': 'close'})
if not ws:
raise BrowserConnectError('\n浏览器连接失败如使用全局代理须设置不代理127.0.0.1地址。')
browser_id = ws.json()['webSocketDebuggerUrl'].split('/')[-1]

View File

@ -42,6 +42,8 @@ class BaseWaiter(object):
def load_start(self, timeout: float = None, raise_err: bool = None) -> bool: ...
def doc_loaded(self, timeout: float = None, raise_err: bool = None) -> bool: ...
def upload_paths_inputted(self) -> bool: ...
def download_begin(self, timeout: float = None, cancel_it: bool = False) -> Union[DownloadMission, bool]: ...