I am trying to simulate the login process to my facebook page using HtmlUnit (and I do have good reasons to do the same). Here is my java code for the same:
public static void main(String[] args) throws IOException {
//tried to experiment with the browser types also. But to the same result
//even using no param constructor does not help.
WebClient webClient=new WebClient(BrowserVersion.CHROME);
HtmlPage page1=webClient.getPage("https://www.facebook.com/bhramakarserver");
HtmlForm loginForm=(HtmlForm)page1.getElementById("login_form");
HtmlTextInput username=(HtmlTextInput)page1.getElementById("email");
HtmlPasswordInput password=(HtmlPasswordInput)page1.getElementById("pass");
username.setValueAttribute("myFbUsername");
password.setValueAttribute("myFbPassword");
HtmlElement button = (HtmlElement) page1.createElement("button");
button.setAttribute("type", "submit");
// append the button to the form
loginForm.appendChild(button);
page1=button.click();
//page1.executeJavaScript("window.scrollBy(0,6000)"); does not work
System.out.println(page1.asXml());
HtmlSpan postContentSpan=(HtmlSpan)page1.getByXPath("//span[@class='userContent']").get(0);
System.out.println(postContentSpan.asXml());
}
When I run this, I get the following error:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at com.rahulserver.fbhighlight.Main.main(Main.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
So clearly the pathogenic line is
HtmlSpan postContentSpan=(HtmlSpan)page1.getByXPath("//span[@class='userContent']").get(0);
The xpath is returning null. I posted this question related to it and go the answer that that the code containing the above xpath is commented out,hence is returning null.
So why is that happening and how do I make it work? As the page loads on scrolling down further,as is usual with facebook, I tried to simulate the process using
page1.executeJavaScript("window.scrollBy(0,6000)");
But yet it does not work and I get the same result. Here is the generated html file's pastebin link:http://pastebin.com/MfXsYSJQ.
I am sure that someone on SO would be able to come up with an out-of-the box answer to it...
.