[英]Use CasperJS/Javascript to submit a form?
這是我的代碼片段:
this.sendKeys('.usertext.cloneable > div > div.md > textarea', "Message");
//...
this.evaluate(function(){
var entry = document.getElementsByClassName("usertext cloneable")[0].id;
document.getElementById(entry).removeAttribute("action")
document.querySelector('#'+entry).submit()
})
在使用sendKeys
填寫該表單后,應將此代碼提交在網站(Reddit.com)上找到的表單。 它完成了文本框的填寫(我可以通過casper.capture
截取屏幕截圖),但是不提交文本。 我最初認為這是因為CSS選擇器錯誤,但我不再確定。 似乎Casper也因為action='#'
屬性而沒有發送表單,因此在提交表單之前將其刪除。 我不知道下一步該怎么做。
以及形式:
<form action="#" class="usertext cloneable" onsubmit="return post_form(this, 'comment')" id="form-t3_3gp8yji5i">
<input type="hidden" name="thing_id" value="t3_3gp8yj">
<div class="usertext-edit md-container" style="">
<div class="md">
<textarea rows="1" cols="1" name="text" class=""></textarea>
</div>
<div class="bottom-area">
<span class="help-toggle toggle" style="">
<a class="option active " href="#" tabindex="100" onclick="return toggle(this, helpon, helpoff)">formatting help</a>
<a class="option " href="#">hide help</a>
</span>
<a href="/wiki/reddiquette" class="reddiquette" target="_blank" tabindex="100">reddiquette</a>
<span class="error TOO_LONG field-text" style="display:none"></span>
<span class="error RATELIMIT field-ratelimit" style="display:none"></span>
<span class="error NO_TEXT field-text" style="display:none"></span>
<span class="error TOO_OLD field-parent" style="display:none"></span>
<span class="error DELETED_COMMENT field-parent" style="display:none"></span>
<span class="error DELETED_LINK field-parent" style="display:none"></span>
<span class="error USER_BLOCKED field-parent" style="display:none"></span>
<div class="usertext-buttons">
<button type="submit" onclick="" class="save">save</button>
<button type="button" onclick="cancel_usertext(this)" class="cancel" style="display:none">cancel</button>
<span class="status"></span>
</div>
</div>
<div class="markhelp" style="display:none">
<p></p>
<p>reddit uses a slightly-customized version of <a href="http://daringfireball.net/projects/markdown/syntax">Markdown</a> for formatting. See below for some basics, or check <a href="/wiki/commenting">the commenting wiki page</a> for more detailed help and solutions to common issues.
</p>
以下是報告的錯誤:
Console: Unsafe JavaScript attempt to access frame with URL https://www.reddit.com/ from frame with URL https://static.adzerk.net/reddit/ads.html?sr=-reddit.com,loggedin&bust2#https://www.reddit.com. Domains, protocols and ports must match. Console: Unsafe JavaScript attempt to access frame with URL https://www.reddit.com/ from frame with URL https://static.adzerk.net/reddit/ads.html?sr=-reddit.com,loggedin&bust2#https://www.reddit.com. Domains, protocols and ports must match. Console: Unsafe JavaScript attempt to access frame with URL https://www.reddit.com/r/Pokemon_adventures/comments/3gp8yj/test_post_please_ignore/?thing_id=t3_3gp8yj&text=%22jnjrkefejkn%22 from frame with URL https://static.adzerk.net/reddit/ads.html?sr=pokemon_adventures,loggedin&bust2#https://www.reddit.com. Domains, protocols and ports must match. Console: Unsafe JavaScript attempt to access frame with URL https://www.reddit.com/r/Pokemon_adventures/comments/3gp8yj/test_post_please_ignore/?thing_id=t3_3gp8yj&text=%22jnjrkefejkn%22 from frame with URL https://static.adzerk.net/reddit/ads.html?sr=pokemon_adventures,loggedin&bust2#https://www.reddit.com. Domains, protocols and ports must match. Console: Unsafe JavaScript attempt to access frame with URL https://www.reddit.com/r/Pokemon_adventures/comments/3gp8yj/test_post_please_ignore/?thing_id=t3_3gp8yj&text=Message from frame with URL https://static.adzerk.net/reddit/ads.html?sr=pokemon_adventures,loggedin&bust2#https://www.reddit.com. Domains, protocols and ports must match. Console: Unsafe JavaScript attempt to access frame with URL https://www.reddit.com/r/Pokemon_adventures/comments/3gp8yj/test_post_please_ignore/?thing_id=t3_3gp8yj&text=Message from frame with URL https://static.adzerk.net/reddit/ads.html?sr=pokemon_adventures,loggedin&bust2#https://www.reddit.com. Domains, protocols and ports must match.
由於您收到的錯誤與表單提交沒有任何關系,因此您可以嘗試以下操作:
使用CasperJS工具提交表單。 采用
this.fill(".usertext.cloneable", {}, true);
而不是this.evaluate(...);
。
使用CasperJS工具單擊一個按鈕:
this.click(".usertext.cloneable button[type='submit']");
如果那沒有幫助,那么您可以使用--web-security=false --ssl-protocol=any --ignore-ssl-errors=true
命令行選項運行CasperJS,嘗試相同的操作。
表單中的action="#"
看起來可疑,因此在再次嘗試之前將其刪除。
如果您無法更改HTML,則可以在提交表單之前使用casper.js將其刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.