Why chromium doesn't open in headless Mode?

Viewed 290

I have the following NodeJS code to open Chromium in headless mode and record a web page to a video :

    const { launch, getStream }  = require("puppeteer-stream");
    const fs = require("fs");
    const { exec } = require("child_process");

    async function test() {         
        const browser = await launch({headless: true});

        const page = await browser.newPage();
        await page.goto("https://www.someurl.com");
        const stream = await getStream(page, { audio: true, video: true});
        
        // record the web page to mp4 video
        const ffmpeg = exec('ffmpeg -y -i - output.mp4');

        stream.pipe(ffmpeg.stdin);

        setTimeout(async () => {
            await stream.destroy();
            stream.on("end", () => {});
        }, 1000 * 60);
    }

The following code works properly but doesn't open chromium in headless mode. No matter what I do, the browser is still opened and visible when browsing the page. No error is thrown.

Does anyone know why it's not opened in headless mode please ?

Thanks

2 Answers

It says in the documentation for puppeteer-stream:

Notice: This will only work in headful mode

This is due to a limitation of Chromium where the Tab Capture API for the extension doesn't work in headless mode. (There are a couple bug reports about this, but I can't find the links at the moment.)

I had the same issue that headless doesn't work with some Websites and Elements (showing blank page content, not finding an element etc.).

But there is another method to "simulate" the headless mode by minimizing and moving the window to a location that can not be seen by the user.

This doesn't hide the chrome task from the taskbar, but the Chrome tab itself will still be hidden for the User.

Just use the following arguments:

var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments(new List<string>() { "--window-size=1,1", "window-position=-2000,0" });  // This hides the chrome window

var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;    // This is to hid the console.

ChromeDriver driver = new ChromeDriver(chromeDriverService, chromeOptions);
driver.Navigate().GoToUrl("https://google.com");

in short the important part:

chromeOptions.AddArguments(new List<string>() { "--window-size=1,1", "window-position=-2000,0" });
chromeDriverService.HideCommandPromptWindow = true;
//driver.Manage().Window.Minimize(); //use this if the code above does not work
Related