如何通过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]
是一个木偶操作者而不是DOM元素?
您还可以使用evaluate
函数。 Puppeteer会知道如何使用手柄。
const href= await page.evaluate(el => el.href, a[i])
如何通过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]
是一个木偶操作者而不是DOM元素?
您还可以使用evaluate
函数。 Puppeteer会知道如何使用手柄。
const href= await page.evaluate(el => el.href, a[i])