javascript - 为什么我不能在Nightmare.evaluate()中使用我的类(class)?

标签 javascript node.js nightmare

我正在尝试使用Nightmare抓取有关动漫的数据的网站,并正在使用evaluate函数在文档中运行querySelectorAll。我想在Anime函数中创建并返回一个evaluate数组(非常简单的类,只有两个属性)。

这是我的代码:

const Nightmare = require('nightmare')

const URL = 'https://horriblesubs.info/shows/'

const browser = Nightmare({
    show: true
})

class Anime {
    constructor(name, url) {
        this.name = name
        this.url = url
    }
}

browser
    .goto(URL)
    .evaluate(() => {
        let shows = document.querySelectorAll('.ind-show')
        var array = new Array()
        shows.forEach((e, i) => {
            array.push(
                new Anime(e.textContent, e.firstChild.href)
            )
        })
        return array
    })
    .then(shows => {
        console.log(shows)
    })

不幸的是,NodeJS告诉我没有定义Anime类。我该如何解决它或获得相同的结果?

最佳答案

evaluate函数参数在浏览器上下文中执行,而不是在测试中执行。

您将返回一个简单的数组,并在Anime函数中创建then实例。

有关更多信息,请参见Nightmare evaluate docsthis Nightmare issue discussion

关于javascript - 为什么我不能在Nightmare.evaluate()中使用我的类(class)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57978832/

相关文章:

node.js - NightmareJS 截图回调

javascript - 模拟水平行内 block 元素,但元素高度可以绕过它们的行内高度

node.js - Estimote 贴纸 + Node.js + Raspberry-Pi

javascript - 如何安排谷歌 session 并在 NodeJs 中获取 session 链接?

javascript - 等待深度函数调用的深度异步等待结果?

mocha.js - 在执行 Nightmare 测试期间如何在多个位置评估页面?

javascript - 在浏览器重新加载/刷新/关闭警报,然后 ajax

javascript - 从顶部滚动后更改元素样式

JavaScript 无法在我机器上的任何浏览器上运行

javascript - Nightmare.js 在 Ubuntu Linux 云服务器上没有按预期工作