无法运行puppeteer-full page截图npm包



我使用的是这里提到的pupeter包https://www.npmjs.com/package/puppeteer-full-page-screenshot

代码脚本

import puppeteer from 'puppeteer';
import fullPageScreenshot from 'puppeteer-full-page-screenshot';
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({ width: 1920, height: 1080 });
await page.goto('http://lambdatest.com/');
await fullPageScreenshot(page, { path: './page.png' });
await browser.close();
})();

但是运行脚本后,我得到以下错误

(node:39184) UnhandledPromiseRejectionWarning: TypeError: fullPageScreenshot is not a function
at file:///Users/arushsaxena/Desktop/DotlapseDemo/smartui-node-sample/puppeteer-lib.js:10:10
at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:39184) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:39184) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

您需要调用fullPageScreenshotdefault()函数才能使其工作:

import puppeteer from 'puppeteer';
import fullPageScreenshot from 'puppeteer-full-page-screenshot';
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({ width: 1920, height: 1080 });
await page.goto('http://lambdatest.com/');
await fullPageScreenshot.default(page, { path: './page.png' });
await browser.close();
})();

或者,您可以使用require而不是import,并将default()函数分配给fullPageScreenshot:

const puppeteer = require('puppeteer');
const fullPageScreenshot = require('puppeteer-full-page-screenshot').default;
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({ width: 1920, height: 1080 });
await page.goto('http://lambdatest.com/');
await fullPageScreenshot(page, { path: './page.png' });
await browser.close();
})();
import puppeteer from 'puppeteer';
import fullPageScreenshot from 'puppeteer-full-page-screenshot';
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({ width: 1920, height: 1080 });
await page.goto('http://lambdatest.com/');

await page.screenshot({ path: 'fullpage.png', fullPage: true });
await browser.close();
})();

相关内容

  • 没有找到相关文章

最新更新