General
George Miloradovich
Researcher, Copywriter & Usecase Interviewer
February 28, 2025
A low-code platform blending no-code simplicity with full-code power 🚀
Get started free
February 28, 2025
•
9
min read

Headless Chrome: How to Use and Configure It

George Miloradovich
Researcher, Copywriter & Usecase Interviewer
Table of contents

Want to automate tasks, scrape data, or test websites efficiently? Headless Chrome can help you do just that. It’s a browser that works without a graphical interface, making it faster and less resource-intensive for tasks like web scraping, automated testing, and SEO analysis.

Key Benefits:

  • Web Scraping: Extract data from JavaScript-heavy websites.
  • Automated Testing: Run faster, resource-saving tests for CI/CD pipelines.
  • Performance Monitoring: Simulate user interactions to debug issues.
  • SEO Analysis: Quickly gather and analyze website data.

Quick Setup:

  1. Install Node.js and Puppeteer.
  2. Configure basic settings like viewport size and resource blocking.
  3. Use scripts to automate tasks, capture screenshots, or generate PDFs.

Platforms like Latenode simplify this process further with low-code tools for automation. Whether you're a developer or a beginner, Headless Chrome is a powerful tool to streamline web tasks. Let’s dive into how to set it up and use it effectively.

What is a headless browser? How do you run Headless Chrome?

Chrome

Setup Guide

Make sure your system meets the required specifications and follow the installation steps for your platform.

Technical Requirements

Check your system's compatibility:

Operating System System Requirements
Windows • Windows 10 or Windows Server 2016+
• Intel Pentium 4 (SSE3 capable) or newer
macOS • macOS Big Sur 11 or newer
Linux • 64-bit Ubuntu 18.04+, Debian 10+
• openSUSE 15.5+ or Fedora 39+
• Intel Pentium 4 (SSE3 capable) or newer

You’ll also need to install Node.js (latest LTS version) to use Puppeteer.

Installation Steps

Follow these steps based on your platform:

  • Windows Download Chrome from its official website, install Node.js, and then run:
    npm install puppeteer
    
  • macOS Use Homebrew to install Chrome and Puppeteer:
    brew install --cask google-chrome
    npm install puppeteer
    
  • Linux Update your system and install Chrome along with Puppeteer:
    sudo apt update
    sudo apt install google-chrome-stable
    npm install puppeteer
    

After installation, double-check your setup to ensure everything is working.

Testing Your Installation

Run these commands to confirm Chrome is installed correctly:

google-chrome-stable --version
google-chrome-stable --headless --disable-gpu --dump-dom https://www.google.com/

If you see Chrome's version and Google's HTML output, Chrome is ready to go. To test Puppeteer, use the script below:

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://www.google.com');
    await browser.close();
})();

Save this code as test.js and run it using node test.js. If it runs without errors, your setup is complete, and you're ready to dive into automation tasks.

Basic Settings

Core Settings

Set up the essential configurations to ensure smooth automation, effective resource management, and reliable request handling.

const browser = await puppeteer.launch({
    headless: true,
    defaultViewport: { width: 1920, height: 1080 },
    args: [
        '--no-sandbox',
        '--disable-setuid-sandbox',
        '--disable-dev-shm-usage',
        '--disable-accelerated-2d-canvas',
        '--disable-gpu'
    ]
});

This setup works well for most automation tasks, using standard desktop screen dimensions and stability-focused arguments. You can tweak these settings based on your specific requirements.

Task-Specific Setup

Fine-tune the configuration for individual tasks. For example, if you're working on web scraping, you can reduce resource usage and avoid detection:

const page = await browser.newPage();

// Block unnecessary resources
await page.setRequestInterception(true);
page.on('request', (request) => {
    if (['image', 'stylesheet', 'font'].includes(request.resourceType())) {
        request.abort();
    } else {
        request.continue();
    }
});

// Set custom headers
await page.setExtraHTTPHeaders({
    'Accept-Language': 'en-US,en;q=0.9',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
});

// Enable JavaScript if required
await page.setJavaScriptEnabled(true);

For automated testing, prioritize stability and consistency:

const page = await browser.newPage();
await page.setDefaultTimeout(30000);
await page.setDefaultNavigationTimeout(30000);
await page.setCacheEnabled(false);

You can further enhance performance by tweaking speed and resource allocation settings.

Speed and Resource Settings

Boost the performance of Headless Chrome by managing resources effectively. Below are some useful configurations:

Setting Type Configuration Purpose
Memory --max-old-space-size=4096 Allocates up to 4GB of memory for Node.js
Process --single-process Runs Chrome as a single process
Rendering --disable-gpu Disables GPU hardware acceleration (as shown earlier)

