User Agents

Web browsers and everyday web tech, explained

Fix Empty Page Source in Headless Chrome on Unix

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

When using headless Chrome on Unix systems to scrape or automate web content, you may encounter an empty page source—meaning the browser loads, but no HTML is returned. This typically happens because JavaScript-rendered content hasn’t fully loaded or because headless mode lacks certain environment signals that websites expect. Fixing this issue involves configuring Chrome properly, ensuring dependencies are met, and simulating real-user conditions to trigger full page rendering.

What You'll Need

  • Chrome or Chromium installed on a Unix system (Linux, macOS, or BSD)
  • ChromeDriver compatible with your Chrome version
  • Python, Node.js, or another supported language runtime (depending on your automation tool)
  • Basic command-line proficiency
  • Access to install system packages if needed (e.g., via apt, yum, or brew)

Step-by-Step

  1. Verify Chrome and ChromeDriver versions: Run google-chrome --version or chromium-browser --version to check your Chrome version. Then run chromedriver --version and confirm they are compatible. Mismatched versions often cause silent failures. Update either Chrome or ChromeDriver as needed.
  2. Launch Chrome with essential headless flags: Use the --headless=new flag (for Chrome 112+) or --headless=chrome for older versions. Avoid deprecated --headless alone. Include --no-sandbox and --disable-dev-shm-usage to prevent memory and permission issues in containerized or minimal Unix environments.
  3. Add user-agent and window size: Some sites block or limit headless browsers. Set a real-looking user agent using --user-agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36". Also set a viewport: --window-size=1920,1080. This mimics a real desktop browser.
  4. Wait for dynamic content to load: After navigating to a page, do not read the page source immediately. Use explicit waits—such as waiting for a specific element to appear—instead of fixed timeouts. In Selenium, use WebDriverWait with expected_conditions. For Puppeteer, use page.waitForSelector().
  5. Enable JavaScript and disable images (optional): Ensure JavaScript is enabled (it is by default). To speed up loading and reduce resource use, consider disabling images with --disable-images or --blink-settings=imagesEnabled=false, but only if images are not critical to content rendering.
  6. Run Chrome with a real display environment (if needed): On some Unix systems, especially headless servers, Chrome may fail without a display. Use xvfb (X Virtual Framebuffer) to simulate a display. Install it via apt install xvfb (Debian/Ubuntu) or equivalent, then launch Chrome with xvfb-run google-chrome --headless=new ....
  7. Check for site-specific detection: Some sites detect and block headless browsers using fingerprinting. Add flags like --disable-blink-features=AutomationControlled and set navigator.webdriver to false via CDP (Chrome DevTools Protocol) commands in your script.
  8. Log and debug output: Enable verbose logging with --enable-logging --v=1 to see internal Chrome errors. Check stdout/stderr for messages like DevToolsActivePort file doesn't exist or JavaScript errors. Use --dump-dom to output the full DOM if supported.
  9. Test with a simple script: Create a minimal script that navigates to a known JavaScript-heavy site (e.g., a React or Angular app) and waits before extracting page_source. If it works, gradually add complexity. If not, isolate the failure point using logs.
  10. Run as a non-root user: Avoid running Chrome as root. Create a dedicated user if necessary. Chrome blocks headless execution as root for security reasons, which can result in blank pages or crashes.

Common Mistakes

  • Using outdated Chrome or ChromeDriver versions without checking compatibility
  • Forgetting to wait for dynamic content—reading page source too early
  • Running headless Chrome without --no-sandbox or --disable-dev-shm-usage in restricted environments
  • Not simulating a real browser fingerprint, leading to blocks
  • Assuming headless mode behaves identically to GUI mode without testing

Tips

  • Always use --headless=new on Chrome 112+ for better compatibility and rendering
  • Combine --disable-gpu with headless mode to reduce resource use on Unix servers
  • Use Puppeteer or Playwright instead of raw Selenium when possible—they handle many headless quirks automatically
  • Set acceptInsecureCerts capability if testing on HTTPS with self-signed certs
  • Run Chrome with --disable-setuid-sandbox in Docker containers if --no-sandbox alone isn’t enough

Troubleshooting

If the page source remains empty, first check Chrome’s output logs. Look for connection errors, JavaScript exceptions, or rendering timeouts. If you see DevToolsActivePort errors, ensure Chrome can access temporary directories. If the process hangs, reduce the page load timeout in your driver. If content is still missing, test with --dump-dom to see if Chrome rendered anything at all. Try launching Chrome in non-headless mode (--headless=new removed) on a desktop Unix system to verify the page loads visually. If it works there, the issue is headless-specific. Consider using a full browser automation tool with stealth plugins to bypass detection.

Frequently Asked Questions

Why is my headless Chrome returning no HTML?

Because JavaScript content hasn’t loaded yet or Chrome is misconfigured. Use proper flags and explicit waits.

Does --no-sandbox make Chrome unsafe?

Yes, but it’s often required in containerized environments. Mitigate risk by running Chrome in a restricted user context.

Can I run headless Chrome without a display server?

Yes, using --headless=new eliminates the need for X11. But if issues persist, xvfb can help.

How do I know if a site is blocking headless Chrome?

Check for blank pages only in headless mode. Look for navigator.webdriver checks or CAPTCHAs appearing unexpectedly.

Is Puppeteer better than Selenium for this?

Puppeteer is Chrome-specific and often handles headless quirks more seamlessly, making it a good alternative.

Do I need to install X11 libraries?

Not usually with modern --headless=new, but some older systems or packages may require libx11 or libxss1.