Fix Empty Page Source in Headless Chrome on Unix
In this article
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
- Verify Chrome and ChromeDriver versions: Run
google-chrome --versionorchromium-browser --versionto check your Chrome version. Then runchromedriver --versionand confirm they are compatible. Mismatched versions often cause silent failures. Update either Chrome or ChromeDriver as needed. - Launch Chrome with essential headless flags: Use the
--headless=newflag (for Chrome 112+) or--headless=chromefor older versions. Avoid deprecated--headlessalone. Include--no-sandboxand--disable-dev-shm-usageto prevent memory and permission issues in containerized or minimal Unix environments. - 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. - 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
WebDriverWaitwithexpected_conditions. For Puppeteer, usepage.waitForSelector(). - 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-imagesor--blink-settings=imagesEnabled=false, but only if images are not critical to content rendering. - 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 viaapt install xvfb(Debian/Ubuntu) or equivalent, then launch Chrome withxvfb-run google-chrome --headless=new .... - Check for site-specific detection: Some sites detect and block headless browsers using fingerprinting. Add flags like
--disable-blink-features=AutomationControlledand setnavigator.webdrivertofalsevia CDP (Chrome DevTools Protocol) commands in your script. - Log and debug output: Enable verbose logging with
--enable-logging --v=1to see internal Chrome errors. Check stdout/stderr for messages likeDevToolsActivePort file doesn't existor JavaScript errors. Use--dump-domto output the full DOM if supported. - 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. - 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-sandboxor--disable-dev-shm-usagein 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=newon Chrome 112+ for better compatibility and rendering - Combine
--disable-gpuwith 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
acceptInsecureCertscapability if testing on HTTPS with self-signed certs - Run Chrome with
--disable-setuid-sandboxin Docker containers if--no-sandboxalone 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.