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: ...