For larger-scale tasks, you can run multiple browser sessions concurrently while managing resources:

const cluster = await Cluster.launch({
    concurrency: Cluster.CONCURRENCY_CONTEXT,
    maxConcurrency: 4,
    monitor: true,
    puppeteerOptions: {
        headless: true,
        args: ['--no-sandbox']
    }
});

Additionally, adjust timeout settings to match your network conditions:

page.setDefaultNavigationTimeout(60000);  // 60 seconds for navigation
page.setDefaultTimeout(30000);            // 30 seconds for other tasks

These configurations will help you strike a balance between speed, stability, and resource efficiency.

sbb-itb-23997f1

JavaScript Operations

Headless Chrome can execute JavaScript and handle web interactions effectively with Puppeteer.

Running Simple Scripts

Puppeteer makes browser automation straightforward:

const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();

// Navigate to a page and wait for the network to be idle
await page.goto('https://example.com', {
    waitUntil: 'networkidle0',
    timeout: 30000
});

// Get the page title using JavaScript
const pageTitle = await page.evaluate(() => {
    return document.title;
});

// Extract specific data from the page
const results = await page.evaluate(() => {
    const data = [];
    document.querySelectorAll('.product-item').forEach(item => {
        data.push({
            name: item.querySelector('.title').textContent,
            price: item.querySelector('.price').textContent
        });
    });
    return data;
});

Page Interaction Methods

You can simulate user actions like clicks and typing to make interactions appear more natural:

// Wait for an element to appear and click it
await page.waitForSelector('.login-button');
await page.click('.login-button');

// Type text into an input field with random delays
await page.type('#username', '[email protected]', {
    delay: Math.floor(Math.random() * 100) + 50
});

// Handle form submission and wait for navigation
await Promise.all([
    page.waitForNavigation(),
    page.click('#submit-button')
]);

"A headless browser is a great tool for automated testing and server environments where you don't need a visible UI shell." - Eric Bidelman

Managing Dynamic Elements

Dynamic content requires specific handling to ensure proper interaction:

// Wait for dynamic content to load
await page.waitForFunction(
    'document.querySelector(".dynamic-content").childNodes.length > 0',
    { timeout: 5000 }
);

// Handle infinite scrolling
async function scrollToBottom() {
    await page.evaluate(async () => {
        await new Promise((resolve) => {
            let totalHeight = 0;
            const distance = 100;
            const timer = setInterval(() => {
                window.scrollBy(0, distance);
                totalHeight += distance;

                if (totalHeight >= document.body.scrollHeight) {
                    clearInterval(timer);
                    resolve();
                }
            }, 100);
        });
    });
}

Here are some common scenarios and solutions for working with dynamic elements:

Scenario Solution Use Case
Loading States Use waitForSelector with visibility check Single-page applications
AJAX Updates Use waitForFunction to verify content Real-time data feeds
Shadow DOM Use evaluateHandle with custom selectors Web components

Optimization Tips:

  • Use explicit waits to avoid unnecessary delays.
  • Implement error handling to manage script failures.
  • Keep an eye on CPU and memory usage during execution.
  • Disable non-essential resources like images or ads to boost performance.

Advanced Features

Building on basic settings and JavaScript operations, these advanced features take Headless Chrome to the next level. They allow for more refined output and better error handling, making your automation tasks even more efficient.

Screenshot Creation

Taking screenshots with Puppeteer is straightforward. Here's how you can capture a full-page screenshot:

const browser = await puppeteer.launch();
const page = await browser.newPage();

// Set a consistent viewport size
await page.setViewport({ 
    width: 1920,
    height: 1080,
    deviceScaleFactor: 2
});

// Wait for the page to load and capture a full-page screenshot
await page.goto('https://example.com', {
    waitUntil: 'networkidle0',
    timeout: 30000
});
await page.screenshot({
    path: 'full-page.jpg',
    fullPage: true,
    type: 'jpeg'
});

Need to capture a specific element? Focus on a particular section of the page:

// Screenshot of a specific element
const element = await page.$('.hero-section');
await element.screenshot({
    path: 'hero.png',
    omitBackground: true
});
Screenshot Option Best Use Case Performance Impact
JPEG Format Large screenshots, faster processing Lower quality, smaller file size
PNG Format High detail or transparency required Larger files, slower processing
Element-specific UI components, selective capture Minimal resource usage

PDF Creation

You can also generate PDFs with custom formatting:

