mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
handle_alert()等待不到时返回False;while均设置超时
This commit is contained in:
parent
fbeb9056c4
commit
a704c12f38
@ -124,7 +124,8 @@ class ChromiumBase(BasePage):
|
|||||||
except TabClosedError:
|
except TabClosedError:
|
||||||
return
|
return
|
||||||
|
|
||||||
while True:
|
end_time = perf_counter() + 10
|
||||||
|
while perf_counter() < end_time:
|
||||||
try:
|
try:
|
||||||
b_id = self.run_cdp('DOM.getDocument')['root']['backendNodeId']
|
b_id = self.run_cdp('DOM.getDocument')['root']['backendNodeId']
|
||||||
self._root_id = self.run_cdp('DOM.resolveNode', backendNodeId=b_id)['object']['objectId']
|
self._root_id = self.run_cdp('DOM.resolveNode', backendNodeId=b_id)['object']['objectId']
|
||||||
@ -133,8 +134,15 @@ class ChromiumBase(BasePage):
|
|||||||
break
|
break
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
if self._debug_recorder:
|
if self._debug:
|
||||||
self._debug_recorder.add_data((perf_counter(), 'err', '读取root_id出错'))
|
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:
|
if self._debug:
|
||||||
print('获取document结束')
|
print('获取document结束')
|
||||||
@ -606,7 +614,7 @@ class ChromiumBase(BasePage):
|
|||||||
self._debug_recorder.add_data((perf_counter(), '操作', '停止页面加载'))
|
self._debug_recorder.add_data((perf_counter(), '操作', '停止页面加载'))
|
||||||
|
|
||||||
self.run_cdp('Page.stopLoading')
|
self.run_cdp('Page.stopLoading')
|
||||||
while self.ready_state != 'complete':
|
while self.ready_state not in ('complete', None):
|
||||||
sleep(.1)
|
sleep(.1)
|
||||||
|
|
||||||
def remove_ele(self, loc_or_ele):
|
def remove_ele(self, loc_or_ele):
|
||||||
@ -770,7 +778,7 @@ class ChromiumBase(BasePage):
|
|||||||
|
|
||||||
if t < times:
|
if t < times:
|
||||||
sleep(interval)
|
sleep(interval)
|
||||||
while self.ready_state != 'complete':
|
while self.ready_state not in ('complete', None):
|
||||||
sleep(.1)
|
sleep(.1)
|
||||||
if self._debug or show_errmsg:
|
if self._debug or show_errmsg:
|
||||||
print(f'重试 {to_url}')
|
print(f'重试 {to_url}')
|
||||||
|
@ -109,7 +109,8 @@ class ChromiumFrame(ChromiumBase):
|
|||||||
if self._debug:
|
if self._debug:
|
||||||
print('---获取document')
|
print('---获取document')
|
||||||
|
|
||||||
while True:
|
end_time = perf_counter() + 10
|
||||||
|
while perf_counter() < end_time:
|
||||||
try:
|
try:
|
||||||
if self._is_diff_domain is False:
|
if self._is_diff_domain is False:
|
||||||
node = self.page.run_cdp('DOM.describeNode', backendNodeId=self.ids.backend_id)['node']
|
node = self.page.run_cdp('DOM.describeNode', backendNodeId=self.ids.backend_id)['node']
|
||||||
@ -124,6 +125,9 @@ class ChromiumFrame(ChromiumBase):
|
|||||||
except Exception:
|
except Exception:
|
||||||
sleep(.1)
|
sleep(.1)
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise RuntimeError('获取document失败。')
|
||||||
|
|
||||||
if self._debug:
|
if self._debug:
|
||||||
print('---获取document结束')
|
print('---获取document结束')
|
||||||
|
|
||||||
@ -272,13 +276,21 @@ class ChromiumFrame(ChromiumBase):
|
|||||||
return 'complete'
|
return 'complete'
|
||||||
|
|
||||||
else:
|
else:
|
||||||
while True:
|
end_time = perf_counter() + 10
|
||||||
|
while perf_counter() < end_time:
|
||||||
try:
|
try:
|
||||||
return self.doc_ele.run_js('return this.readyState;')
|
return self.doc_ele.run_js('return this.readyState;')
|
||||||
except ContextLossError:
|
except ContextLossError:
|
||||||
node = self.run_cdp('DOM.describeNode', backendNodeId=self.frame_ele.ids.backend_id)['node']
|
try:
|
||||||
doc = ChromiumElement(self.page, backend_id=node['contentDocument']['backendNodeId'])
|
node = self.run_cdp('DOM.describeNode', backendNodeId=self.frame_ele.ids.backend_id)['node']
|
||||||
return doc.run_js('return this.readyState;')
|
doc = ChromiumElement(self.page, backend_id=node['contentDocument']['backendNodeId'])
|
||||||
|
return doc.run_js('return this.readyState;')
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
sleep(.1)
|
||||||
|
|
||||||
|
raise RuntimeError('获取document失败。')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def scroll(self):
|
def scroll(self):
|
||||||
@ -558,7 +570,7 @@ class ChromiumFrame(ChromiumBase):
|
|||||||
|
|
||||||
if t < times:
|
if t < times:
|
||||||
sleep(interval)
|
sleep(interval)
|
||||||
while self.ready_state != 'complete':
|
while self.ready_state not in ('complete', None):
|
||||||
sleep(.1)
|
sleep(.1)
|
||||||
if self._debug:
|
if self._debug:
|
||||||
print('重试')
|
print('重试')
|
||||||
|
@ -261,7 +261,7 @@ class ChromiumPage(ChromiumBase):
|
|||||||
|
|
||||||
self.driver.stop()
|
self.driver.stop()
|
||||||
self._driver_init(tab_id)
|
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()
|
self._get_document()
|
||||||
|
|
||||||
def close_tabs(self, tabs_or_ids=None, others=False):
|
def close_tabs(self, tabs_or_ids=None, others=False):
|
||||||
@ -315,7 +315,7 @@ class ChromiumPage(ChromiumBase):
|
|||||||
:param accept: True表示确认,False表示取消,其它值不会按按钮但依然返回文本值
|
:param accept: True表示确认,False表示取消,其它值不会按按钮但依然返回文本值
|
||||||
:param send: 处理prompt提示框时可输入文本
|
:param send: 处理prompt提示框时可输入文本
|
||||||
:param timeout: 等待提示框出现的超时时间,为None则使用self.timeout属性的值
|
:param timeout: 等待提示框出现的超时时间,为None则使用self.timeout属性的值
|
||||||
:return: 提示框内容文本,未等到提示框则返回None
|
:return: 提示框内容文本,未等到提示框则返回False
|
||||||
"""
|
"""
|
||||||
timeout = self.timeout if timeout is None else timeout
|
timeout = self.timeout if timeout is None else timeout
|
||||||
timeout = .1 if timeout <= 0 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:
|
while not self._alert.activated and perf_counter() < end_time:
|
||||||
sleep(.1)
|
sleep(.1)
|
||||||
if not self._alert.activated:
|
if not self._alert.activated:
|
||||||
return None
|
return False
|
||||||
|
|
||||||
res_text = self._alert.text
|
res_text = self._alert.text
|
||||||
if self._alert.type == 'prompt':
|
if self._alert.type == 'prompt':
|
||||||
|
@ -97,7 +97,7 @@ class ChromiumPage(ChromiumBase):
|
|||||||
def close_other_tabs(self, tabs_or_ids: Union[
|
def close_other_tabs(self, tabs_or_ids: Union[
|
||||||
str, ChromiumTab, List[Union[str, ChromiumTab]], Tuple[Union[str, ChromiumTab]]] = None) -> None: ...
|
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: ...
|
def quit(self) -> None: ...
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user