如何使用.$$ eval函数 [英] How to use .$$eval function

查看:854
本文介绍了如何使用.$$ eval函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行以下代码:

I'm trying to run this code:

var aaa = await page.$$eval(selector, list => (list, value) => 
    {
        return resolve(list.find(element => element.textContent === value));
    }
    ,value);

但是我收到一个错误.

因此,我尝试在列表"中打印项目(因为我假设存在问题),因此尝试了以下代码:

Therefore, I tried to print the items in "list" (because I assumed that the problem is there), I tried this code:

var aaa = await page.$$eval(selector, list => list);

我收到"aaa"为空.

And I received that "aaa" is empty.

你知道可能是什么问题吗?

Any idea what may be the problem?

推荐答案

您正尝试从尝试改用 page.$$() 如果您想返回 ElementHandle 数组.

Try using page.$$() instead if you would like to return an ElementHandle array.

看看木偶文档表示page.$$eval():

page.$$ eval(selector,pageFunction [,... args])

page.$$eval(selector, pageFunction[, ...args])

  • selector <string> A selector to query page for
  • pageFunction <function> Function to be evaluated in browser context
  • ...args <...Serializable|JSHandle> Arguments to pass to pageFunction
  • returns: <Promise<Serializable>> Promise which resolves to the return value of pageFunction

此方法在页面中运行Array.from(document.querySelectorAll(selector))并将其作为第一个参数传递给pageFunction.

This method runs Array.from(document.querySelectorAll(selector)) within the page and passes it as the first argument to pageFunction.

如果pageFunction返回承诺,然后page.$$eval将等待承诺解决并返回其值.

If pageFunction returns a Promise, then page.$$eval would wait for the promise to resolve and return its value.

示例:

const divsCounts = await page.$$eval('div', divs => divs.length);

这篇关于如何使用.$$ eval函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