From d2068e49b5b7d92965853748de3a412be0f0245a Mon Sep 17 00:00:00 2001 From: g1879 Date: Thu, 11 Jan 2024 23:44:56 +0800 Subject: [PATCH] =?UTF-8?q?4.0.0b37=E5=AE=8C=E5=96=84js=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_elements/chromium_element.py | 10 ++++++++-- DrissionPage/_pages/chromium_page.py | 8 ++++---- DrissionPage/_units/waiter.pyi | 2 ++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index b23c271..570684c 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -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 diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index faac5d2..b925c9c 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -264,7 +264,7 @@ class ChromiumPage(ChromiumBase): def _handle_options(addr_or_opts): """设置浏览器启动属性 :param addr_or_opts: 'ip:port'、ChromiumOptions、Driver - :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] diff --git a/DrissionPage/_units/waiter.pyi b/DrissionPage/_units/waiter.pyi index dcfae8f..a04dd9a 100644 --- a/DrissionPage/_units/waiter.pyi +++ b/DrissionPage/_units/waiter.pyi @@ -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]: ...