【发布时间】:2021-08-26 23:42:46
【问题描述】:
我正在尝试对可能包含多个部分的登录页面中的每个部分进行截图。我能够在我注释掉的“Round1”中有效地做到这一点。
我的目标是学习如何编写更精简/更简洁的代码,所以我又做了一次尝试,“Round2”。
在本节中,它会截取屏幕截图。但是,它使用文件名JSHandle@node.png 截取第 3 节的屏幕截图。当然,我做错了。
Round1(完美运行)
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.somelandingpage.com');
// const elOne = await page.$('.section-one');
// await elOne.screenshot({path: './public/SectionOne.png'})
// takes a screenshot SectionOne.png
// const elTwo = await page.$('.section-two')
// await elTwo.screenshot({path: './public/SectionTwo.png'})
// takes a screenshot SectionTwo.png
// const elThree = await page.$('.section-three')
// await elThree.screenshot({path: './public/SectionThree.png'})
// takes a screenshot SectionThree.png
第二轮
我创建了一个包含所有变量的数组并尝试遍历它们。
const elOne = await page.$('.section-one');
const elTwo = await page.$('.section-two')
const elThree = await page.$('.section-three')
let lpElements = [elOne, elTwo, elThree];
for(var i=0; i<lpElements.length; i++){
await lpElements[i].screenshot({path: './public/'+lpElements[i] + '.png'})
}
await browser.close();
})();
这仅截取第三部分的屏幕截图,但文件名错误 (JSHandle@node.png)。控制台上没有错误消息。
如何通过修改 Round2 代码重现 Round1?
【问题讨论】:
-
傀儡师不是傀儡。标签已编辑。
标签: javascript arrays puppeteer