【问题标题】:Nightmare.js not working as expected on Ubuntu Linux cloud serverNightmare.js 在 Ubuntu Linux 云服务器上无法正常工作
【发布时间】:2016-01-14 16:44:19
【问题描述】:

我似乎无法让 nightmare.js 在 Ubuntu Linux 14.04 服务器上工作 [通过 DigitalOcean]。

我已经安装了 PhantomJS (1.9.8) 和 Node (4.2.4),据我所知它们运行良好。

例如,当我运行这个时:

phantomjs loadspeed.js http://www.yahoo.com

使用包含此内容的 loadspeed.js:

"use strict";
var page = require('webpage').create(),
    system = require('system'),
    t, address;

if (system.args.length === 1) {
    console.log('Usage: loadspeed.js <some URL>');
    phantom.exit(1);
} else {
    t = Date.now();
    address = system.args[1];
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('FAIL to load the address');
        } else {
            t = Date.now() - t;
            console.log('Page title is ' + page.evaluate(function () {
                return document.title;
            }));
            console.log('Loading time ' + t + ' msec');
        }
        phantom.exit();
    });
}

我得到以下输出:

Page title is Yahoo
Loading time 700 msec

但是,当我尝试运行一个简单的噩梦时:

node --harmony hello_nightmare.js

hello_nightmare.js 包含以下内容:

var Nightmare = require('nightmare');

var google = new Nightmare()
  .goto('http://google.com')
  .wait()
  .run(function(err, nightmare) {
    if (err) return console.log(err);
    console.log('Done!');
  });

我没有得到任何输出;感觉就像我只是在命令行上按了“Enter”。

我还在噩梦 github 网站上尝试了这个例子:

npm install nightmare vo
node --harmony hello_nightmare_main.js

使用 hello_nightmare_main.js 包含以下内容:

var Nightmare = require('nightmare');
var vo = require('vo');

vo(function* () {
  var nightmare = Nightmare({ show: true });
  var link = yield nightmare
    .goto('http://yahoo.com')
    .type('input[title="Search"]', 'github nightmare')
    .click('.searchsubmit')
    .wait('.ac-21th')
    .evaluate(function () {
      return document.getElementsByClassName('ac-21th')[0].href;
    });
  yield nightmare.end();
  return link;
})(function (err, result) {
  if (err) return console.log(err);
  console.log(result);
});

还是不行。

我该如何解决这个噩梦?

【问题讨论】:

  • 截至 2017 年 10 月,使用 chromeless 似乎是最好的选择(相同的 API / 运行 headless-chrome)

标签: javascript linux node.js ubuntu nightmare


【解决方案1】:

您的问题很可能是由 https://github.com/segmentio/nightmare/issues/224

Nightmare 使用需要 X 显示器的 Electron;由于您的服务器没有显示器,您可以使用 Xvfb 提供一个虚拟的。安装 xvfb,然后运行

xvfb-run node --harmony hello_nightmare.js

【讨论】:

  • 谢谢。我刚刚运行了这个apt-get install -y xvfb x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic x11-apps clang libdbus-1-dev libgtk2.0-dev libnotify-dev libgnome-keyring-dev libgconf2-dev libasound2-dev libcap-dev libcups2-dev libxtst-dev libxss1 libnss3-dev gcc-multilib g++-multilib,然后尝试了 xvfb 命令,但我也没有得到任何输出。
  • 我发现为了调试它,你可以运行node_modules/nightmare/node_modules/electron-prebuilt/dist/electron --help 来查看 Electron 是否正在启动(你应该得到一条帮助消息;如果它什么也没打印,那么它就失败了,很可能是缺乏显示)。使用xvfb-run 运行该命令是否有任何输出?
  • 好的。所以我再次运行了 apt-get,这一次移除了 -y 开关并手动执行所有操作,因为我注意到一些与使用 -y 相关的错误。之后我再次运行 xvfb ,它似乎正在工作。我将创建一个新的 droplet 并再次尝试(在安装 xvfb 时使用 -y --force-yes)以确保一切顺利。干杯。
  • 我相信这些包中,只有 xvfb 是真正需要的(apt-get 会引入它的所有依赖项)。您可能仍然会收到一些关于缺少字体的警告,但 xvfb 会运行得很好。祝你好运。
  • 在这里我在想为什么它昨天没有在我的树莓派上运行在 Arch linux 上。我认为这只是因为电子还不支持 ARM,但它确实成功下载了二进制文件。感谢 @yoz 和 Obinwanne Hill 澄清这个问题。
【解决方案2】:

