我尝试了你的代码,它对我来说很好,这里有一个有点扩展的版本供你尝试:
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();
这是我的输出:
[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"
这显然会为您的数组中的两个链接运行您的代码。