简体   繁体   English

使用噩梦截屏选择器制作多个屏幕截图

[英]Make multiple screenshots with nightmare-screenshot-selector

I have a web-page with set of 'li' elements. 我有一个包含“ li”元素集的网页。 I need to make screenshots for each 'li' element and save it to a new files. 我需要为每个“ li”元素制作屏幕截图,并将其保存到新文件中。 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. 我插入了一个.end()调用,对我有用。 Slightly modified code, grabs two regions of Google home page: 稍作修改的代码,获取了Google主页的两个区域:

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)
            })
        })
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM