Javascript 如果没有使用CasperJS的表单,则无法填充输入元素

Javascript 如果没有使用CasperJS的表单,则无法填充输入元素,javascript,html,casperjs,Javascript,Html,Casperjs,使用CasperJS,我试图模拟一个用户操作,将xml文件导入到使用backbonejs编写的单页web应用程序中: 1) 用户单击“导入”,这是一个“a”html标记 fill()方法还有一个替代方法,称为evaluate() 是否尝试使用casper.fill函数?是的,我试过了,但没有效果。从doc:fill()函数中,它接受“name”属性引用的字段,并且我要填充的输入标记没有“name”属性。我明白了。我想你的问题更多的是我如何填写一个文件输入witch casper。你不能用JS填写

使用CasperJS,我试图模拟一个用户操作,将xml文件导入到使用backbonejs编写的单页web应用程序中:

1) 用户单击“导入”,这是一个“a”html标记


fill()方法还有一个替代方法,称为evaluate()


是否尝试使用casper.fill函数?是的,我试过了,但没有效果。从doc:fill()函数中,它接受“name”属性引用的字段,并且我要填充的输入标记没有“name”属性。我明白了。我想你的问题更多的是我如何填写一个文件输入witch casper。你不能用JS填写一个文件输入,但phantomjs有一个函数uploadFile。尝试时发生了什么?我尝试添加casper.then(function(){this.page.uploadFile('input#import file','file.xml');});在waitForResource调用之后,即使文件名不正确,日志中也没有发生任何事情。您将如何使用它
fill
是一个方便的函数,它在内部使用
evaluate
。我想您可以使用
evaluate
方法,而不需要存在表单元素,其中,
fill
方法要求表单元素是存在的,这是正确的。但是如何使用
评估
解决文件上载问题?你是对的,这对文件上载没有帮助。实际上,我是通过谷歌搜索登陆这里的,因为我自己正在寻找一种方法来填充输入元素而不需要表单元素,后来通过casperjs docs发现,
evaluate
可以做到这一点,但我没有意识到,上面的问题实际上是关于文件上传的。可能是问题的标题可以更新,以明确表示问题与文件上载有关。谢谢
<a id="import-button" href="#" class="pointer"><i class="glyphicon glyphicon-import"></i> Import</a>
<input type="file" id="import-file" style="display:none"/>
var casper = require('casper').create({
    verbose: true,
    logLevel: "debug"
});

var x = require('casper').selectXPath;

casper.on('remote.message', function(msg) {
    this.echo('remote message caught: ' + msg);
});

casper.start('http://try.activeeon.com/studio/', function() {
    this.echo("Page title: " + this.getTitle());
    this.viewport(1366, 768);
});

casper.waitForResource("gears.png", function() {
    this.echo('gears.png has been loaded.');        
});

casper.thenClick('a[id="import-button"]', function() {    

    this.wait(2000, function(){

       this.evaluate(function(){
            var inputElement = document.querySelector('input#import-file');        
            try {
                inputElement.setAttribute('value', 'file.xml');            
                inputElement.click();
            } catch(err) {
                console.log("---> oups " + err);
            }        
        });

        this.echo('-----------------------------------------');

        //this.page.uploadFile('input#import-file','file.xml'); 
        this.fill('input#import-file', {}, true);

        this.echo('-----------------------------------------');

        this.wait(2000, function() {

            this.capture('test-image.jpg', {
                top: 0,
                left: 0,
                width: 1366,
                height: 768
            });
        });
    });        
});

casper.run();