【发布时间】:2018-12-21 10:46:11
【问题描述】:
我目前正在开发一个系统。我需要使用 Java 单击网站按钮。所以我正在使用 HtmlUnit 库。有一个名为https://tempmail.ninja/ 的网站会生成临时电子邮件。我需要的是单击 tempmail.ninja 中的“生成”按钮的程序,它会生成一封临时电子邮件。但问题是它没有点击。不生成电子邮件。
这是我尝试过的代码,
try
{
WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setCssEnabled(true);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getCookieManager().setCookiesEnabled(true);
HtmlPage page = webClient.getPage("https://tempmail.ninja");
//Here is button id. Instead of this I used HtmlAnchor, HtmlSubmitInput and etc.
//But any of those didn't work
HtmlButton htmlButton = page.getHtmlElementById("generaEmailTemporal");
htmlButton.click();
webClient.waitForBackgroundJavaScript(5000 * 2);
//Print the generated email but Currently nothing display
HtmlTextInput htmlTextInput = (HtmlTextInput) page.getElementById("emailtemporal");
System.out.println(htmlTextInput.getText());
webClient.close();
} catch(ElementNotFoundException | FailingHttpStatusCodeException | IOException ex) {
Logger.getLogger(WebTesting.class.getName()).log(Level.SEVERE, null, ex);
}
这里是按钮的 HTML 代码。我使用检查元素得到这个。
<p class="text-center" id="btnGeneraEmailTemporal">
<button class="btn btn-labeled btn-primary" id="generaEmailTemporal" type="button">
<span class="btn-label"><i class="fa fa-hand-o-right" aria-hidden="true"></i></span> Generate Temp Mail
</button>
</p>
我是 HtmlUnit 的新手。那么有人可以帮助我吗?我非常感谢。 谢谢你的帮助。
【问题讨论】: