增加set.auto_handle_alert();handle_alert()增加next_one参数

This commit is contained in:
g1879 2023-12-12 21:24:08 +08:00
parent a11267c8a3
commit 6a063787ee
4 changed files with 41 additions and 12 deletions

View File

@ -835,13 +835,19 @@ class ChromiumBase(BasePage):
if cookies:
self.run_cdp_loaded('Network.clearBrowserCookies')
def handle_alert(self, accept=True, send=None, timeout=None):
def handle_alert(self, accept=True, send=None, timeout=None, next_one=False):
"""处理提示框,可以自动等待提示框出现
:param accept: True表示确认False表示取消其它值不会按按钮但依然返回文本值
:param send: 处理prompt提示框时可输入文本
:param timeout: 等待提示框出现的超时时间为None则使用self.timeout属性的值
:param next_one: 是否处理下一个出现的提示框为True时timeout参数无效
:return: 提示框内容文本未等到提示框则返回False
"""
if next_one:
self._alert.handle_next = accept
self._alert.next_text = send
return
timeout = self.timeout if timeout is None else timeout
timeout = .1 if timeout <= 0 else timeout
end_time = perf_counter() + timeout
@ -857,16 +863,6 @@ class ChromiumBase(BasePage):
self.driver.run('Page.handleJavaScriptDialog', accept=accept)
return res_text
def _on_alert_close(self, **kwargs):
"""alert关闭时触发的方法"""
self._alert.activated = False
self._alert.text = None
self._alert.type = None
self._alert.defaultPrompt = None
self._alert.response_accept = kwargs.get('result')
self._alert.response_text = kwargs['userInput']
self._has_alert = False
def _on_alert_open(self, **kwargs):
"""alert出现时触发的方法"""
self._alert.activated = True
@ -877,6 +873,22 @@ class ChromiumBase(BasePage):
self._alert.response_text = None
self._has_alert = True
if self._alert.auto is not None:
self.handle_alert(self._alert.auto)
elif self._alert.handle_next is not None:
self.handle_alert(self._alert.handle_next, self._alert.next_text)
self._alert.handle_next = None
def _on_alert_close(self, **kwargs):
"""alert关闭时触发的方法"""
self._alert.activated = False
self._alert.text = None
self._alert.type = None
self._alert.defaultPrompt = None
self._alert.response_accept = kwargs.get('result')
self._alert.response_text = kwargs['userInput']
self._has_alert = False
def _wait_loaded(self, timeout=None):
"""等待页面加载完成,超时触发停止加载
:param timeout: 超时时间
@ -1107,6 +1119,9 @@ class Alert(object):
self.defaultPrompt = None
self.response_accept = None
self.response_text = None
self.handle_next = None
self.next_text = None
self.auto = None
def close_privacy_dialog(page, tid):

View File

@ -227,7 +227,8 @@ class ChromiumBase(BasePage):
def clear_cache(self, session_storage: bool = True, local_storage: bool = True, cache: bool = True,
cookies: bool = True) -> None: ...
def handle_alert(self, accept: bool = True, send: str = None, timeout: float = None) -> Union[str, False]: ...
def handle_alert(self, accept: bool = True, send: str = None, timeout: float = None,
next_one: bool = False) -> Union[str, False]: ...
def _on_alert_close(self, **kwargs): ...
@ -257,3 +258,6 @@ class Alert(object):
self.defaultPrompt: str = ...
self.response_accept: str = ...
self.response_text: str = ...
self.handle_next: Optional[bool] = ...
self.next_text: str = ...
self.auto: Optional[bool] = ...

View File

@ -146,6 +146,14 @@ class ChromiumBaseSetter(BasePageSetter):
self._page.run_cdp('Network.enable')
self._page.run_cdp('Network.setExtraHTTPHeaders', headers=headers)
def auto_handle_alert(self, on_off=True, accept=True):
"""设置是否启用自动处理弹窗
:param on_off: bool表示开或关
:param accept: bool表示确定还是取消
:return: None
"""
self._page._alert.auto = accept if on_off else None
# --------------即将废弃---------------
@property

View File

@ -59,6 +59,8 @@ class ChromiumBaseSetter(BasePageSetter):
def headers(self, headers: dict) -> None: ...
def auto_handle_alert(self, on_off: bool = True, accept: bool = True) -> None: ...
def upload_files(self, files: Union[str, list, tuple]) -> None: ...