handle_alert()等待不到时返回False;while均设置超时

This commit is contained in:
g1879 2023-03-31 18:26:33 +08:00
parent fbeb9056c4
commit a704c12f38
4 changed files with 35 additions and 15 deletions

View File

@ -124,7 +124,8 @@ class ChromiumBase(BasePage):
except TabClosedError:
return
while True:
end_time = perf_counter() + 10
while perf_counter() < end_time:
try:
b_id = self.run_cdp('DOM.getDocument')['root']['backendNodeId']
self._root_id = self.run_cdp('DOM.resolveNode', backendNodeId=b_id)['object']['objectId']
@ -133,8 +134,15 @@ class ChromiumBase(BasePage):
break
except Exception:
if self._debug_recorder:
self._debug_recorder.add_data((perf_counter(), 'err', '读取root_id出错'))
if self._debug:
print('重试获取document')
if self._debug_recorder:
self._debug_recorder.add_data((perf_counter(), 'err', '读取root_id出错'))
sleep(.1)
else:
raise RuntimeError('获取document失败。')
if self._debug:
print('获取document结束')
@ -606,7 +614,7 @@ class ChromiumBase(BasePage):
self._debug_recorder.add_data((perf_counter(), '操作', '停止页面加载'))
self.run_cdp('Page.stopLoading')
while self.ready_state != 'complete':
while self.ready_state not in ('complete', None):
sleep(.1)
def remove_ele(self, loc_or_ele):
@ -770,7 +778,7 @@ class ChromiumBase(BasePage):
if t < times:
sleep(interval)
while self.ready_state != 'complete':
while self.ready_state not in ('complete', None):
sleep(.1)
if self._debug or show_errmsg:
print(f'重试 {to_url}')

View File

@ -109,7 +109,8 @@ class ChromiumFrame(ChromiumBase):
if self._debug:
print('---获取document')
while True:
end_time = perf_counter() + 10
while perf_counter() < end_time:
try:
if self._is_diff_domain is False:
node = self.page.run_cdp('DOM.describeNode', backendNodeId=self.ids.backend_id)['node']
@ -124,6 +125,9 @@ class ChromiumFrame(ChromiumBase):
except Exception:
sleep(.1)
else:
raise RuntimeError('获取document失败。')
if self._debug:
print('---获取document结束')
@ -272,13 +276,21 @@ class ChromiumFrame(ChromiumBase):
return 'complete'
else:
while True:
end_time = perf_counter() + 10
while perf_counter() < end_time:
try:
return self.doc_ele.run_js('return this.readyState;')
except ContextLossError:
node = self.run_cdp('DOM.describeNode', backendNodeId=self.frame_ele.ids.backend_id)['node']
doc = ChromiumElement(self.page, backend_id=node['contentDocument']['backendNodeId'])
return doc.run_js('return this.readyState;')
try:
node = self.run_cdp('DOM.describeNode', backendNodeId=self.frame_ele.ids.backend_id)['node']
doc = ChromiumElement(self.page, backend_id=node['contentDocument']['backendNodeId'])
return doc.run_js('return this.readyState;')
except:
pass
sleep(.1)
raise RuntimeError('获取document失败。')
@property
def scroll(self):
@ -558,7 +570,7 @@ class ChromiumFrame(ChromiumBase):
if t < times:
sleep(interval)
while self.ready_state != 'complete':
while self.ready_state not in ('complete', None):
sleep(.1)
if self._debug:
print('重试')

View File

@ -261,7 +261,7 @@ class ChromiumPage(ChromiumBase):
self.driver.stop()
self._driver_init(tab_id)
if read_doc and self.ready_state == 'complete':
if read_doc and self.ready_state in ('complete', None):
self._get_document()
def close_tabs(self, tabs_or_ids=None, others=False):
@ -315,7 +315,7 @@ class ChromiumPage(ChromiumBase):
:param accept: True表示确认False表示取消其它值不会按按钮但依然返回文本值
:param send: 处理prompt提示框时可输入文本
:param timeout: 等待提示框出现的超时时间为None则使用self.timeout属性的值
:return: 提示框内容文本未等到提示框则返回None
:return: 提示框内容文本未等到提示框则返回False
"""
timeout = self.timeout if timeout is None else timeout
timeout = .1 if timeout <= 0 else timeout
@ -323,7 +323,7 @@ class ChromiumPage(ChromiumBase):
while not self._alert.activated and perf_counter() < end_time:
sleep(.1)
if not self._alert.activated:
return None
return False
res_text = self._alert.text
if self._alert.type == 'prompt':

View File

@ -97,7 +97,7 @@ class ChromiumPage(ChromiumBase):
def close_other_tabs(self, tabs_or_ids: Union[
str, ChromiumTab, List[Union[str, ChromiumTab]], Tuple[Union[str, ChromiumTab]]] = None) -> None: ...
def handle_alert(self, accept: bool = True, send: str = None, timeout: float = None) -> Union[str, None]: ...
def handle_alert(self, accept: bool = True, send: str = None, timeout: float = None) -> Union[str, False]: ...
def quit(self) -> None: ...