gpt4 book ai didi

java - HTMLUnit 使用 JavaScript 代码执行表单

转载 作者:太空宇宙 更新时间:2023-11-04 07:39:50 27 4
gpt4 key购买 nike

我想做的就是使用 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 ♣</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 notify WARNING: Obsolete content type encountered: 'text/javascript'.

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

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

我怎样才能让它发挥作用?有什么想法吗?

最佳答案

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

webClient.waitForBackgroundJavaScript(10 * 1000);

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

关于java - HTMLUnit 使用 JavaScript 代码执行表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16242365/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com