在操纵up中获取元素的子代

So my goal here is to count the number of children of a div class that has inside it multiple div's; I do that by selecting the div then using the children attribute, and then I count the children with their length property and print it.

为了实现我的目标,我试图在puppeteer中实现以下代码:

$('div[data-test-selector="center-window__content"]').children

(可在chrome调试控制台上完美运行)

这是我转换后的代码:

let childrens = await page.evaluate(() => document.querySelector('div[data-test-selector="center-window__content"]'), i => Array.from(i.children));

However this expression evaluates to undefined.

On their documentation for evaluate it is said that:

如果传递给page.evaluate的函数返回不可序列化的值,则page.evaluate解析为undefined。

是这样吗?我该如何解决?

提前致谢。

评论
  • 随怀芹
    随怀芹 回复

    您可以尝试一下:

    const children = await page.evaluate(() => {
        return Array.from(document.querySelector('div[data-test-selector="center-window__content"]').children);
    })
    console.log("Result:", children);