How can I get WebDriver to dismiss a Firefox Security Alert?

江枫思渺然 提交于 2019-12-10 16:23:41

问题


I am writing a test script using spock, geb, and WebDriver. The script submits a form on an insecure page. The page submits to a secure HTTPS URL. Firefox shows a warning for this, specifcally:

This causes the following error:

org.openqa.selenium.UnhandledAlertException: Modal dialog present
Build info: version: '2.23.0', revision: '17137', time: '2012-06-07 09:08:12'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.7.4', java.version: '1.6.0_33'
Driver info: driver.version: RemoteWebDriver
Session ID: 79e0444f-4c8e-2b4c-9066-601e7cdb3aa9
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:188)
    at org.openqa.selenium.remote.ErrorHandler.createUnhandledAlertException(ErrorHandler.java:169)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:141)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:458)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:462)
    at org.openqa.selenium.remote.RemoteWebDriver.getTitle(RemoteWebDriver.java:204)
    at geb.Page.getTitle(Page.groovy:200)
    at groovy.lang.Closure.getPropertyTryThese(Closure.java:197)
    at groovy.lang.Closure.getPropertyDelegateFirst(Closure.java:187)
    at groovy.lang.Closure.getProperty(Closure.java:172)

How do I have WebDriver dismiss this alert if it shows up?


回答1:


Found the answer in a question: Handling Alert in webdriver 2.21and mozilla11

driver.switchTo().alert().accept()



回答2:


Remember to add a wait on alert before executing accept on it. Following function for me in java.

    private void acceptSecurityAlert() {

    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(10, TimeUnit.SECONDS)          
                                                            .pollingEvery(3, TimeUnit.SECONDS)          
                                                            .ignoring(NoSuchElementException.class);    
    Alert alert = wait.until(new Function<WebDriver, Alert>() {       

        public Alert apply(WebDriver driver) {

            try {

                return driver.switchTo().alert();

            } catch(NoAlertPresentException e) {

                return null;
            }
        }  
    });

    alert.accept();
}


来源:https://stackoverflow.com/questions/12082052/how-can-i-get-webdriver-to-dismiss-a-firefox-security-alert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!