1

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

Thomas Carlton
  • 5,344
  • 10
  • 63
  • 126

2 Answers2

1

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.)

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Thanks for your answer. Do you know any other solution to run it in headless mode please ? – Thomas Carlton Nov 04 '21 at 23:47
  • @ThomasCarlton Not until the Chromium issue is fixed. But, what is the specific reason you need to run it headless? What platform are you running on? If you just need to hide the window, that's likely possible. – Brad Nov 04 '21 at 23:49
  • @ThomasCarlton Here's that Chromium bug I was talking about: https://bugs.chromium.org/p/chromium/issues/detail?id=1195881 (This writeup is for a different API, but likely caused by the same limitations that you're getting for the Tab Capture API, which puppeteer-stream uses internally.) – Brad Nov 04 '21 at 23:51
  • Oh thanks. I'm running it on Windows and I need it to be in headless mode for performance reasons. "If you just need to hide the window, that's likely possible" That would be great. By any chance, do you know how to hide the window ? – Thomas Carlton Nov 04 '21 at 23:53
  • @ThomasCarlton Actually, it appears that there's another reason puppeteer-stream isn't working: https://stackoverflow.com/q/45372066/362536 On Linux, I'd just use xvfb-run, but can't think of what I'd use on Windows to hide the window, sorry. Maybe run your code under another user, like a service user... not sure if that will have side effects or not. There are likely some Auto-IT-style utilities out there for changing the window style to hidden/unclickable after Chrome has launched. – Brad Nov 05 '21 at 00:02
0

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
Dharman
  • 30,962
  • 25
  • 85
  • 135