CasperJS表单提交不起作用

时间:2014-10-02 19:49:47

标签: javascript casperjs

我想将文件上传到本地网站,问题是我可以上传但无法提交。 我上传后上传截图,但我无法提交。 html表单和CasperJs代码如下所述。

HTML表单:

<form method="post" action="http://127.0.0.1/" enctype="multipart/form-data">
    <input type="file"   name="pict">
    &nbsp;Name:&nbsp;(optional)&nbsp;<input type="text"   name="image_platform">
    <input type="submit" value="Send">
</form>

用于上传图片的CasperJS代码:

casper.then(function(){
      this.evaluate(function(fileName) {__utils__.findOne('input[type="file"]').setAttribute('value',fileName)},{fileName:fileName});
      this.echo('Name='+this.evaluate(function() {return __utils__.findOne('input[type="file"]').getAttribute('name')}));
      this.echo('Value='+this.evaluate(function() {return __utils__.findOne('input[type="file"]').getAttribute('value')}));
      this.page.uploadFile('input[type="file"]',fileName);
      this.wait(1000, function() {
            this.click(x('//html/body/form/input[3]'));
      });
});

用于提交不起作用的表格的CasperJS代码:

casper.thenEvaluate(function() {
    document.forms[0].submit();
});

casper.then(function(){
    casper.click(x('/html/body/form/input[3]'));
});

这是HTML:

<img src="logo.jpg">
<h1>Program located on server test file</h1>
<p>This test will ensure that Program located on server is working correctly</p>
<p>First, make sure Program located on server is running and at least one image type is selected (see below)</p>
<p>Make sure that you have use host file redirect selected from the settings menu.</p>
<p>Click 'Browse' or 'Choose File' and navigate to \lalalanomeaning\file</p>
<p>Then select any type&lt;XX&gt; folder and select image.gif</p>
<p>Then click 'Send'</p>
<p>You should receive a response like this:</p>
<p>200|593430|1539|0|0|&lt;answer here&gt;</p>
<form 
 method="post" 
 action="http://127.0.0.1/" 
 enctype="multipart/form-data">
 <input type="file"   name="pict">
 &nbsp;Name:&nbsp;(optional)&nbsp;<input type="text"   name="image_platform">
 <input type="submit" value="Send">
</form>

这是CasperJS代码:

var casper = require('casper').create();
var x = require('casper').selectXPath;
casper.userAgent('Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36')
casper.start('file:///C:/Users/myname/Desktop/cs/test.html', function() {
    this.echo(this.getTitle());
}).viewport(1200,1000);

//casper.then(function(){
//  casper.click(x('/html/body/form/input[1]'));
//});

var webPage = require('webpage');
var page = webPage.create();
var fileName ='C:\\Users\\myname\\Desktop\\cs\\dm.png';

casper.then(function(){
  this.evaluate(function(fileName) {__utils__.findOne('input[type="file"]').setAttribute('value',fileName)},{fileName:fileName});
  this.echo('Name='+this.evaluate(function() {return __utils__.findOne('input[type="file"]').getAttribute('name')}));
  this.echo('Value='+this.evaluate(function() {return __utils__.findOne('input[type="file"]').getAttribute('value')}));
  this.page.uploadFile('input[type="file"]',fileName);
  this.wait(1000, function() {
        this.click(x('//html/body/form/input[3]'));
    });
});

//casper.thenEvaluate(function() {
//    document.forms[0].submit();
//});

casper.wait(7000, function(){
    casper.capture('caca.png');
});
casper.run();

我无法上传图片,但我会尽力解释。

图片正在上传...

我无法通过点击或通过javascript提交表单。

0 个答案:

没有答案