I have a web-page with set of 'li' elements. I need to make screenshots for each 'li' element and save it to a new files. I'm trying to use nightmare-screenshot-selector for it. But I get a few files with the same screenshot but with different names (from my array).
Here is my code.
const Nightmare = require('nightmare');
const fs = require('fs');
const screenshotSelector = require('nightmare-screenshot-selector');
Nightmare.action('screenshotSelector', screenshotSelector);
function savePicture(picture) {
picture = ['v1', 'v2', 'v3'];
let browser = Nightmare({
show: false,
webPreferences: {
partition: 'nopersist'
}
});
browser
.goto('https://www.google.com')
picture.forEach(v => {
browser
.wait(7000)
.screenshotSelector(`li[class="${v}"]`)
.then(function (data) {
fs.writeFileSync(`img/${v}.png`, data)
})
.catch((error) => {
console.log('Error loading the page', error)
})
})
browser.end();
}
I inserted a .end() call, works for me. Slightly modified code, grabs two regions of Google home page:
function savePicture(picture) {
picture = ['div#hplogo', 'div.tsf-p'];
let browser = Nightmare({
show: false,
webPreferences: {
partition: 'nopersist'
}
});
browser
.goto('https://www.google.com')
picture.forEach(v => {
browser
.wait(2000)
.screenshotSelector(v)
.then(function (data) {
fs.writeFileSync(`img/${v}.png`, data)
})
.then(()=> {
browser.end();
})
.catch((error) => {
console.log('Error loading the page', error)
})
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.