使用casperjs上传文件时遇到问题。以下是网站的链接:http://207.140.168.200/login.R 以下是HTML内容
<div id="collapse-file-upload" class="panel-collapse in" data-colwidth="2" style="height: auto;">
<img class="panel-shadow right" width="5px" height="105" src="/img/left_bordergray.png">
<div class="panel-body" style="height: 105px;">
<div id="file-upload-div" class="widget-vsize">
<div id="file-upload-wrapper">
<div id="file-upload-controls" class="btn-group-sm">
<input id="file" type="file" multiple="" style="display: inline;">
<span class="checkbox" style="display: inline-block; padding-top: 6px;">
<label>
<input id="upload-to-notebook" type="checkbox">
<span>Upload to notebook</span>
</label>
</span>
<button id="upload-submit" class="btn btn-default-ext" style="float: right;width: 33px;" type="submit">
<i class="icon-upload-alt"></i>
</button>
<div class="progress" style="display:none">
<div id="progress-bar" class="progress-bar" style="width: 0%" aria-valuemax="100" aria-valuemin="0" aria-valuenow="0" role="progressbar">
</div>
</div>
</div>
<div id="file-upload-results-row">
<div id="file-upload-results-scroller">
<div id="file-upload-results"> </div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
及以下是我用于自动化的casperjs代码
casper.then(function(){
casper.evaluate(function () {
var element = document.getElementById('file');
this.sendKeys(element, '/home/prateek/Download/Notebook 1.R');
this.wait(3000)
});
this.click({ type : 'css' , path : '#upload-submit'});
this.echo('file has been uploaded')
});
根本没有从那个位置选择文件。请注意我没有运行它无头,所以我可以看到正在进行的所有操作
答案 0 :(得分:0)
经过这么多个月后找到了解决方案并因此发布了解决方案。我知道回答我的问题很奇怪,但我发现解决方案因此向前发布并发布答案
其中'filename'是将要上传的文件的位置
casper.then(function () {
this.evaluate(function (fileName) {
__utils__.findOne('input[type="file"]').setAttribute('value', fileName)
}, {fileName: fileName});
this.page.uploadFile('input[type="file"]', fileName);
console.log('Selecting a file');
});
casper.then(function () {
casper.evaluate(function () {
$('#upload-to-notebook').click();
});
console.log("Clicking on Upload to notebook check box");
this.click(x(".//*[@id='upload-submit']"));
console.log("Clicking on Submit icon");
});