HtmlUnit:单击不响应时登录HtmlElement

时间:2018-07-03 23:28:17

标签: java html xpath htmlunit

嗨,这是我第一次使用HtmlUnit [2.31版],我正尝试登录到一个网页。这是HTML:

webpack-dev-server

这是我的代码:

<body>
    <div id="login">
        <div id="header">
            User Log In
        </div>
        <div id="error">Enter your credentials to login</div>
        <table>
            <tr>
                <th>Username</th>
                <td><input type="text" id="username" /></td>
            </tr>
            <tr>
                <th>Password</th>
                <td><input type="password" id="password" /></td>
            </tr>
        </table>
        <div id="buttons">
            <input type="button" value="Login" id="button" onclick="login();" />
        </div>
    </div>
</body>

}

两个页面(登录之前和之后)与打印出来的相同。所以我必须在这里做错了。任何帮助将不胜感激。

编辑1: 在输入用户名和密码之后,在点击行之前,我已经尝试过Thread.sleep(2000)

编辑2: 用于登录的js:

 WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52);
 webClient.getOptions().setJavaScriptEnabled(false);
 webClient.getOptions().setUseInsecureSSL(true);
 try{            
        HtmlPage page = webClient.getPage(url);
        String pageContent = page.asText();
        System.out.println(pageContent);
        HtmlButtonInput button = page.getFirstByXPath("//input[@type = 'button']");
     //I'm new to XPath, but I think this works okay


        HtmlTextInput name  = (HtmlTextInput) page.getElementById("username"); 
        HtmlPasswordInput pwd  = (HtmlPasswordInput) page.getElementById("password");

        System.out.println(name.getSelectedText());
        name.setValueAttribute(username);
        pwd.setValueAttribute(password);
        System.out.println(name.getSelectedText());

        HtmlPage loggedInPage = button.click();
        String pageContent2 = loggedInPage.asText();
        System.out.println("after logged in");
        System.out.println(pageContent2);

2 个答案:

答案 0 :(得分:1)

由于您尚未发布您正在呼叫的网址,因此我只能提供一些提示。

即使HtmlUnit在幕后发挥了很多魔力,您也需要对所有网络技术有基本的了解

从代码看来,登录是基于Ajax完成的;这有一些含义:

  • Ajax需要启用JavaScript(默认为HtmlUnit)
  • Ajax是异步的-HtmlUnit中的所有动作(例如,点击)都是同步的,这意味着您必须等待ajax调用完成
  • 在特殊情况下,ajax调用会通过使用不同的URL(document.location ='main.html')重新加载页面来成功更改页面的内容。因此,您必须刷新页面变量

或在代码中

try (WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52))
{
    webClient.getOptions().setUseInsecureSSL(true);

    HtmlPage page = webClient.getPage(url);
    String pageContent = page.asText();
    System.out.println(pageContent);

    HtmlButtonInput button = page.getFirstByXPath("//input[@type = 'button']");
    // to make sure you got the right element
    System.out.println(button.asXml());

    HtmlTextInput name  = (HtmlTextInput) page.getElementById("username"); 
    HtmlPasswordInput pwd  = (HtmlPasswordInput) page.getElementById("password");

    // use type() to simulate typing
    name.type(username);
    pwd.type(password);

    // no need to get the page here because this is still the one the
    // button is placed on
    button.click();

    // wait for ajax to do the job
    webClient.waitForBackgroundJavaScript(10000);


    // ok hopefully the job is done and the login was successfull
    // lets get the current page out of the current window
    HtmlPage loggedInPage = (HtmlPage) page.getEnclosingWindow().getTopWindow().getEnclosedPage();

    ...

    // check the result
    // you can also write this to a file and open it in a real browser
    // maybe the login was failing and there is an error message
    // rendered on this page
    System.out.println(loggedInPage.asXml());

}

希望有帮助。

答案 1 :(得分:0)

我建议您尝试设置:

 webClient.getOptions().setJavaScriptEnabled(true);
 webClient.getOptions().setRedirectEnabled(true);