作者:mobiledu2502861465 | 来源:互联网 | 2022-12-01 13:40
1> CodeGuru..:
基于https://github.com/GoogleChrome/puppeteer
您只需要在Ubuntu 18.04中运行以下命令
npm i puppeteer
不幸的是,这还不够。
您将需要以下依赖关系
sudo apt-get install gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
之后,如果按照其示例运行它,将会收到错误消息
(node:28469) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
[1025/150325.817887:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
解决方案是
const browser = await puppeteer.launch({args: ['--no-sandbox']});
添加--no-sandbox
然后它将相应地工作。完整的工作源代码如下
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({args: ['--no-sandbox']});
const page = await browser.newPage();
await page.goto('http://owlcommand.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();
puppeteer@1.9.0~install的解决方案:无法在wd%s%s中运行(wd =%s)
npm install --unsafe-perm
屏幕截图大小
默认值很小,如果要测试的页面具有响应能力,则可以使用其他视口设置对其进行测试。您可以通过setViewport方法更改其尺寸。
await page.setViewport({
width: 1600,
height: 1000
});