All i want to do is remotely ask a question on ask.fm using java nad HTMLUnit framework. I think it doesn't work because of the javascript code, but i'm not sure.
this is how the ask question form looks like :
KILO K ♣
//
window.setTimeout(function(){try{$("textarea#profile-input").focus();}catch(e){}},10);
//]]>
This is how I'm trying to do:
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17);
webClient.getCookieManager().setCookiesEnabled(true);
webClient.setJavaScriptEnabled(true);
webClient.setThrowExceptionOnScriptError(false);
HtmlPage page = webClient.getPage("http://ask.fm/username");
List forms = page.getForms();
HtmlForm ask = forms.get(1);
HtmlSubmitInput button = ask.getInputByName("commit");
HtmlTextArea text = ask.getTextAreaByName("question[question_text]");
text.setText("question");
HtmlPage click = button.click();
webClient.closeAllWindows();
I got also a lot of warning while executing the program:
PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'text/javascript'.
This is what the post contains when submited in chrome:
authenticity_token:b9o8BHnVU6KDQ/oMsdQh+ClYPlS10134bpa7bFCkqtA=
question[question_text]:question
authenticity_token:b9o8BHnVU6KDQ/oMsdQh+ClYPlS10134bpa7bFCkqtA=
How do i get it worked? Any idea?
解决方案
You are doing it right, you are just not waiting enough for the submit to be processed. Add this after the HtmlPage click = button.click(); line:
webClient.waitForBackgroundJavaScript(10 * 1000);
It will wait 10 seconds. For a maybe nicer solution, you can try this one also.