javascript - CasperJS 像 for 循环一样多次提交和评估

标签 javascript webkit phantomjs casperjs headless-browser

我正在尝试使用 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/

相关文章:

selenium-webdriver - JRuby Watir phantomjs 异常未知错误(Windows 说 "The operation completed successfully.",但它没有。)

javascript - 如何声明一个全局函数来从浏览器控制台调用 Angular 服务?

javascript - 如何检查 Kendo UI ComboBox 是否脏(即值已从默认值更改)?

javascript - 使用 CasperJS 循环等待选择器

javascript - iphone/ipad 杂志框架、iScroll 或更新版本?

javascript - 在 Webkit 浏览器中使用 jQuery 动画后,缩放的透明 PNG 会失去抗锯齿功能

javascript - 为什么 phantomjs 代码不通过数组?

javascript - Express.js 4 : How to access app. locals.<myvar> 在routes/index.js 中?

javascript - 将 Canvas 的一部分裁剪到另一个 Canvas 对象中

iphone - D3.js 管理 Webkit translate3d : Is that possible?