【问题标题】:HTMLUnit Redirection and Button ClickHTMLUnit 重定向和按钮点击
【发布时间】:2012-05-31 01:20:31
【问题描述】:

我对 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>

还有一些隐藏的输入。

这是 -trying- 按下名为“允许”的按钮的 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)

但是,有一个名为“允许”的按钮。我正在查看 second 页面的来源,我看到了这个:

<input type="submit" name="allow" value="Allow"/>
<input type="submit" name="deny" value="Deny"/>

所以,有一个名为允许和拒绝的按钮。但是,此代码失败。 这可能是因为JS还是什么? 我尝试从 firstPage 中找到提交按钮并使用它提交表单。不是用假按钮,它再次失败。 我为此使用了 HTMLSubmitInput,它再次失败。

再次感谢。

【问题讨论】:

    标签: java htmlunit


    【解决方案1】:

    抱歉,我还不能发表评论...

    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 设置初始化 webclient,如下所示: client = new WebClient(BrowserVersion.FIREFOX_3_6); 它获得了最好的 html 代码覆盖率 (http://build.canoo.com/htmlunit/artifacts/)

    始终尝试打开/关闭 JS:client.setJavaScriptEnabled(false);client.setThrowExceptionOnScriptError(false);

    【讨论】:

    • 我更新了我的代码。问题仍然存在。但是,我会根据您的反馈更改它们。你可以在这里查看我的更新。非常感谢。 gist.github.com/2853917
    • 我更改了这样的代码:HtmlForm form2 = newPage.getFirstByXPath("//form[@action='authorize']"); HtmlPage finalPage = form2.getButtonByName("allow").click(); 但它仍然失败。
    猜你喜欢
    • 2016-11-28
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 2018-12-23
    相关资源
    最近更新 更多