PhantomJS - page.evaluate 不是函数

PhantomJS - page.evaluate is not a function

var phantom = require('phantom');

phantom.create()
        .then(function (ph) {
            _ph = ph;
            return ph.createPage();
        })
        .then(function(page) {
            _page = page;
            url = "http://www.aeiou.pt";
            return page.open(url);
        })
        .then(function(page) {

        console.log("hello3");
            page.evaluate(function () {

我的代码以这样的开头。 console.log "hello3" 被打印出来,但随后出现错误:

TypeError: page.evaluate is not a function at /home/someone/server123.js:58:11 at at process._tickCallback (internal/process/next_tick.js:188:7)

为什么会出现这种情况?

节点版本:v8.6.0

Npm 版本:5.3.0

幻影版:phantom@4.0.5

您遇到的问题是 page.open() 没有 return 页面 -- 它 return 是状态。因此,传递给下一个 then() 的值是状态,您尝试对其调用 evaluate 。这当然不行。

他们在 example 中处理此问题的方式是在 then() 链之外设置一个页面变量,他们可以在每个 then() 链中访问该页面变量。你几乎是用 _page = page; 如果 _page 是在函数外定义的,你应该能够调用 _page.evaluate() 而不是从 [=18 的 return 值调用它=].

var phantom = require('phantom');
var _page;

phantom.create()
    .then(function (ph) {
        _ph = ph;
        return ph.createPage();
    })
    .then(function(page) {
        _page = page;
        url = "http://www.aeiou.pt";
        return page.open(url);
    })
    .then(function(status) {
        // check status for errors here
        console.log("hello3");
        _page.evaluate(function () {