簡體   English   中英

Nightmare.js 在 Ubuntu Linux 雲服務器上沒有按預期工作

[英]Nightmare.js not working as expected on Ubuntu Linux cloud server

我似乎無法讓 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);
});

而且它仍然不起作用。

我該如何解決這個噩夢?

您的問題很可能由https://github.com/segmentio/nightmare/issues/224描述

Nightmare 使用需要 X 顯示的 Electron; 由於您的服務器沒有顯示器,您可以使用 Xvfb 提供虛擬顯示器。 安裝 xvfb,然后運行

xvfb-run node --harmony hello_nightmare.js

我只是為后代張貼這個。

下面是在干凈的 Ubuntu Linux 機器上使用節點 (4.2.4) 安裝 nightmarejs 的 bash 腳本。 我已經在運行 14.04 的 DigitalOcean Droplet 上對此進行了測試。

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

我希望這有幫助。

由於 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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM