如何接受网站的所有提醒?

时间:2014-07-25 08:48:31

标签: selenium python-3.x

浏览网页时,我偶尔会收到提醒。我如何接受出现的每个警报?我不知道给定网页中的警报数量是多少。

2 个答案:

答案 0 :(得分:0)

您可以设置功能。但是,某些特定浏览器可能无法实现所需的功能,请自行测试(据我所知,它仅适用于FF)。

capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,UnexpectedAlertBehaviour.ACCEPT);

答案 1 :(得分:0)

link提醒,让我们进行测试:

from selenium import webdriver
from selenium.webdriver.common import alert
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
class AlertsManager:
    def alertsManager(self,url):
        self.url_to_visit=url
        self.driver=webdriver.Firefox()
        self.driver.get(self.url_to_visit)
        try:
            while WebDriverWait(self.driver,1).until(EC.alert_is_present()):
                self.alert=self.driver.switch_to_alert()
                self.driver.switch_to_alert().accept()
        except TimeoutException:
            pass
        print("Continue what you want here ...")

if __name__=='__main__':
    AM=AlertsManager()
    url="http://htmlite.com/JS006.php"
    AM.alertsManager(url)