我正在 puppeteer 中制作一个网页抓取程序,该程序应该从网站收集文本。 我创建此类是为了从“#identifierId > option:nth-child(1)”收集文本并将其存储为对象属性,但它返回一个未定义的值:我缺少什么?
async getText () {
await this.page.waitForSelector('#identifierId> option:nth-child(1)');
this.findText = await this.page.evaluate(() => {
this.text = this.document.querySelector('#identifierId> option:nth-child(1)').innerText
return this.text
})
this.ArrayObject[0].text = this.findText.text
}
最佳答案
请尝试:
async getText () {
await this.page.waitForSelector('#identifierId> option:nth-child(1)');
this.findText = await this.page.evaluate(() => {
this.text = this.document.querySelector('#identifierId> option:nth-child(1)').innerText
return this.text
})
this.ArrayObject[0].text = this.findText
}
说明 - 基本上,在你的 this.findText 函数中,你实际上已经返回了 this.text ,这意味着 this.findText 实际上已经是你试图设置 this.ArrayObject[0].text 的值。
关于javascript - Document.querySelector() 返回未定义的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59377771/