看起来
currentSrc
财产为我解决了这个问题。这是完整的工作解决方案:
const getXpathElement = await page.$x(xpath);
const promises = getXpathElement.map((element) => page.evaluate(el => {
if (el.currentSrc && (el.tagName && el.tagName.toLowerCase() === "img")) // when the xpath ends with tag name, Ex: //div//img
return el.currentSrc;
else // when the xpath ends with property name, Ex: //div//div/@src
return el.textContent
}, element));