噩梦JS不工作

Nightmare JS not working

本文关键字:工作 JS 噩梦      更新时间:2023-09-26

我知道问题的标题看起来很模糊!但就是这样。

我在我的生产服务器上安装了nodejs,它让phantomjs正常工作,然后我通过npm install nightmare安装了梦魇,我可以在node_modules中看到它,我尝试了github上开发者列出的例子:

var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true })
nightmare
  .goto('http://yahoo.com')
  .type('input[title="Search"]', 'github nightmare')
  .click('#uh-search-button')
  .wait('#main')
  .evaluate(function () {
    return document.querySelector('#main .searchCenterMiddle li a').href
  })
  .end()
  .then(function (result) {
    console.log(result)
  })

什么都没有发生,脚本没有输出任何内容,我将脚本简化为一个简单的goto,对于我的服务器上的一个页面,当我通过node file.js 运行脚本时,从未调用过该页面

我有CentOS 6.7,phantomjs 1.1我还用最新版本的phantomjs在一个新的centos7安装上测试了它,也是一样。

我是不是错过了某种先决条件?由于node script.js没有给出任何输出,我如何调试该问题

更新:显然问题是,梦魇"而不是phantomjs"使用的electron需要一个图形环境,这就是它无法在我的环境中运行的原因

新版梦魇需要electron,而不是PhantomsJs。确保electron命令在$PATH变量中。

安装Electron

npm i -g electron-prebuilt

要调试:

DEBUG=nightmare* node script.js

看看这个Dockerfile:https://github.com/aheuermann/docker-electron/blob/master/7/Dockerfile

这是您需要的最低限度的libs。开始你的脚本:

Xvfb -ac -screen scrn 1280x2000x24 :9.0 &
export DISPLAY=:9.0
DEBUG=* node src/index.js

基于电子的应用程序不应该再崩溃

您也可以尝试在后台设置electron,而不实际显示任何GUI。您检查这是否有效:

var nightmare = Nightmare({ show: false});