0

My program fails when it encounters a confirm box. I need to click that confirm box in order to proceed. I could not dig into its Html(using firebug) or so. Some one please let me know how to handle this situation?? Even this didn't work

enter code here
DesiredCapabilities capabilities;
capabilities = new DesiredCapabilities();
capabilities.setJavascriptEnabled(true);
capabilities.setCapability("takesScreenshot", false);
capabilities.setCapability(
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
            "/usr/local/bin/phantomjs");
WebDriver myTestDriver = new PhantomJSDriver(capabilities);
myTestDriver.manage().window().maximize();
myTestDriver.findElement(By.xpath("//input[@value = 'confirm']")).click();
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {  
    e.printStackTrace();
}
Alert javascriptconfirm = myTestDriver.switchTo().alert();
javascriptconfirm.accept();

Selenium Web Driver : Handle Confirm Box using Java

Thanks in Advance

Community
  • 1
  • 1
user2187797
  • 1
  • 1
  • 2

1 Answers1

1

Phantom does not currently handle alert and confirm dialogs out of the box (see here).

Until Phantom & Ghostdriver implement native support, the workaround is to inject javascript to handle the alert or confirm yourself.

Here's a snippet of C# code to do it, assuming that you want to simulate the user clicking "OK" in the confirm dialog:

string script = "window.confirm = function(message) { lastConfirmationMessage = message; return true; }";
IJavaScriptExecutor executor = (IJavaScriptExecutor) Driver;
executor.ExecuteScript(script);

You can then execute other javascript to retrieve "lastConfirmationMessage" if you want to assert that the text was as expected.

Jonathan Moffatt
  • 13,309
  • 8
  • 51
  • 49