CasperJS waitForSelector 不会将 "then" 添加到步骤数组

CasperJS waitForSelector doesn't add "then" to step array

我的代码是这样的:

var firstFrameLoadingTime = 3000;

firstFrameLoadingWaiter(function() {
    casper.echo("callback");
});

function firstFrameLoadingWaiter(callback) {
    casper.waitForSelector('div',
        function suc() {
            casper.echo('success!');
        },
        function timeout() {        
                casper.echo('failure!');
        },
        firstFrameLoadingTime);
}

问题是 suc 函数从未被调用。我的意思是它没有添加到 CasperJS 步骤的数组中。

这是日志的一部分:

[18] => [info] [phantom] Step _step 5/5 https://... (HTTP 200)
[19] => [info] [phantom] Step _step 5/5: done in 3392ms.
[20] => [info] [phantom] waitFor() finished in 40ms.
[21] => [info] [phantom] Done 5 steps in 3451ms

如果在超时之前未在页面上找到选择器,脚本会非常有效。

UPD。事实证明问题出在 do_whilewaitFor 不兼容。

经过一段时间的研究,我发现问题出在 do_whilestep.then 函数的修改中。 do_whilewaitFor 函数不兼容。

解决方法 (pull request) 很简单,但不是最清楚的。 我刚刚以一种特殊的方式命名了该函数并添加了一个小检查:

var isWaitSuccessFun = step.name.indexOf('successThen') != -1;
if( isWaitSuccessFun || !this.steps[this.current].executed ) {

要使您的 waitFor 正常工作,只需确保成功函数名称包含 successThen,例如:

casper.waitFor(
    function check(){
        // check if page is ready
    },
    function successThen(){
        // execute after page is ready
    },
    function(){
        // time out happened before page got ready
    },
    timeOutTime
);

这也使得 waitForSelector 与其他基于 waitFor 的类似函数一样工作。

完整代码可以在我的 forked repository.

中找到