CasperJS打开数组中的链接

时间:2016-10-21 06:42:29

标签: javascript arrays casperjs

我使用CasperJS刮取表格来收集姓名,地址,城市和网址。我已经能够收集信息并将其放入数组中但是在打开URL时遇到问题。我想打开匹配地址的URL(我解析地址以匹配街道名称并将URL推送到数组中)。我已将mLink设置为全局变量,但不确定是否需要在参数中传递它。此外,我已经测试了getUnclaimedAmount,并且该功能似乎正常工作。

我已尝试查看以下链接,但仍有问题: CasperJS - How to open up all links in an array of links

casper.then(function() {
    // Call functions and get name, address, city, URL into individual arrays
    names = this.evaluate(getName);
    addresses = this.evaluate(getAddress);
    cities = this.evaluate(getCity);
    links = this.evaluate(getLink);

    // Merge name, address, city, URL arrays into 'info' array.
    for(var i = 0, l = names.length; i < l; i++) {
        info[i] = {};
        info[i].Name = names[i];
        info[i].Address = addresses[i];
        info[i].City = cities[i];
        info[i].URLs = links[i];

        //Parse the address to find the street that matches user's input
        var parsed_address = info[i].Address.split(" ")

        //Matching street name will return the URL and append an array of URLs
        for(var ii = 0, plen = parsed_address.length; ii < plen; ii++){
            lowercase_name = parsed_address[ii].toLowerCase()
            if (lowercase_name == street){
                  mAddress.push(info[i].Address)
                  mLink.push(info[i].URLs)
            };
        };
    };

});

Casper.then(function() {
    //Open the URL for a matching address
    for(var i = 0, len = mLink.length; i < len; i++){
        casper.thenOpen(mLink[i]);

        //Get the unclaimed amount
        amount = this.evaluate(getUnclaimedAmount);
        this.echo('Unclaimed Amount: ' + amount);
  };
});

1 个答案:

答案 0 :(得分:0)

试试这个

casper.thenOpen(mLink[i]);

casper.then(function() {
        //Get the unclaimed amount
        amount = this.evaluate(getUnclaimedAmount);
        this.echo('Unclaimed Amount: ' + amount);
});

当您打开链接时,您还需要等待!