Opening local HTML file using Puppeteer

前端 未结 7 1947
梦谈多话
梦谈多话 2020-12-30 19:04

Is it possible to open a local HTML file with headless Chrome using Puppeteer (without a web server)? I could only get it to work against a local server.

I found

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-30 19:34

    Let's take a screenshot of an element from a local HTML file as an example

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

提交回复
热议问题