From 605fd6b30ea31b88a85cae14959b171fae5f0497 Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 20 Mar 2023 15:25:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dget=5Flocal=5Fstorage()?= =?UTF-8?q?=E5=92=8Cget=5Fsession=5Fstorage()=E8=8E=B7=E5=8F=96=E6=89=80?= =?UTF-8?q?=E6=9C=89=E6=95=B0=E6=8D=AE=E6=97=B6=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=9Bjs=E8=BF=94=E5=9B=9E=E5=AD=97=E5=85=B8=E6=97=B6?= =?UTF-8?q?=E8=83=BD=E6=AD=A3=E7=A1=AE=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_base.py | 48 ++++++++++++++++++++++++++------ DrissionPage/chromium_element.py | 11 ++++++-- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index 9c54946..e49a13f 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -631,16 +631,45 @@ class ChromiumBase(BasePage): :param item: 要获取的项,不设置则返回全部 :return: sessionStorage一个或所有项内容 """ - js = f'sessionStorage.getItem("{item}");' if item else 'sessionStorage;' - return self.run_js_loaded(js, as_expr=True) + # js = f'sessionStorage.getItem("{item}");' if item else 'sessionStorage;' + # 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): """获取localStorage信息,不设置item则获取全部 :param item: 要获取的项目,不设置则返回全部 :return: localStorage一个或所有项内容 """ - js = f'localStorage.getItem("{item}");' if item else 'localStorage;' - return self.run_js_loaded(js, as_expr=True) + if item: + 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, full_page=False, left_top=None, right_bottom=None): @@ -1173,10 +1202,11 @@ class Screencast(object): else: 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 = ''' - async function () { - stream = await navigator.mediaDevices.getDisplayMedia({video: true, audio: true}) + function () { + stream = navigator.mediaDevices.getDisplayMedia({video: true, audio: true}) mime = MediaRecorder.isTypeSupported("video/webm; codecs=vp9") ? "video/webm; codecs=vp9" : "video/webm" @@ -1186,7 +1216,9 @@ class Screencast(object): 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): """停止录屏 diff --git a/DrissionPage/chromium_element.py b/DrissionPage/chromium_element.py index 777d94c..4329d6b 100644 --- a/DrissionPage/chromium_element.py +++ b/DrissionPage/chromium_element.py @@ -1369,7 +1369,7 @@ def parse_js_result(page, ele, result): the_type = result['type'] if the_type == 'object': - sub_type = result['subtype'] + sub_type = result.get('subtype', None) if sub_type == 'null': return None @@ -1383,9 +1383,14 @@ def parse_js_result(page, ele, result): return make_chromium_ele(page, obj_id=result['objectId']) 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'] - 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: return result['value']