1

我想做的就是使用 java nad HTMLUnit 框架在 ask.fm 上远程提问。我认为由于 javascript 代码它不起作用,但我不确定。

这就是提问表格的样子:

  <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 ♣&lt;/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>

这就是我正在尝试做的事情:

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();

我在执行程序时也收到了很多警告:

PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl 通知警告:遇到过时的内容类型:'text/javascript'。

这是在 chrome 中提交时帖子包含的内容:

authenticity_token:b9o8BHnVU6KDQ/oMsdQh+ClYPlS10134bpa7bFCkqtA=
question[question_text]:question
authenticity_token:b9o8BHnVU6KDQ/oMsdQh+ClYPlS10134bpa7bFCkqtA=

我如何让它发挥作用?任何想法?

4

1 回答 1

1

你做得对,你只是没有等待足够的时间来处理提交。在行后添加HtmlPage click = button.click();

webClient.waitForBackgroundJavaScript(10 * 1000);

它将等待 10 秒。对于一个可能更好的解决方案,您也可以尝试这个

于 2013-04-28T01:18:55.493 回答