如何使用.$$ eval函数 [英] How to use .$$eval function
问题描述
我正在尝试运行以下代码:
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?
推荐答案
您正尝试从 undefined
,因为DOM元素不是尝试改用 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 forpageFunction
<function> Function to be evaluated in browser context...args
<...Serializable|JSHandle> Arguments to pass topageFunction
- 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屋!