为什么这段代码会导致使用phantom模块的Node.js挂起

Why does this code cause Node.js using the phantom module to hang

本文关键字:phantom 模块 挂起 js Node 段代码 代码 为什么      更新时间:2023-09-26

如果我更改此项:

var phantom = require('phantom');
phantom.create(function(ph) {
    return ph.createPage(function(page) {
        return page.open("http://www.google.com", function(status) {
            console.log("opened google? ", status);
            return page.evaluate((function() {
                return document.title;
            }), function(result) {
                console.log('Page title is ' + result);
                return ph.exit();
            });
        });
    });
});

到此:

 var phantom = require('phantom');
    phantom.create(function(ph) {
        return ph.createPage(function(page) {
            return page.open("http://www.google.com", function(status) {
                console.log("opened google? ", status);
                return page.get('title',(function(title) {
                    return title;
                }), function(result) {
                    console.log('Page title is ' + result);
                    return ph.exit();
                });
            });
        });
    });

打印"打开的谷歌?"后,节点挂在控制台上?成功",并且没有进一步的输出。

我正在尝试使用page.get(),而不是幻影模块文档中描述的page.evaluate:

不能直接获取/设置属性,请使用p.get('version',回调)

您滥用了page.get()。这个方法只有两个参数,而不是三个。

方法如下:

page.get('title', function(title) {
  console.log('Page title is ' + title);
  return ph.exit();
});