From 2834cc8ca3bea593ed55a85c9209cd0c32048dbc Mon Sep 17 00:00:00 2001 From: g1879 Date: Tue, 26 May 2020 14:09:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=84=E7=90=86=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=E6=A1=86=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/driver_page.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/DrissionPage/driver_page.py b/DrissionPage/driver_page.py index f04ecc6..3585533 100644 --- a/DrissionPage/driver_page.py +++ b/DrissionPage/driver_page.py @@ -8,6 +8,7 @@ from glob import glob from typing import Union, List, Any from urllib import parse +from selenium.common.exceptions import NoAlertPresentException from selenium.webdriver.chrome.webdriver import WebDriver from selenium.webdriver.remote.webelement import WebElement @@ -213,3 +214,25 @@ class DriverPage(object): def chrome_downloading(self, download_path: str) -> list: """检查下载情况""" return glob(f'{download_path}\\*.crdownload') + + def process_alert(self, mode: str = 'ok', text: str = None) -> Union[str, None]: + """处理提示框 + :param mode: 'ok' 或 'cancel',若输入其它值,不会按按钮但依然返回文本值 + :param text: 处理prompt提示框时可输入文本 + :return: + """ + try: + alertObject = self.driver.switch_to.alert + except NoAlertPresentException: + return None + + if text: + alertObject.send_keys(text) + + text = alertObject.text + if mode == 'cancel': + alertObject.dismiss() + elif mode == 'ok': + alertObject.accept() + + return text