簡體   English   中英

為什么我不能在Nightmare.evaluate()中使用我的課程?

[英]Why can't i use my class in Nightmare.evaluate()?

我正在嘗試使用Nightmare抓取有關動漫數據的網站,並正在使用evaluate函數在文檔中運行querySelectorAll 我想在evaluate函數中創建並返回一個Anime數組(非常簡單的類,只有兩個屬性)。

這是我的代碼:

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函數參數是在瀏覽器上下文中而不是測試中執行的。

您將返回一個簡單的數組, thenthen函數中創建Anime實例。

有關更多信息,請參閱噩夢evaluate文檔此噩夢問題討論

暫無
暫無

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

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