javascript Puppeteer page.evaluate querySelectorAll 返回空对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46377955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Puppeteer page.evaluate querySelectorAll return empty objects
提问by Abdullah Alsigar
I am trying out Puppeteer. This is a sample code that you can run on: https://try-puppeteer.appspot.com/
我正在试用 Puppeteer。这是您可以运行的示例代码:https: //try-puppeteer.appspot.com/
The problem is this code is returning an array of empty objects:
问题是这段代码正在返回一个空对象数组:
[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]
[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{ },{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}, {}、{}、{}、{}、{}、{}、{}、{}、{}、{}、{}、{}、{}、{}、{}、{}、{} ,{},{},{},{},{},{},{}]
Am I making a mistake?
我犯了一个错误吗?
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();
回答by Abdullah Alsigar
The values returned from evaluate function should be json serializeable. https://github.com/GoogleChrome/puppeteer/issues/303#issuecomment-322919968
从评估函数返回的值应该是 json 可序列化的。 https://github.com/GoogleChrome/puppeteer/issues/303#issuecomment-322919968
the solution is to extract the href values from the elements and return it.
解决方案是从元素中提取 href 值并返回它。
await this.page.evaluate((sel) => {
let elements = Array.from(document.querySelectorAll(sel));
let links = elements.map(element => {
return element.href
})
return links;
}, sel);
回答by Can Ali
I faced the similar problem and i solved it like this;
我遇到了类似的问题,我是这样解决的;
await page.evaluate(() =>
Array.from(document.querySelectorAll('.title'),
e => e.href));
回答by Grant Miller
Problem:
问题:
The return value for page.evaluate()must be serializable.
的返回值page.evaluate()必须是可序列化的。
According to the Puppeteer documentation, it says:
根据Puppeteer 文档,它说:
If the function passed to the
page.evaluatereturns a non-Serializablevalue, thenpage.evaluateresolves toundefined. DevTools Protocol also supports transferring some additional values that are not serializable byJSON:-0,NaN,Infinity,-Infinity, and bigint literals.
如果传递给 的函数
page.evaluate返回一个不可序列化的值,则page.evaluate解析为undefined。DevTools协议还支持转移是不可序列通过一些附加价值JSON:-0,NaN,Infinity,-Infinity,和BIGINT文字。
In other words, you cannot return an element from the page DOM environment back to the Node.js environment because they are separate.
换句话说,您无法将页面 DOM 环境中的元素返回到 Node.js 环境,因为它们是分开的。
Solution:
解决方案:
You can return an ElementHandle, which is a representation of an in-page DOM element, back to the Node.js environment.
您可以将 一个ElementHandle,它是页面内 DOM 元素的表示形式,返回到 Node.js 环境。
Use page.$$()to obtain an ElementHandlearray:
使用page.$$()以获得ElementHandle阵列:
let list = await page.$$('.title');
Otherwise, if you want to to extract the hrefvalues from the elements and return them, you can use page.$$eval():
否则,如果您想href从元素中提取值并返回它们,您可以使用page.$$eval():
let list = await page.$$eval('.title', a => a.href);

