2016-09-25 110 views
0

我在此之前發了一篇文章,但已經獲得了一些關於如何做的更多細節,但我仍然無法做到這一點。這是代碼的主要部分。當我運行它時,我得到了一大堆與控制檯中的css相關的警告。它不會工作。我試圖獲得用戶的名字,正如我在代碼中提到的那樣。如果有人能幫上忙,那對我來說意味着很多。該網站是我的學校網站:https://lionel2.kgv.edu.hk/login/index.php。如果有幫助,我已經包括登錄網站(我刪除了除用戶部分以外的大多數元素)。在此先感謝,如何從使用HTMLUNIT登錄的網站獲取信息?

Vijay。

網站: https://drive.google.com/a/kgv.hk/file/d/0B-O_Xw0mAw7tajJhVlRxTkFhOE0/view?usp=sharing

//most of this is from https://gist.github.com/harisupriyanto/6805988 

    String loginUrl = "http://lionel2.kgv.edu.hk"; 
    int loginFormNum = 1; 
    String usernameInputName = "nameinput"; 
    String passwordInputName = "passinput"; 
    String submitLoginButtonValue = "Sign In"; 

    // create the HTMLUnit WebClient instance 
    WebClient wclient = new WebClient(); 

    // configure WebClient based on your desired 
    wclient.getOptions().setPrintContentOnFailingStatusCode(false); 
    wclient.getOptions().setCssEnabled(true); 
    wclient.getOptions().setThrowExceptionOnFailingStatusCode(false); 
    wclient.getOptions().setThrowExceptionOnScriptError(false); 

try { 

     final HtmlPage loginPage = (HtmlPage)wclient.getPage(loginUrl); 

     final HtmlForm loginForm = loginPage.getForms().get(loginFormNum); 

     final HtmlTextInput txtUser = loginForm.getInputByName(usernameInputName); 
     txtUser.setValueAttribute(username); 
     final HtmlPasswordInput txtpass = loginForm.getInputByName(passwordInputName); 
     txtpass.setValueAttribute(password); 
     final HtmlSubmitInput submitLogin = loginForm.getInputByValue(submitLoginButtonValue); 


     final HtmlPage returnPage = submitLogin.click(); 

     final HtmlElement returnBody = returnPage.getBody(); 
     //if (//there is a class called "Login info, then print out the  nodeValue.) { 

    // } 

    } catch(FailingHttpStatusCodeException e) { 
     e.printStackTrace(); 
    } catch(Exception e) { 
     e.printStackTrace(); 
    } 
    } 

回答

0

你很可能並不需要CSS所以你可以禁用它。

爲了提高性能並減少警告和錯誤,我儘可能地禁用/限制。

webClient.setJavaScriptTimeout(30 * 1000); // 30s 
    webClient.getOptions().setTimeout(300 * 1000); // 300s 
    webClient.getOptions().setCssEnabled(false); 
    webClient.getOptions().setThrowExceptionOnScriptError(false); // no Exceptions because of javascript 
    webClient.getOptions().setPopupBlockerEnabled(true); 
+0

我加了那些但我得到這些錯誤。 – Vijay

+0

感謝您的回覆 我添加了這些,但我得到這些錯誤:https://docs.google.com/a/kgv.hk/document/d/1FPdOCfYGzjgidddPUDN3GZtL-TDAn5b_OrQiLIlW8Mo/edit?usp=sharing – Vijay

+0

如果您知道如何請給我留言,我真的很感激。 – Vijay