【发布时间】:2014-05-15 21:28:45
【问题描述】:
我是 CasperJS 的新手,我正在尝试弄清楚执行流程。
这就是我想要实现的目标:
加载页面
存储页面的图像
将此图像传递给函数并执行它(此过程相当长:~15 秒)
等待函数返回结果
在加载页面中使用返回值填写表单中的一个字段
提交表格
这是一个代码 sn-p,它试图解释我想出的解决方案:
var globProcessedImage;
var casper = require('casper').create({
viewportSize: {
width: 1024,
height: 768
}
});
casper.start('http://example.com/');
casper.then(function() {
this.captureSelector('./image.png', '#img-node');
});
casper.waitFor(function() {
return globProcessedImage !== undefined;
}, function then() {
this.sendKeys('#imagePassword', globProcessedImage);
});
casper.then(function() {
this.capture('./page.png');
});
casper.run();
casper.on('image.processed', function() {
setTimeout(function() {
globProcessedImage = 'my_result';
}, 15000);
});
这导致ReferenceError: Can't find variable: globProcessedImage。
我仍然不清楚 Web 自动化和“外部”功能如何与 CasperJS 混合在一起,以及参数如何在页面和 casper/phantom 环境之间传递。
【问题讨论】:
-
emit不是这样运作的。emit触发回调您应该以同步方式执行 longProcess。你用casper.wait(20000)半同步试过了吗? -
我想要实现的实际上非常简单:1)开始网络自动化 2)暂停自动化并调用传递参数的外部函数 3)从函数中检索结果 4)使用结果恢复表单填充。我应该把
casper.wait放在哪里?如果外部进程耗时超过 20000ms 怎么办?
标签: javascript casperjs webautomation