CasperJS 从我的 for 循环中多次只发布最后一项

CasperJS posting only the last item multiple times from my for-loop

CasperJS 很棒,但它没有将控制台输出的内容发布到我的本地主机。

casper.wait(5000, function () {
    casper.wait(1000, function () {
        casper.then(function(){
            for  (var i = 0 ; i < 10; i++) {
                var description = casper.fetchText(x('//*[@id="acDataId-local'+i+'"]/a')); //*[@id="acDataId-local0"]/a
                console.log(description);
                var target_date = casper.fetchText(x('//*[@id="dtDataId-local'+i+'"]/text()[1]')); 
                console.log(target_date);
                var target_location = casper.fetchText(x('//*[@id="veDataId-local'+i+'"]')); 
                console.log(target_location);

                console.log(i, description)

                casper.then(function () {

                    casper.open('http://localhost:1337/events', {
                        method: 'post',
                            data:   {
                            'description': description,
                            'target_date':  target_date,
                            'target_location':  target_location,
                        },
                        headers: {
                           "stuff":"stuff"
                        }
                    });
                });
            }
            this.echo('POST ' + i );
        });
    });
});


casper.run();

Console.log 正好在我想要的时候输出,但它只发布了最后一项。我尝试在多个地方添加 casper.wait,但它似乎没有帮助!

CasperJS 中的所有 then*wait* 函数都是 and JavaScript has function-level scope.

这意味着 for 循环会立即执行,并计划在 for 循环完全完成后执行几个 then() 步骤。届时,函数级变量 descriptiontarget_date 等都将具有最后一个 i 的值,而 i 将为 10。这是一个通用的 JavaScript 例子:JavaScript closure inside loops – simple practical example

您可以

  • 将两个调用 casper.then()casper.open() 更改为单个调用 casper.thenOpen(),其中循环变量直接传递给函数:

    casper.thenOpen('http://localhost:1337/events', {
        method: 'post',
            data:   {
            'description': description,
            'target_date':  target_date,
            'target_location':  target_location,
        },
        headers: {
           "stuff":"stuff"
        }
    });
    
  • 或"close"通过引入IIFE每次迭代的变量:

    for  (var i = 0 ; i < 10; i++) {
        (function(){
            var description = casper.fetchText(x('//*[@id="acDataId-local'+i+'"]/a')); //*[@id="acDataId-local0"]/a
            console.log(description);
            var target_date = casper.fetchText(x('//*[@id="dtDataId-local'+i+'"]/text()[1]')); 
            console.log(target_date);
            var target_location = casper.fetchText(x('//*[@id="veDataId-local'+i+'"]')); 
            console.log(target_location);
    
    
            console.log(i, description)
    
    
            casper.then(function () {
    
                casper.open('http://localhost:1337/events', {
                    method: 'post',
                        data:   {
                        'description': description,
                        'target_date':  target_date,
                        'target_location':  target_location,
                    },
                    headers: {
                       "stuff":"stuff"
                    }
                });
            });
        })();
    }