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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With