I have been using HtmlUnit to login into my school portal distantly. As I understand, school portals have always problems with logging in. I know that there are other questions about this topic, but if you don't mind, could you please look though my specific example.
Using my code I want to fill out the login form on the main page to then access my personal profile. However, htmlUnit gives me a run-time error and returns back to the main page.
I attached the code I try to execute and errors that I get. Also, I attached output that I get (It is not what I want). (This is what link I am finally trying to get: https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/PortalMainPage)
public class MistarLogin {
public static void main(String[] args) throws Exception {
//java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
//java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
int javaScriptLeftNum = webClient.waitForBackgroundJavaScript(3000);
// Get the first page
HtmlPage firstPage = (HtmlPage) webClient.getPage("https://mistar.oakland.k12.mi.us/novi/StudentPortal");
System.out.println(firstPage.asText());
System.out.println("\n\n\n\n\n\n\n-------------------------------------------------------------------------------\n\n\n\n\n\n");
// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
HtmlForm form = firstPage.getFormByName("loginform");
// Enter login and passwd
//form.getInputByName("districtid").setValueAttribute("");
form.getInputByName("Pin").setValueAttribute("email");
form.getInputByName("Password").setValueAttribute("password");
// Click "Sign In" button/link
HtmlInput loginButton = form.getInputByValue("Log In");
HtmlPage testPage = (HtmlPage) loginButton.click();
// I added the cookie section but this returns a null pointer exception
Set<Cookie> cookie = webClient.getCookieManager().getCookies();
if(cookie != null){
Iterator<Cookie> i = cookie.iterator();
while (i.hasNext()) {
webClient.getCookieManager().addCookie(i.next());
}
}
// final HtmlPage secondPage = loginButton.click();
Then just print out firstPage, testPage (The output is the same)
This is what output I get:
Student Portal
District Website
StudentPortal Login
User Name:
Password:
Log In
0
Novi Community School District 1
Loading Information...
Loading Information...
This is an error I get (the most essential one):
com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError SEVERE: runtimeError: message=[An invalid or illegal selector was specified (selector: ':input[form="loginform"]' error: Invalid selector: *:input[form="loginform"]).] sourceName=[https://mistar.oakland.k12.mi.us/novi/StudentPortal/Scripts/jquery-1.11.2.min.js?m=20150316043710] line=[2] lineSource=[null] lineOffset=[0]
P.S. Sorry for formating, I am not familiar with Stackoverflow fomating rules. P.P.S. If there are any questions, please ask. I will be looking on this question constantly. Thank you, again.