Below is an example of how to handle an alert box popping up after an jQuery ajax call return when using selenium with the Facebook PHP WebDriver

<?php
/*before this is all your code that is doing things to the browser and perhaps submitting some data via ajax*/
//Lets make the webdriver wait for the ajax call
$webdriverWait = new WebDriverWait($driver, 10);
$webdriverWait->until(
//pass in our closure
function($driver)
{
echo 'loop…';
try
{
//Wait for jQuery to be done processing ajax request
$result = (0 === count($driver->executeScript("return jQuery.active == 0")));
}
catch (\Exception $ex) {
//catch an exception when execScript is called when an alert box is open.
//the Facebook driver will automatically handle closing the alert box
//return true so we can break out of the loop, and
$result = true;
}
return $result;
}
);
/*continue with your script*/