配置分辨率数组,自动获取当前窗口界面的截图

发布时间 2024-01-10 09:34:31作者: lambertlt
var Nightmare = require("nightmare");
const fs = require("fs");
const screenshotSelector = require('nightmare-screenshot-selector');
Nightmare.action('screenshotSelector', screenshotSelector);

let url = "https://www.baidu.com/"
let resolutionRatio = [{ w: 1000, h: 500 }]
var nightmare = Nightmare({
    show: true,
    executionTimeout: 1000 * 60 * 100,
    waitTimeout: 1000 * 60 * 100,
});
nightmare.goto(`${url}`)

for (let i = 0; i < resolutionRatio.length; i++) {
    const element = resolutionRatio[i];
    nightmare.viewport(element.w, element.h)
        .wait(3000)
        .screenshotSelector("html")
        .then(function (result) {
            console.log(result);
            fs.writeFileSync(`./img/${i}.png`, result)
        })
        .catch(function (error) {
            console.error("Search failed:", error);
        });
}

nightmare.end()