评估函数返回的值应该是json可序列化的。https://github.com/GoogleChrome/puppeteer/issues/303#issuecomment-322919968
解决方案是从元素中提取href值并将其返回。
await this.page.evaluate((sel) => {let elements = Array.from(document.querySelectorAll(sel));let links = elements.map(element => { return element.href})return links; }, sel);解决方法
我正在尝试木偶戏。这是您可以在其上运行的示例代码:https : //try-puppeteer.appspot.com/
问题是此代码返回一个空对象数组:
[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{} ,{},{},{},{},{},{},{}]
我在弄错吗?
const browser = await puppeteer.launch();const page = await browser.newPage();await page.goto(’https://reddit.com/’);let list = await page.evaluate(() => { return Promise.resolve(Array.from(document.querySelectorAll(’.title’)));});console.log(JSON.stringify(list))await browser.close();