Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HtmlUnit not able to login It returns same page after form submit

Tags:

java

htmlunit

I am trying to login on this a site with the help of HtmlUnit but after clicking login it is returning the same page with the input fields filled by values I put unable to login please suggest me a solution.

I am trying following code

        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();

After clicking login button same page is returning with filled input fields. so please help me out. Thanks

like image 250
Kunal Kishore Avatar asked Jan 31 '13 06:01

Kunal Kishore


1 Answers

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();
like image 79
Opal Avatar answered Nov 09 '22 15:11

Opal