gpt4 book ai didi

java - 无法使用 HtmlUnit 点击谷歌的新 reCaptcha 复选框

转载 作者:行者123 更新时间:2023-11-30 10:41:24 26 4
gpt4 key购买 nike

我正在尝试下载 recaptcha 的所有图像,但不知何故我无法点击 reCaptcha iframe 的复选框。单击它时,HtmlUnit 会抛出 WrappedException。我不确定为什么会这样,并且我应该如何点击链接并下载图像?我猜这是 GWT 的问题。我可以点击任何其他普通按钮。

任何帮助将不胜感激

主要站点是:https://www.google.com/recaptcha/api2/demo

到目前为止我已经完成了。

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());
}
}

堆栈跟踪:

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

最佳答案

将 HTMLUnit 升级到 2.22 并将 htmlunit-core-js 库升级到 2.22 后,一切都按预期工作。

关于java - 无法使用 HtmlUnit 点击谷歌的新 reCaptcha 复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38501537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com