Headless mode allows you to run browser automation without displaying a visible browser window. This is ideal for server environments, CI/CD pipelines, and background tasks.
Enabling headless mode
Basic headless setup
Simply pass headless=True to the start() function:
Using Config object
For more control, use a Config object:
From config.py:37-81:
Headless mode features
Automatic user agent cleaning
Nodriver automatically removes “Headless” from the user agent:
From tab.py:203-222:
All features work in headless
Headless mode supports all nodriver features:
Chrome arguments for headless
The headless mode uses Chrome’s new headless implementation:
From config.py:192-194:
Nodriver uses --headless=new instead of the old --headless flag. The new mode is more compatible and harder to detect.
Sandbox mode
Why sandbox matters
Sandbox mode is important for security, especially in production environments:
From config.py:99-108:
If you’re running as root on Linux, nodriver automatically disables sandbox mode. This is less secure but necessary for root execution.
Server/Docker environments
Running in Docker
Example Dockerfile for headless nodriver:
Python script for Docker:
Running on Linux servers
For headless Linux servers, you may need additional dependencies:
Python script:
Debugging headless mode
Remote debugging
Connect to a headless browser using the inspector:
Then open the inspector URL in another browser to see what’s happening.
Opening inspector programmatically
From tab.py:189-196:
Usage:
Taking screenshots for debugging
Headless is faster
Headless mode is generally faster than headed mode:
Memory usage
Headless mode uses less memory without GPU rendering:
Resource limits
Set resource limits for long-running processes:
Using --single-process can make the browser less stable. Only use it if you’re having memory issues.
Real-world example: Scraping service
CI/CD integration
GitHub Actions example
Test file:
Best practices