我使用puppeteer js遍历所有元素,如下所示:
const getAllElements = await page.$$('._1Nk0C');
for (let [i, link] of getAllElements.entries()) {
await link.hover();
await link.screenshot({path: `example${i}.png`});
}
该元素内还有另一个元素,其标题为“下载”。像这样:
<a title="Download photo" href="https://example.com/df.jpg/download?force=true" rel="nofollow" download="" target="_blank" class="_1QwHQ"></a>
我需要获取该元素,以便可以单击它。 我尝试过:
for (let [i, link] of getAllElements.entries()) {
await link.hover();
await link.screenshot({path: `example${i}.png`});
const download = await page.evaluate(() => [...document.querySelector('a[title="Download photo"]')])
console.log(download);
}
这给了我一个错误:
UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: document.querySelector is not a function or its return value is not iterable
如何获取该元素?
答案 0 :(得分:2)
您可以在$
变量上调用link
:
let linkPhoto = await link.$('a[title="Download photo"]');