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

2

4

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:

Security Warning

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?

Whimper answered 22/8, 2012 at 22:2 Comment(0)
W
5

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

driver.switchTo().alert().accept()
Whimper answered 22/8, 2012 at 22:13 Comment(4)
I'm getting the exception even after calling that statement.Jeanniejeannine
Maybe you are getting a different type of alert?Whimper
I just had to upgrade my selenium driver. Sorry for the false alarm!Jeanniejeannine
I am facing the same problem. Can anybody help me with the solution?Monopteros
S
1

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();
}
Sadi answered 7/4, 2017 at 5:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.