我在使用 HTMLUnit 时遇到问题。不久我要做的是填写一份表格并登录到一个网页,然后按该页面上的按钮。事实上,我做不到这个过程,但我正在努力。这是我的 HTML 表单源代码和 Java 源代码:
这是来自登录屏幕:
<form action="/login" method="post">
...
<input type="text" name="login_email" id="login_email" value="" />
<input type="password" name="login_password" id="login_password" />
<input type="submit" id="login_submit" name="login_submit" value="Sign in" />
</form>
此表单中有一些隐藏的输入。我知道这听起来很有趣,但是当我不对隐藏输入执行任何操作时,我的 Java 代码就可以工作。
这是我使用此表单登录的 Java 代码:
此代码来自 stackoverflow 问题。我只是测试一下,仅此而已。
WebClient webClient = new WebClient();
webClient.setThrowExceptionOnScriptError(false);
HtmlPage currentPage = webClient.getPage("https://www.blablabla.com:1234");
final HtmlForm form = currentPage.getFirstByXPath("//form[@action='/login']");
HtmlTextInput username = (HtmlTextInput) currentPage.getElementById("login_email");
HtmlPasswordInput password = (HtmlPasswordInput) currentPage.getElementById("login_password");
username.setText("username@blablabla.com");
password.setText("passW0rd");
HtmlButton submitButton = (HtmlButton) currentPage.createElement("button");
submitButton.setAttribute("type", "submit");
form.appendChild(submitButton);
HtmlPage newPage = submitButton.click();
System.out.println(newPage.asText());
在下一部分之前一切都很好。我可以登录,查看新页面的内容。
但是,当我尝试按下新页面中的按钮时,我什么也没得到。事实上,我想我根本按不下去。
这是我的“buttony”和新网页的 HTML 源代码:
<form action="auth" method="post">
<input type="submit" name="allow" value="Allow"/>
</form>
还有一些隐藏的输入。
以下是尝试按下名称为“allow”的按钮的 Java 代码:
HtmlButton button = newPage.getElementByName("allow");
HtmlPage page = button.click() ;
为了最后一次检查,我使用了另一段代码:
System.out.println(page.asText());
但是我遇到了这样的错误
错误开始
WARNING: getElementById(script1338426904717) did a getElementByName for Internet Explorer
31.May.2012 04:15:04 com.gargoylesoftware.htmlunit.javascript.host.ActiveXObject jsConstructor
WARNING: Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash'.
31.May.2012 04:15:04 com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError
SEVERE: runtimeError: message=[Automation server can't create object for 'ShockwaveFlash.ShockwaveFlash'.] sourceName=[https://www.jdkahsjkda/dksajda.js] line=[12] lineSource=[null] lineOffset=[0]
31.May.2012 04:15:04 com.gargoylesoftware.htmlunit.javascript.host.ActiveXObject jsConstructor
错误结束
只要我可以登录,这些错误对我来说就没有问题。
我可以登录并查看该页面。它说的是“欢迎用户名密码...” 但是,我无法按下按钮,也无法执行其他任何操作。
希望大家能帮我解决这个问题。
非常感谢。
保重,谢谢。
编辑:
现在我收到此错误:
Exception in thread "main" com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[*] attributeName=[name] attributeValue=[allow]
at com.gargoylesoftware.htmlunit.html.HtmlPage.getElementByName(HtmlPage.java:1565)
at cza.main(cza.java:54)
但是,有一个名为“允许”的按钮。我正在查看第二个页面的源代码,我看到了这个:
<input type="submit" name="allow" value="Allow"/>
<input type="submit" name="deny" value="Deny"/>
因此,有一个名为允许和拒绝的按钮。然而,这段代码失败了。 这可能是因为 JS 还是什么原因? 我尝试从首页找到提交按钮并使用它提交表单。不使用假按钮,它又失败了。 我为此使用了 HTMLSubmitInput,它再次失败。
再次感谢。
最佳答案
抱歉,我还不能发表评论...
newPage.getWebResponse().getContentAsString()
内容是什么?
我猜你的页面可能包含许多名为“allow”的 html 元素
最好确保您获得独特且正确的元素,有很多方法可以做到这一点:
element.getElementById("id")
page.getFirstByXPath("xpathExpr") || page.getByXPath("xpathExpr")
等等...目标实际上是确保使用您需要的元素。
当使用输入时,将表单作为变量来操作输入始终是一个好方法。例如:
HtmlForm form = page.getforms(0);
form.getInputByName("name");
form.getInputByValue("value");
顺便说一句,一些提示:使用 FF 设置初始化 Web 客户端,如下所示: 客户端 = new WebClient(BrowserVersion.FIREFOX_3_6); 它获得了最佳的 html 代码覆盖率 ( http://build.canoo.com/htmlunit/artifacts/ )
始终尝试打开/关闭 JS:client.setJavaScriptEnabled(false);client.setThrowExceptionOnScriptError(false);
关于java - HTMLUnit 重定向和按钮单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10826317/