public Response handle() throws Exception {
waitForPageToLoad();
int implicitWait = (Integer) getConf("implicit_wait", 0);
long deadline = System.currentTimeMillis() + implicitWait;
RemoteWebElement rwe = null;
do {
try {
rwe = findElement();
break;
} catch (InvalidSelectorException e) {
// no recovery here.
throw e;
} catch (NoSuchElementException e) {
//ignore and try again.
} catch (RemoteExceptionException e2) {
// looking on the root element, but document became invalid.
// Something (alert during onload ) might have prevented the page
// refresh.
if (!getRequest().hasVariable(":reference")) {
getWebDriver().getContext().newContext();
}
}
} while (System.currentTimeMillis() < deadline);
if (rwe == null) {
throw new NoSuchElementException(
"No element found for " + getRequest().getPayload() + " after waiting for " + implicitWait
+ " ms.");
} else {
JSONObject res = new JSONObject();
res.put("ELEMENT", rwe.getReference());
Response resp = new Response();
resp.setSessionId(getSession().getSessionId());
resp.setStatus(0);
resp.setValue(res);
return resp;