修复get_src()某情况下timeout失效问题;new_tab()现在返回新标签页id

This commit is contained in:
g1879 2023-03-21 11:43:54 +08:00
parent f9afa9f677
commit db6c03a062
3 changed files with 16 additions and 16 deletions

View File

@ -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

View File

@ -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):
"""跳转到主标签页"""

View File

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