2015-08-26 69 views
0

這裏是我的代碼片段:使用CasperJS/Javascript提交表單?

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選擇器,但我不再確定。這似乎也是卡斯帕因爲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. 
+0

請正確格式化您的標記。另外,我沒有看到您的腳本以及您如何驗證沒有任何事情發生。你正在截圖嗎?還是你正在看這些請求? –

+0

@Artjom B.通過casper.capture使用屏幕截圖。如果帖子確實通過,我也可以看到腳本的結果(在Reddit.com上)。現在修復標記。 –

+1

請註冊到'resource.error','page.error','remote.message'和'casper.page.onResourceTimeout'事件([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf) )。也許有錯誤。 –

回答

2

因爲,你有沒有什麼做的表單提交,你可以試試下面的事情錯誤:

  • 使用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同樣的事情。

+0

casper.fill方法奏效。謝謝。 –

1

action="#"形式看起來可疑,所以我會重試前將其刪除。

如果您無法更改HTML,可以在提交表單之前使用casper.js將其刪除。

+0

認爲你可以詳細闡述一點點? –

+1

因爲'action =#',Casper.js沒有發送表單,因此在提交表單之前可能需要刪除該屬性(操作) – Filype

+0

嘗試使用以下代碼刪除操作,但問題仍然存在。表格不會提交。 'casper.evaluate(函數(){ MESSAGEID = 「形狀t3_3gp8yji5i」 的document.getElementById(MESSAGEID).removeAttribute( 「動作」) document.querySelector( '#' + MESSAGEID).submit() })' –