HTMLUnit executing form with javascript code

2020-04-23 06:02发布

问题:

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 :

  <form action="/userame/questions/create" autocomplete="off" id="question_form" method="post" onsubmit="$.ajax({data:$.param($(this).serializeArray()) + '&amp;authenticity_token=' + encodeURIComponent('b9o8BHnVU6KDQ/oMsdQh+ClYPlS10134bpa7bFCkqtA='), dataType:'script', type:'post', url:'/username/questions/create'}); Forms.Profile.afterSubmit(); return false;" style="display:block"><div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="b9o8BHnVU6KDQ/oMsdQh+ClYPlS10134bpa7bFCkqtA=" /></div>

    <div id="profile-title">
      <div class="profile-title-text">
        <span class="text-headline" dir="ltr"><span dir="ltr">KILO K ♣</span></span>
      </div>
    </div>

    <div id="postLoaderTerritory">
      <textarea class="composeQuestion-form growable-textarea" id="profile-input" name="question[question_text]"></textarea>
      <div id="postLoader"></div>
    </div>

    <div id="post_options-border">
      <div id="post_options">



        <div id="generalLevel">
          <div class="profile-title-counter" id="question_counter_span"></div>

          <input class="submitBlue submitBlue-active" id="question_submit" name="commit" onclick="RLTME.rec('profile_q_send_click');return Forms.Profile.allowSubmit(this)" type="submit" value="Ask" />


        </div>

      </div>
    </div>


        <script type="text/javascript">
//<![CDATA[

            window.setTimeout(function(){try{$("textarea#profile-input").focus();}catch(e){}},10);

//]]>
</script>




  </form>

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<HtmlForm> 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?

回答1:

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.