修复get_local_storage()和get_session_storage()获取所有数据时的问题;js返回字典时能正确解析

This commit is contained in:
g1879 2023-03-20 15:25:38 +08:00
parent 6817e00a4f
commit 605fd6b30e
2 changed files with 48 additions and 11 deletions

View File

@ -631,16 +631,45 @@ class ChromiumBase(BasePage):
:param item: 要获取的项不设置则返回全部 :param item: 要获取的项不设置则返回全部
:return: sessionStorage一个或所有项内容 :return: sessionStorage一个或所有项内容
""" """
js = f'sessionStorage.getItem("{item}");' if item else 'sessionStorage;' # js = f'sessionStorage.getItem("{item}");' if item else 'sessionStorage;'
return self.run_js_loaded(js, as_expr=True) # return self.run_js_loaded(js, as_expr=True)
if item:
js = f'sessionStorage.getItem("{item}");'
return self.run_js_loaded(js, as_expr=True)
else:
js = '''
var dp_ls_len = sessionStorage.length; // 获取长度
var dp_ls_arr = new Array(); // 定义数据集
for(var i = 0; i < dp_ls_len; i++) {
var getKey = sessionStorage.key(i);
var getVal = sessionStorage.getItem(getKey);
dp_ls_arr[i] = {'key': getKey, 'val': getVal}
}
return dp_ls_arr;
'''
return {i['key']: i['val'] for i in self.run_js_loaded(js)}
def get_local_storage(self, item=None): def get_local_storage(self, item=None):
"""获取localStorage信息不设置item则获取全部 """获取localStorage信息不设置item则获取全部
:param item: 要获取的项目不设置则返回全部 :param item: 要获取的项目不设置则返回全部
:return: localStorage一个或所有项内容 :return: localStorage一个或所有项内容
""" """
js = f'localStorage.getItem("{item}");' if item else 'localStorage;' if item:
return self.run_js_loaded(js, as_expr=True) js = f'localStorage.getItem("{item}");'
return self.run_js_loaded(js, as_expr=True)
else:
js = '''
var dp_ls_len = localStorage.length; // 获取长度
var dp_ls_arr = new Array(); // 定义数据集
for(var i = 0; i < dp_ls_len; i++) {
var getKey = localStorage.key(i);
var getVal = localStorage.getItem(getKey);
dp_ls_arr[i] = {'key': getKey, 'val': getVal}
}
return dp_ls_arr;
'''
return {i['key']: i['val'] for i in self.run_js_loaded(js)}
def get_screenshot(self, path=None, as_bytes=None, as_base64=None, def get_screenshot(self, path=None, as_bytes=None, as_base64=None,
full_page=False, left_top=None, right_bottom=None): full_page=False, left_top=None, right_bottom=None):
@ -1173,10 +1202,11 @@ class Screencast(object):
else: else:
if self._cid is None: if self._cid is None:
self._cid = self._page.run_cdp('Page.createIsolatedWorld', frameId=self._page.tab_id)['executionContextId'] self._cid = self._page.run_cdp('Page.createIsolatedWorld', frameId=self._page.tab_id)[
'executionContextId']
js = ''' js = '''
async function () { function () {
stream = await navigator.mediaDevices.getDisplayMedia({video: true, audio: true}) stream = navigator.mediaDevices.getDisplayMedia({video: true, audio: true})
mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9") mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9")
? "video/webm; codecs=vp9" ? "video/webm; codecs=vp9"
: "video/webm" : "video/webm"
@ -1186,7 +1216,9 @@ class Screencast(object):
mediaRecorder.start() mediaRecorder.start()
} }
''' '''
self._page.run_cdp('Runtime.callFunctionOn', functionDeclaration=js, executionContextId=self._cid) print('请手动选择要录制的目标。')
r = self._page.run_cdp('Runtime.callFunctionOn', functionDeclaration=js, executionContextId=self._cid)
print(r)
def stop(self, video_name=None): def stop(self, video_name=None):
"""停止录屏 """停止录屏

View File

@ -1369,7 +1369,7 @@ def parse_js_result(page, ele, result):
the_type = result['type'] the_type = result['type']
if the_type == 'object': if the_type == 'object':
sub_type = result['subtype'] sub_type = result.get('subtype', None)
if sub_type == 'null': if sub_type == 'null':
return None return None
@ -1383,9 +1383,14 @@ def parse_js_result(page, ele, result):
return make_chromium_ele(page, obj_id=result['objectId']) return make_chromium_ele(page, obj_id=result['objectId'])
elif sub_type == 'array': elif sub_type == 'array':
r = page.run_cdp('Runtime.getProperties', objectId=result['result']['objectId'], r = page.run_cdp('Runtime.getProperties', objectId=result['objectId'],
ownProperties=True)['result'] ownProperties=True)['result']
return [parse_js_result(page, ele, result=i['value']) for i in r] return [parse_js_result(page, ele, result=i['value']) for i in r[:-1]]
elif 'objectId' in result: # dict
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}
else: else:
return result['value'] return result['value']