Ani*_*mde 19 automation unit-testing google-chrome puppeteer
是否可以使用puppeteer(没有Web服务器)使用无头chrome打开本地html文件?我只能将它连接到本地服务器.
我可以看到setContent()api和goto()api和
com*_*eye 17
我刚刚在本地做了一个测试(你可以看到我在windows上做了这个),而puppeteer使用page.goto和一个完整的文件URL愉快地打开我的本地html文件,并将其保存为pdf:
'use strict';
const puppeteer = require('puppeteer');
(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('file://C:/Users/compoundeye/test.html');
await page.pdf({
path: 'test.pdf',
format: 'A4',
margin: {
top: "20px",
left: "20px",
right: "20px",
bottom: "20px"
}
});
await browser.close();
})();
Run Code Online (Sandbox Code Playgroud)
如果您需要使用相对路径,可能需要查看有关相对文件路径的使用的问题:File Uri Scheme和Relative Files
Chu*_*ran 11
如果文件位于本地,则使用setContent优于goto
var contentHtml = fs.readFileSync('file://C:/Users/compoundeye/test.html', 'utf8');
await page. setContent(contentHtml);
Run Code Online (Sandbox Code Playgroud)
您可以在此处检查setContent和goto之间的性能
为什么不打开HTML文件读取内容,然后“setContent”
import puppeteer from 'puppeteer';
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// __dirname is a global node variable that corresponds to the absolute
// path of the folder containing the currently executing file
await page.goto(`file://${__dirname}/pages/test.html`);
const element = await page.$('.myElement');
if (element) {
await elementHandle.screenshot({
path: `./out/screenshot.png`,
omitBackground: true,
});
}
await browser.close();
})();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14226 次 |
| 最近记录: |