[英]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
函數參數是在瀏覽器上下文中而不是測試中執行的。
您將返回一個簡單的數組, then
在then
函數中創建Anime
實例。
有關更多信息,請參閱噩夢evaluate
文檔和此噩夢問題討論 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.