2015-08-26 66 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() })' –