Javascript phantomjs page.evaluate未在数组中返回预期值
以下内容打印到屏幕{b:['test']}。“a”数组中没有打印的值 为什么“a”不会添加任何项目,而“b”会添加?当它打印b中每个项目的outerhtml时,我可以看到它们具有“href”属性Javascript phantomjs page.evaluate未在数组中返回预期值,javascript,jquery,node.js,phantomjs,Javascript,Jquery,Node.js,Phantomjs,以下内容打印到屏幕{b:['test']}。“a”数组中没有打印的值 为什么“a”不会添加任何项目,而“b”会添加?当它打印b中每个项目的outerhtml时,我可以看到它们具有“href”属性 phantom.create(function(ph) { return ph.createPage(function(page) { return page.open(url, function(status) { console.log("opened " + url + "
phantom.create(function(ph) {
return ph.createPage(function(page) {
return page.open(url, function(status) {
console.log("opened " + url + "? ", status);
if (status === "fail") {
ph.exit();
return;
}
page.injectJs('jquery.min.js', function() {
console.log("loaded jquery");
setTimeout(function() {
return page.evaluate(function() {
var a = [],
b = [];
$("a").each(function() {
a.push($(this).attr("href"));
b.push($(this)[0].outerHTML);
});
return {
a: a,
b: b
};
}, function(result) {
console.log(result);
ph.exit();
});
}, 3000);
});
});
});
});
您需要提供更多的上下文。您对PhantomJS API的使用不是我所期望的。你用的是什么版本?例如:在我的版本(1.9.1)中,没有
phantom.create()
方法。类似地,在我的版本中,injectJs
不接受回调参数,它返回一个布尔值。我使用nodejs桥接到phantomjs,我认为这就是为什么这些方法都是异步的。我正在使用的桥位于,请注册到和事件。也许有错误。href
属性是否存在?当我使用'a.push(''+$(this.attr(“href”);'时,它打印每个href属性。不确定为什么在$(this.attr('href')前加一个空字符串有效?