简体   繁体   中英

CasperJS - loop through an array of url

i'm trying to loop through an array of url with CasperJS and execute several steps.

var links = ['https://www.facebook.com/delivagri/inbox/?selected_item_id=1921693171204929',
'https://www.facebook.com/delivagri/inbox/?selected_item_id=1879523705421876'];

But when i'm using the evaluate function, the return variable is either null or is working only for the first URL :

    casper.start().each(links, function(self, link) {
        self.thenOpen(link, function() {
            var list = this.evaluate(function(){
                return document.getElementsByClassName("_50u0 _60p- _14hj")
            });
        console.log("This page contains :", list.length, " unanswered comments");
    });
});

Thanks for your answers.

I tried your code an it works fine for me, here is a bit expended version for you to try:

var casper = require('casper').create({
  logLevel: 'debug',
  verbose: true,
  viewportSize: {width: 1200, height: 1080 }
});

var links = [
  'https://www.facebook.com/delivagri/inbox/?selected_item_id=1921693171204929',
  'https://www.facebook.com/delivagri/inbox/?selected_item_id=1879523705421876'
];

casper
  .start()
  .each(links, function (self, link) {
    self.thenOpen(link, function () {
      var list = this.evaluate(function () {
        return document.getElementsByClassName("_50u0 _60p- _14hj")
      });
      console.log("This page contains :", list.length, " unanswered comments");
  });
})
.run();

And here is my output:

[info] [phantom] Starting...
[info] [phantom] Running suite: 4 steps
[debug] [phantom] opening url: https://www.facebook.com/delivagri/inbox/?selected_item_id=1921693171204929, HTTP GET
[debug] [phantom] Navigation requested: url=https://www.facebook.com/delivagri/inbox/?selected_item_id=1921693171204929, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://www.facebook.com/delivagri/inbox/?selected_item_id=1921693171204929"
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/4 https://www.facebook.com/delivagri/inbox/?selected_item_id=1921693171204929 (HTTP 200)
This page contains : 0  unanswered comments
[info] [phantom] Step anonymous 2/4: done in 2670ms.
[debug] [phantom] opening url: https://www.facebook.com/delivagri/inbox/?selected_item_id=1879523705421876, HTTP GET
[debug] [phantom] Navigation requested: url=https://www.facebook.com/delivagri/inbox/?selected_item_id=1879523705421876, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://www.facebook.com/delivagri/inbox/?selected_item_id=1879523705421876"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 4/4 https://www.facebook.com/delivagri/inbox/?selected_item_id=1879523705421876 (HTTP 200)
This page contains : 0  unanswered comments
[info] [phantom] Step anonymous 4/4: done in 3524ms.
[info] [phantom] Done 4 steps in 3547ms
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"

which obviously runs your code for both links in your array.

You can use each() to loop through each page and getElementsInfo() to retrieve information about all elements matching the provided selector.

This will allow you to avoid toggling between the CasperJS environment and the remote DOM environment. You can learn more about the different environments from the documentation .

Simple Solution:

var links = [
  'https://www.facebook.com/delivagri/inbox/?selected_item_id=1921693171204929',
  'https://www.facebook.com/delivagri/inbox/?selected_item_id=1879523705421876'
];

var casper = require('casper').create();

casper.each(links, function (self, link) {
  self.thenOpen(link);

  self.then(function () {
    var list = this.getElementsInfo('._50u0._60p-._14hj');

    this.echo('This page contains: ' + list.length + ' unanswered comments');
  });
});

casper.run();

Note: This method does not return a NodeList , only a simple array of object representations of matching elements; this is because the Casper environment and the page DOM environment are not the same, so DOM objects need to be serialized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM