chrome-headlessHow to get web page source HTML using Chrome Remote Interface
Execute this javascript file using nodejs:
const chromeLauncher = require('chrome-launcher');
const CDP = require('chrome-remote-interface');
chromeLauncher.launch({ port: 9222, chromeFlags: [ '--headless' ] }).then(function(chrome) {
  CDP(async (client) => {
    const {Network, Page, Runtime} = client;
    await Page.enable();
    await Page.navigate({url: 'https://github.com'});
    await Page.loadEventFired();
    
    const result = await Runtime.evaluate({ expression: 'document.documentElement.outerHTML' });
    const html = result.result.value;
    console.log(html);
    
    await client.close();
    chrome.kill();
  });
});ctrl + c| require('chrome-launcher')Chrome-Launcher library to start/stop Chrome browser programmatically | require('chrome-remote-interface')Chrome-Remote-Interface library to operate | 
| chromeLauncher.launchlaunch Chrome with specified params | Page.navigatego to specified URL | 
| await Page.loadEventFired()wait while page is loaded | Runtime.evaluateexecute given  | 
| document.documentElement.outerHTMLreturns full page HTML | console.log(html)output HTML right into console | 
Usage example
nodejs test.jsoutput
<html lang="en" data-a11y-animated-images="system"><head>
    <meta charset="utf-8">
    ...More of Chrome Headless
- How to run chrome headless with extensions
- How to capture video using Chrome Remote Interface
- How to save web page source HTML to a file
- How to take full page page screenshot
- How to take page screenshot in specific resolution
- Headless chrome usage example
- How to launch Google Chrome using Xvfb
- How to use proxy with authentication in headless mode
- How to write logs in headless mode
- How to trigger mouse click in headless mode
See more codes...