await page.pdf({
    path: 'document.pdf',
    format: 'A4',
    margin: {
        top: '1in',
        right: '1in',
        bottom: '1in',
        left: '1in'
    },
    printBackground: true,
    displayHeaderFooter: true,
    headerTemplate: '<div style="font-size: 10px;">Generated on {{date}}</div>',
    footerTemplate: '<div style="font-size: 10px;">Page <span class="pageNumber"></span> of <span class="totalPages"></span></div>'
});

"Headless Chrome is a way to run the Chrome browser in a headless environment. Essentially, running Chrome without chrome! It brings all modern web platform features provided by Chromium and the Blink rendering engine to the command line." - Eric Bidelman, Chrome for Developers

Once your outputs are ready, you can use built-in tools to debug and fine-tune performance.

Troubleshooting Tools

Debugging issues in Headless Chrome is easier with the Chrome DevTools Protocol:

// Enable debugging
const browser = await puppeteer.launch({
    headless: true,
    devtools: true,
    args: ['--remote-debugging-port=9222']
});

// Add error logging
page.on('console', msg => console.log('Browser console:', msg.text()));
page.on('pageerror', err => console.error('Page error:', err));

For more complex issues, you can automate error capture:

try {
    await page.goto('https://example.com');
} catch (error) {
    await page.screenshot({
        path: `error-${Date.now()}.png`,
        fullPage: true
    });
    console.error('Navigation failed:', error);
}

For example, Chrome DevTools has been used to address element identification issues in Google IDP services .

Debugging Method Purpose When to Use
Remote DevTools Live inspection Complex rendering issues
Console Logging Track script execution Script flow problems
Error Screenshots Visual debugging UI-related failures

Using Headless Chrome with Latenode

Latenode

This section explains how to utilize a low-code platform like Latenode for Headless Chrome automation. Latenode integrates Headless Chrome into its system, making web automation straightforward for both developers and non-technical users.

About Latenode

Latenode

Latenode includes built-in Headless Chrome functionality through its "Headless browser" node system. This allows teams to automate workflows without having to directly manage Puppeteer.

Feature Description Benefit
Visual Builder Drag-and-drop workflow creation Simplifies basic automation tasks
AI Code Copilot Automated code generation Speeds up complex scenario setups
Integrated Data Storage Built-in data handling Makes managing extracted data easier
NPM Integration Access to 1M+ packages Adds extra functionality

Latenode Setup Steps

Here’s an example script to get started:

async function run({execution_id, input, data, page}) {
    // Set user agent for better compatibility
    await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/98.0.4758.102');

    // Configure viewport for reliable element detection
    await page.setViewport({
        width: 1920,
        height: 1080,
        deviceScaleFactor: 1
    });

    return {
        status: 'success'
    }
}

For more advanced web tasks, Latenode’s Headless browser node provides access to page manipulation functions. It also manages browser instances automatically, so you don’t have to set up Puppeteer manually.

Platform Highlights

Latenode streamlines Headless Chrome automation by addressing common challenges with traditional coding. Key features include:

  • Automated error handling and retry options
  • Built-in proxy management
  • Visual debugging tools for workflows
  • Execution history tracking for up to 60 days (available in the Prime plan)

Pricing is based on execution usage, offering options from a free tier (300 credits) to enterprise-level plans that support up to 1.5 million scenario runs per month. This makes it a flexible and budget-friendly choice for scaling automation efforts.

For teams juggling multiple workflows, the visual builder speeds up development while supporting advanced features like screenshot capture and PDF generation. By simplifying deployment and management, Latenode enhances what Headless Chrome already offers, making automation more accessible.

Conclusion

Summary

Headless Chrome makes web automation faster and more efficient by eliminating the need for a full browser interface. It reduces resource consumption and speeds up processes, making it ideal for tasks like web scraping, testing, SEO analysis, and performance tracking . Platforms like Latenode make deploying Headless Chrome easier with visual tools and automated features, requiring less technical know-how.

Getting Started

Follow these steps to start using Headless Chrome:

  1. Setup Basics:
    Install Node.js and Puppeteer. These tools provide APIs that simplify automation tasks.
  2. Configure Settings:
    Begin by navigating pages and taking screenshots. Fine-tune performance by adjusting these settings:
    Setting Purpose Benefit
    Disable Images Save bandwidth Faster page loads
    Custom Viewport Ensure consistent rendering Better element detection
    Resource Blocking Avoid unnecessary downloads Faster execution
  3. Advanced Features:
    Use waitForSelector to manage dynamic content and set up error handling for smoother operations. For scaling, Latenode offers flexible plans, starting with a free tier (300 credits) and going up to enterprise solutions that support up to 1.5 million executions monthly.

Related Blog Posts

Related Blogs

Use case

Backed by