javascript - 如果服务器在一次执行后没有重置,PhantomJS 进程就会死亡

标签 javascript node.js phantomjs node-horseman

我的项目包含很少使用node-horseman的方法,但是每次服务器重新启动后,无论是什么方法,在我执行某些操作后,该进程都会终止。例如,我有一种方法可以检索 Google 快讯中的快讯列表:

* getRSSNames() {
  return new Promise((resolve, reject) => {
    horseman
      .userAgent(this.userAgent)
      .open('https://www.google.com/alerts')
      .evaluate(function() {
        var item = []

        jQuery('#manage-alerts-div li').each(function(key, val) {
          item.push({
            dataId: jQuery(val).attr('data-id'),
            name: jQuery(val).find('.query_div').text(),
            url: jQuery(val).find('a').attr('href')
          })
        })

        return item
      }).then(function(data) {
        resolve(data)
      })
      .close()
  })
}

如果我刚刚启动了本地服务器,则成功。然后,在 select 中呈现列表,选择一个并尝试将其删除:

* deleteRSSFeed() {
  let feedID = this.feedID.replace(/%3A/g, ':')

  return new Promise((resolve, reject) => {
    horseman
      .userAgent(this.userAgent)
      .open('https://google.com/alerts')
      .evaluate(function(selector) {
        return jQuery('[data-id="' + selector + '"]').find('.delete_button').click()
      }, feedID)
      .then(function(data) {
        resolve(data)
      })
      .close()
  })
}

这会导致错误:

Unhandled rejection HeadlessError: Phantom Process died
    at poll_func (/home/gabriel/Sites/co-report/api/node_modules/node-phantom-simple/node-phantom-simple.js:584:10)
    at /home/gabriel/Sites/co-report/api/node_modules/node-phantom-simple/node-phantom-simple.js:52:7
    at ClientRequest.<anonymous> (/home/gabriel/Sites/co-report/api/node_modules/node-phantom-simple/node-phantom-simple.js:501:11)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:188:7)
    at Socket.socketErrorListener (_http_client.js:308:9)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at emitErrorNT (net.js:1272:8)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

我还有一个登录Google的功能。如果我登录并在服务器运行的情况下刷新页面,因为我登录并刷新时将检索提要列表,这将导致相同的错误。请帮助我。

最佳答案

好吧...我的解决方案是在每个方法中实例化horseman。就是这样。

关于javascript - 如果服务器在一次执行后没有重置,PhantomJS 进程就会死亡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42063329/

相关文章:

javascript - c3js › 值 0 在 Y 和 Y2 之间对齐

javascript - 对于事件页面链接,我如何使用作悬停显示的 data-alt-src 图像变为事件状态

node.js - { 蒙戈超时错误 : Server selection timed out after 30000 ms

java - 在每次运行时为 PhantomJs 使用相同的 session

grails - Grails、Geb 和 PhantomJS 的功能测试不起作用

javascript - 调用数组原型(prototype)函数

javascript - 如何以及在何处将 JQuery 添加到 DotnetNuke(DNN) 的模块元素中?

node.js - Spotify Web Api - 开始/恢复用户的播放

json - 如何读取 json 文件并将条目保存到 mongodb

javascript - 使用 PhantomJS 的 jQuery 错误