Logging in with HtmlUnit

I am very new to HtmlUnit. I want to know if I can enter the site using htmlunit and perform some operations on this site, for example, I want to enter my office portal and spend my vacation.

I use the html block and it shows some errors, is it possible to do this with the html unit or are there any other tools that I can use for this purpose ...
Here is my code
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6); webClient.setJavaScriptEnabled(true); webClient.getCookieManager().setCookiesEnabled(true); final HtmlPage page1 = webClient.getPage("http://www.ccstechnologies.org/login.aspx/"); final HtmlForm form = page1.getFormByName("form1"); final HtmlSubmitInput button = form.getInputByName("BtnLogin"); final HtmlTextInput textField = form.getInputByName("Username"); final HtmlPasswordInput pwd = form.getInputByName("password"); textField.setValueAttribute("username"); pwd.setValueAttribute("password"); final HtmlPage page2 = button.getEnclosingForm().click(); String htmlBody = page2.getWebResponse().getContentAsString(); System.out.println("Base Uri 1 : "+page1); System.out.println("Base Uri 2 : "+page2); webClient.closeAllWindows(); 

But when I print page2 , it only shows the URL of the login page, and it does not return the URL of the main page. What could be the problem?

This is what I got in the console when I click on the form

May 28, 2012 11:44:15 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Outdated content type failed: "application / x-javascript". May 28, 2012 11:44:16 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Outdated content type failed: "application / x-javascript". Uri 1 base: HtmlPage(http://www.ccstechnologies.org/login.aspx/)@2741851 Uri 2 base: HtmlPage (http://www.ccstechnologies.org/login.aspx/) @ 2741851

button generated results

 May 29, 2012 10:00:02 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'application/x-javascript'. May 29, 2012 10:00:02 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'application/x-javascript'. May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'application/x-javascript'. May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'application/x-javascript'. May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'application/x-javascript'. May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'application/x-javascript'. May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'text/javascript'. May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'text/javascript'. May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'text/javascript'. May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'text/javascript'. May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'text/javascript'. May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'text/javascript'. May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error WARNING: CSS error: [259:24] Error in expression. Invalid token "=". Was expecting one of: <S>, <COMMA>, "/", <PLUS>, "-", <HASH>, <STRING>, ")", <URI>, "inherit", <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <DIMENSION>, <PERCENTAGE>, <NUMBER>, <FUNCTION>, <IDENT>. May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error WARNING: CSS error: [259:29] Error in style rule. Invalid token "\r\n ". Was expecting one of: "}", ";". May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning WARNING: CSS warning: [259:29] Ignoring the following declarations in this rule. HtmlPage(http://192.168.0.5/login.aspx)@23511316 HtmlPage(http://192.168.0.5/login.aspx)@17700115 
+8
java htmlunit
source share
4 answers

Ok, I looked at him, it seems that the problem was in the button. I replaced you with a line of code for the button as follows:

  final HtmlPage page2 = (HtmlPage) form.getInputByValue("Login").click(); 

now it seems that he is at least trying to enter the system (and, of course, he prints an invalid login on the page), so he must work with the corresponding credentials. to print the page in java and see how it uses system.out.println (page1.asText ()) or asXml depending on what you want to see

my code finally is:

 final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6); webClient.setJavaScriptEnabled(true); webClient.getCookieManager().setCookiesEnabled(true); try{ final HtmlPage page1 = webClient.getPage("http://www.ccstechnologies.org/login.aspx/"); final HtmlForm form = page1.getFormByName("form1"); final HtmlSubmitInput button = form.getInputByName("BtnLogin"); final HtmlTextInput textField = form.getInputByName("Username"); final HtmlPasswordInput pwd = form.getInputByName("password"); textField.setValueAttribute("username"); pwd.setValueAttribute("password"); System.out.println(page1.asText()); final HtmlPage page2 = (HtmlPage) form.getInputByValue("Login").click(); String htmlBody = page2.getWebResponse().getContentAsString(); System.out.println(page2.asText()); System.out.println("Base Uri 1 : "+page1); System.out.println("Base Uri 2 : "+page2); webClient.closeAllWindows();}catch (Exception e) { // TODO: handle exception } 
+4
source share

this is what you should install for javascript:

 webClient.getOptions().setJavaScriptEnabled(false); 

and you can also add them to it.

 webClient.getOptions().setRedirectEnabled(true); webClient.getOptions().setThrowExceptionOnScriptError(false); webClient.getOptions().setCssEnabled(false); webClient.getOptions().setUseInsecureSSL(true); webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); webClient.getCookieManager().setCookiesEnabled(true); 

This should solve the problem, as it was for me.

+3
source share

try to enable cookies and try to enable javascript to ignore errors that it can print ... (I thought that errors in red are bad, in the html module this is not necessary)

+1
source share

If the website uses ajax call to login. It worked for me. Install this

 webClient.setAjaxController(new NicelyResynchronizingAjaxController()); 

This will cause all ajax calls to be synchronous.

This is how I configure the WebClient object

 WebClient webClient = new WebClient(BrowserVersion.CHROME); webClient.getOptions().setJavaScriptEnabled(true); webClient.getOptions().setCssEnabled(false); webClient.getOptions().setUseInsecureSSL(true); webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); webClient.getCookieManager().setCookiesEnabled(true); webClient.setAjaxController(new NicelyResynchronizingAjaxController()); webClient.getOptions().setThrowExceptionOnScriptError(false); webClient.getCookieManager().setCookiesEnabled(true); 
+1
source share

All Articles