6

我正在尝试在 HtmlUnit 的帮助下登录该站点,但是单击登录后,它返回相同的页面,其中输入字段填充了我无法登录的值,请建议我一个解决方案。

我正在尝试以下代码

        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(true);
        webClient.getOptions().setRedirectEnabled(true);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        webClient.getCookieManager().setCookiesEnabled(true);

        String url="http://xxxxxxxxx.xxx/";
        String name="XXXX";//here real value i am putting for name, accountNo and pass instead of XXXX
        String accountNo="XXXX";
        String pass="XXXX";

        HtmlPage page = webClient.getPage(url);
        System.out.println("1st page : "+page.asText());

        HtmlForm form=(HtmlForm)page.getElementById("aspnetForm");
        HtmlInput uName=(HtmlInput)form.getByXPath("//*[@id=\"ctl00_LoginControl_textUserName_text\"]").get(0);
        uName.setValueAttribute(name);
        HtmlInput acNo=(HtmlInput)form.getByXPath("//*[@id=\"ctl00_LoginControl_textCompanyAccount_text\"]").get(0);
        acNo.setValueAttribute(accountNo);          
        HtmlPasswordInput password=(HtmlPasswordInput)form.getByXPath("//*[@id=\"ctl00_LoginControl_textPassword\"]").get(0);
        password.setValueAttribute(pass);
        HtmlSubmitInput button = (HtmlSubmitInput) form.getByXPath("//*[@id=\"ctl00_LoginControl_buttonLogin\"]").get(0);

        page = (HtmlPage) button.click();
        System.out.println("2nd Page : "+page.asText());

        webClient.closeAllWindows();

单击登录按钮后,相同的页面将返回填充的输入字段。所以请帮帮我。谢谢

4

2 回答 2

6
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(false); // I think this speeds the thing up
webClient.getOptions().setRedirectEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getCookieManager().setCookiesEnabled(true);

String url="http://truckstop.com/";
String name="XXXX";
String accountNo="XXXX";
String pass="XXXX";

HtmlPage page = webClient.getPage(url);
System.out.println("1st page : "+page.asText());

HtmlForm form=(HtmlForm)page.getElementById("aspnetForm");
HtmlInput uName=(HtmlInput)form.getByXPath("//*[@id=\"ctl00_LoginControl_textUserName_text\"]").get(0);
uName.setValueAttribute(name);
HtmlInput acNo=(HtmlInput)form.getByXPath("//*[@id=\"ctl00_LoginControl_textCompanyAccount_text\"]").get(0);
acNo.setValueAttribute(accountNo);          
HtmlPasswordInput password=(HtmlPasswordInput)form.getByXPath("//*[@id=\"ctl00_LoginControl_textPassword\"]").get(0);
password.setValueAttribute(pass);
HtmlSubmitInput button = (HtmlSubmitInput) form.getByXPath("//*[@id=\"ctl00_LoginControl_buttonLogin\"]").get(0);

WebWindow window = page.getEnclosingWindow();
button.click();
while(window.getEnclosedPage() == page) {
    // The page hasn't changed.
    Thread.sleep(500);
}
// This loop above will wait until the page changes.
page = window.getEnclosedPage();
System.out.println("2nd Page : "+page.asText());

webClient.closeAllWindows();
于 2013-02-04T03:53:23.510 回答
0

也许该站点检查Referer标头参数:

webClient.addRequestHeader("Referer", "http://truckstop.com/");

使用 WebWindowListener 对页面更改做出反应(点击按钮后也等待 javascript):

webClient.addWebWindowListener( new WebWindowListener() {
    void webWindowContentChanged(WebWindowEvent event) {
        System.out.println("2nd Page : "+event.getNewPage().asText());
        webClient.closeAllWindows();
    }
    ...
});

button.click();
webClient.waitForBackgroundJavaScript(TIMEOUT);
于 2013-02-04T08:19:08.213 回答