I am trying to download all the images of recaptcha but somehow I am not able to click on the reCaptcha iframe's checkbox. When clicking on it the HtmlUnit throws the WrappedException. I am not sure why this is happening and How am I suppose to click on the link and download the images? My guess is this is the problem with the GWT. I can click any other normal button.
Any help will be highly appreciated
The main site is: https://www.google.com/recaptcha/api2/demo
Here I have done so far.
private static final Logger LOG = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) throws IOException {
try (WebClient webClient = new WebClient(BrowserVersion.FIREFOX_38)) {
webClient.getCache().clear();
final WebClientOptions webClientOptions = webClient.getOptions();
webClientOptions.setTimeout(40000);
webClientOptions.setRedirectEnabled(false);
// webClientOptions.setUseInsecureSSL(true);
webClient.setAlertHandler(new AlertHandler() {
public void handleAlert(Page page, String string) {
System.out.printf("alert: %s%n", string);
LOG.info("javascript alert: {}", string);
}
});
webClientOptions.setJavaScriptEnabled(true);
webClient.setCssErrorHandler(new SilentCssErrorHandler());
// webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClientOptions.setThrowExceptionOnScriptError(false);
webClientOptions.setThrowExceptionOnFailingStatusCode(false);
HtmlPage reCaptchaFrame;
final HtmlPage page = webClient.getPage("https://www.google.com/recaptcha/api2/demo");
webClient.getJavaScriptEngine().pumpEventLoop(1000);
webClient.waitForBackgroundJavaScript(200);
int waitForBackgroundJavaScript = webClient.waitForBackgroundJavaScript(200);
int loopCount = 0;
while (waitForBackgroundJavaScript > 0 && loopCount < 2) {
++loopCount;
waitForBackgroundJavaScript = webClient.waitForBackgroundJavaScript(200);
if (waitForBackgroundJavaScript == 0) {
if (LOG.isTraceEnabled())
LOG.trace("HtmlUnit exits background javascript at loop counter " + loopCount);
break;
}
}
JavaScriptEngine engine = webClient.getJavaScriptEngine();
engine.holdPosponedActions();
final List<FrameWindow> frames = page.getFrames();
reCaptchaFrame = (HtmlPage) frames.get(0).getEnclosedPage();
// initiating to enter the reCaptcha
final HtmlSpan reCaptchaAnchor = reCaptchaFrame.getFirstByXPath(".//span[@id='recaptcha-anchor']");
if (reCaptchaAnchor == null) {
throw new NullPointerException("Captcha not found");
}
try {
HtmlPage page1 = reCaptchaAnchor.click(); // here I get the exception
} catch (WrappedException e) {
LOG.info("Found some stupid exception {}", e.details());
}
} catch (Exception e) {
LOG.info("Found exception {}", e.getMessage());
}
}
Stacktrace:
net.sourceforge.htmlunit.corejs.javascript.WrappedException: Wrapped java.lang.NullPointerException
at net.sourceforge.htmlunit.corejs.javascript.Context.throwAsScriptRuntimeEx(Con text.java:2053)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.doProcessPostponedActions(JavaScriptEngine.java:1007)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.processPostponedActions(JavaScriptEngine.java:1072)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:789)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:732)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:679)
at recaptchatest.Main.main(Main.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.NullPointerException
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.hasTopCall(ScriptRuntime.java:3263)
at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:102)
at com.gargoylesoftware.htmlunit.javascript.host.Promise$1.execute(Promise.java:136)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.doProcessPostponedActions(JavaScriptEngine.java:1002)
... 10 more
After upgrading HTMLUnit to 2.22 and htmlunit-core-js library to 2.22 everything working as expected.