From db6c03a062d6051be5443e17a58c6f809adf7d0f Mon Sep 17 00:00:00 2001 From: g1879 Date: Tue, 21 Mar 2023 11:43:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dget=5Fsrc()=E6=9F=90=E6=83=85?= =?UTF-8?q?=E5=86=B5=E4=B8=8Btimeout=E5=A4=B1=E6=95=88=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=9Bnew=5Ftab()=E7=8E=B0=E5=9C=A8=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E6=96=B0=E6=A0=87=E7=AD=BE=E9=A1=B5id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_element.py | 20 +++++++++----------- DrissionPage/chromium_page.py | 10 ++++++---- DrissionPage/chromium_page.pyi | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/DrissionPage/chromium_element.py b/DrissionPage/chromium_element.py index ded40fb..50e2981 100644 --- a/DrissionPage/chromium_element.py +++ b/DrissionPage/chromium_element.py @@ -406,12 +406,7 @@ class ChromiumElement(DrissionElement): :param timeout: 等待资源加载的超时时间 :return: 资源内容 """ - src = self.prop('currentSrc') - if not src: - return None - timeout = self.page.timeout if timeout is None else timeout - if self.tag == 'img': # 等待图片加载完成 js = ('return this.complete && typeof this.naturalWidth != "undefined" ' '&& this.naturalWidth > 0 && typeof this.naturalHeight != "undefined" ' @@ -420,19 +415,22 @@ class ChromiumElement(DrissionElement): while not self.run_js(js) and perf_counter() < end_time: sleep(.1) - node = self.page.run_cdp('DOM.describeNode', backendNodeId=self._backend_id)['node'] - frame = node.get('frameId', None) - frame = frame or self.page.tab_id - result = None end_time = perf_counter() + timeout while perf_counter() < end_time: + src = self.prop('currentSrc') + if not src: + continue + + node = self.page.run_cdp('DOM.describeNode', backendNodeId=self._backend_id)['node'] + frame = node.get('frameId', None) + frame = frame or self.page.tab_id + try: result = self.page.run_cdp('Page.getResourceContent', frameId=frame, url=src) break except CallMethodError: - pass - sleep(.1) + sleep(.1) if not result: return None diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index 7d83300..2d0b939 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -193,12 +193,12 @@ class ChromiumPage(ChromiumBase): """新建一个标签页,该标签页在最后面 :param url: 新标签页跳转到的网址 :param switch_to: 新建标签页后是否把焦点移过去 - :return: None + :return: 新标签页的id """ if switch_to: begin_tabs = set(self.tabs) len_tabs = len(begin_tabs) - self.run_cdp('Target.createTarget', url='') + tid = self.run_cdp('Target.createTarget', url='')['targetId'] tabs = self.tabs while len(tabs) == len_tabs: @@ -211,10 +211,12 @@ class ChromiumPage(ChromiumBase): self.get(url) elif url: - self.run_cdp('Target.createTarget', url=url) + tid = self.run_cdp('Target.createTarget', url=url)['targetId'] else: - self.run_cdp('Target.createTarget', url='') + tid = self.run_cdp('Target.createTarget', url='')['targetId'] + + return tid def to_main_tab(self): """跳转到主标签页""" diff --git a/DrissionPage/chromium_page.pyi b/DrissionPage/chromium_page.pyi index 338fbaa..c63ce2e 100644 --- a/DrissionPage/chromium_page.pyi +++ b/DrissionPage/chromium_page.pyi @@ -81,7 +81,7 @@ class ChromiumPage(ChromiumBase): def get_tab(self, tab_id: str = None) -> ChromiumTab: ... - def new_tab(self, url: str = None, switch_to: bool = True) -> None: ... + def new_tab(self, url: str = None, switch_to: bool = True) -> str: ... def to_main_tab(self) -> None: ...