我正在尝试使用 CasperJS 来自动执行一些通常需要大量时间才能完成的步骤。基本上我需要登录我们的 CMS 并检查是否安装了一些插件。如果是,则只需更新它们,但如果不是,则创建它们。我设法登录并进入包含插件列表的页面,但我在这里遇到了麻烦。这是我需要做的伪代码:
for every plugin defined in my config file
populate the search plugins form and submit it
evaluate if the response contains my plugin
这是代码
casper.thenOpen(snippetsUrl, function() {
console.log(colorizer.colorize("###############PROCESSING SNIPPETS###################", 'ERROR'));
this.capture('snippets.png');
this.each(Config.snippets.station, function(self, snippet) {
self.fill('form[id="changelist-search"]', {q: snippet.name}, true);
self.then(function() {
this.capture(snippet.name + '.png');
})
});
});
发生的情况是,我的表单连续提交多次,并且在我的“然后”步骤中,我最终多次捕获同一页面......如何解决此问题?
最佳答案
试试这个:
this.each(Config.snippets.station, function(self, snippet)
{
self.then(function()
{
this.fill('form[id="changelist-search"]', {q: snippet.name}, true);
});
self.then(function()
{
this.capture(snippet.name + '.png');
})
});
您的初始代码不起作用的原因是 Capser 的 then
声明了一个延迟执行步骤。如果展开,您的代码实际上执行了以下操作:
submit form 1
place capture 1 into a queue
submit form 2
place capture 2 into a queue
submit form 3
place capture 3 into a queue
// then execute the queue
capture 1
capture 2
caprure 3
生成的代码将所有步骤放入队列中,因此它们会按正确的顺序执行。
关于javascript - CasperJS 像 for 循环一样多次提交和评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13976876/