如何通过 Puppeteer 获取父元素和子元素?
How to get parent and children elements by Puppeteer?
我得到父元素的子元素为
const a = await page.$$('a');
for (let i = 0; i < a.length; i++) {
constant link = // How to get HREF of a[i]?
const photo = await a[i].$eval('img', el => el.getAttribute('src'));
console.log(link);
console.log(photo);
}
但是由于 a[i]
是一个 puppeteer 句柄而不是 DOM 元素,我如何才能获得父元素的属性?
您也可以使用evaluate
功能。 Puppeteer 会知道如何使用手柄。
const href= await page.evaluate(el => el.href, a[i])
我得到父元素的子元素为
const a = await page.$$('a');
for (let i = 0; i < a.length; i++) {
constant link = // How to get HREF of a[i]?
const photo = await a[i].$eval('img', el => el.getAttribute('src'));
console.log(link);
console.log(photo);
}
但是由于 a[i]
是一个 puppeteer 句柄而不是 DOM 元素,我如何才能获得父元素的属性?
您也可以使用evaluate
功能。 Puppeteer 会知道如何使用手柄。
const href= await page.evaluate(el => el.href, a[i])