我只是为了后代发布这个。

以下是在干净的 Ubuntu Linux 机器上使用 node (4.2.4) 安装 nightmarejs 的 bash 脚本。我已经在运行 14.04 的 DigitalOcean 液滴上对此进行了测试。

apt-get -y update
apt-get -y upgrade
apt-get -y --force-yes install make unzip g++ libssl-dev git xvfb x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic x11-apps clang libdbus-1-dev libgtk2.0-dev libnotify-dev libgnome-keyring-dev libgconf2-dev libasound2-dev libcap-dev libcups2-dev libxtst-dev libxss1 libnss3-dev gcc-multilib g++-multilib
mkdir src
cd src
wget https://nodejs.org/dist/v4.2.4/node-v4.2.4.tar.gz
tar xzf node-v4.2.4.tar.gz
cd node-v4.2.4
./configure
make -j2
make install
cd ..
mkdir nightmarejs
cd nightmarejs
npm -f init
npm install --save nightmare vo

然后您只需创建 .js 文件(例如 hello_nightmare.js)(在安装 nightmarejs 的同一目录中),然后使用以下命令运行它(如 @yoz 的回答中已经提到的):

xvfb-run node --harmony hello_nightmare.js

我希望这会有所帮助。

【讨论】:

  • 谢谢朋友,确实很有帮助。
  • 谢谢,我的噩梦脚本已经运行了,但是你知道如何让我的节点项目使用 PM2 运行吗?
  • @loveNZ 使用 PM2 的时候有点复杂。本质上,您想创建一个简单的 API(使用 ExpressJS 之类的东西进行路由),其中路由链接到 JS 文件中的函数,然后创建一个对 Nightmare 执行某些操作的函数,然后使用 PM2 运行节点应用程序。这意味着您的脚本现在可以从 HTTP 请求而不是命令行激活。我建议您探索使用 HorsemanJS(这是我现在更喜欢的)而不是 Nightmare。
  • 干杯,我最终使用了 node-xvfb。所以,只需要在我的 nightmarejs 代码之前执行 xvfb.startSync(),在 nightmarejs 完成之后执行 xvfb.stopSync()。
  • 似乎单独安装 xvfb 并不能解决问题 - 添加其他软件包使其工作但增加了 1.2 Gb
【解决方案3】:

由于 electron 需要 X 显示器,您需要安装以下所有软件包

sudo apt-get install -y xvfb x11-xkb-utils xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic x11-apps clang libdbus-1-dev libgtk2.0-dev libnotify-dev libgnome-keyring-dev libgconf2-dev libasound2-dev libcap-dev libcups2-dev libxtst-dev libxss1 libnss3-dev gcc-multilib g++-multilib

aws ec2 的 ubuntu 服务器中测试,它工作正常

然后运行你的脚本:

xvfb-run node --harmony script.js

【讨论】:

    【解决方案4】:

    Nightmare.js 使用 Electron 浏览器并且需要 X 服务器。安装 xvfb 及其依赖项,以便您可以在没有显示硬件的情况下运行图形应用程序:

    sudo apt-get install -y xvfb \
    x11-xkb-utils \
    xfonts-100dpi \
    xfonts-75dpi \
    xfonts-scalable \
    xfonts-cyrillic \
    x11-apps \
    clang libdbus-1-dev \
    libgtk2.0-dev libnotify-dev \
    libgconf2-dev \
    libasound2-dev libcap-dev \
    libcups2-dev libxtst-dev \
    libxss1 libnss3-dev \
    gcc-multilib g++-multilib
    

    创建nightmare.js 文件并添加以下内容:

    const Nightmare = require("nightmare");    
    const outputFile = "test.png";
    
    const nightmare = Nightmare({ show: true })
    
    nightmare
      .goto('https://duckduckgo.com')
      .type('#search_form_input_homepage', 'github nightmare')
      .click('#search_button_homepage')
      .wait('#r1-0 a.result__a')
      .evaluate(() => document.querySelector('#r1-0 a.result__a').href)
      .end()
      .then(res => {
        console.log(res)
      })
      .catch(error => {
        console.error('Search failed:', error)
      })
    

    运行脚本:

    $ xvfb-run node --harmony nightmare.js
    // output: https://github.com/segmentio/nightmare
    

    【讨论】:

    • 谢谢!这仅适用于我,ubuntu 20.04,AWS
    猜你喜欢
    • 1970-01-01
    • 2017-06-02
    • 2012-08-02
    • 1970-01-01
    • 2012-11-03
    • 2011-02-27
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多