这个
page.$$eval()
方法运行
Array.from(document.querySelectorAll(selector))
并将其作为第一个参数传递给page函数。
自从
a
在表示数组的示例中,您需要指定要获取数组的哪个元素
href
或者你需要
map
所有的
href
数组的属性。
第页$$评估()
const hrefs = await page.$$eval('a', links => links.map(a => a.href));
或者,您也可以使用
page.evaluate()
或者两者的结合
page.$$()
,则,
elementHandle.getProperty()
或
jsHandle.jsonValue()
实现页面中所有链接的数组。
页评估()
const hrefs = await page.evaluate(() => {
return Array.from(document.getElementsByTagName('a'), a => a.href);
});
第页$$()/元素句柄。getProperty()/jsHandle。jsonValue()
const hrefs = await Promise.all((await page.$$('a')).map(async a => {
return await (await a.getProperty('href')).jsonValue();
}));