If your Selenium scripts throw sandbox-related errors when taking screenshots in Chrome, this guide walks you through the exact fixes. You’ll learn why the Chrome sandbox fails in certain environments (like Docker or CI servers) and how to configure Selenium and Chrome to bypass it safely, so your screenshots save without interruption.
What You’ll Need
- A working Selenium setup (Python, Java, or Node.js) with ChromeDriver installed
- Chrome browser installed on the machine running the tests
- Access to the terminal/command line to run your script
- Basic knowledge of editing your Selenium script’s options
Step-by-Step
- Identify the exact error message. Run your Selenium script and note the error. Common messages include:
Failed to move to new namespace: PID namespaces supported, Network namespace supported, but failed: errno = Operation not permitted, orERROR:zygote_host_impl_linux.cc(89) Running as root without --no-sandbox is not supported. This tells you which part of the sandbox is failing. - Add the
--no-sandboxargument to Chrome options. In your Selenium code, before creating the driver, add this argument. For example in Python:from selenium import webdriver from selenium.webdriver.chrome.options import Options opts = Options() opts.add_argument('--no-sandbox') driver = webdriver.Chrome(options=opts)In Java:
options.addArguments("--no-sandbox");In Node.js:chromeOptions.args.push("--no-sandbox"); - Add
--disable-dev-shm-usageas well. This is especially important in Docker containers where the/dev/shmshared memory is small. It makes Chrome use the/tmpdirectory instead. Add it the same way as the previous argument. - If running as root, also add
--disable-gpu. Some Linux environments without a GPU will fail with sandbox errors when GPU acceleration is attempted. Add this argument to avoid that. - Set the
chromeDriverbinary path if needed. Sometimes the error is not about the sandbox but about ChromeDriver not finding the Chrome binary. Usedriver = webdriver.Chrome(executable_path='/path/to/chromedriver', options=opts)(or the equivalent in your language) to point to the correct location. - Try headless mode. If the sandbox error only happens when Chrome tries to open a visible window, run headless:
opts.add_argument('--headless'). This often avoids sandbox permission issues entirely. - Update ChromeDriver and Chrome. Version mismatches can cause bizarre errors. Ensure ChromeDriver matches the installed Chrome major version. Check with
chromedriver --versionandgoogle-chrome --version(orchrome --version). - Run the script again and take a screenshot. After applying the arguments, your script should proceed. Test with a simple
driver.save_screenshot('test.png')to confirm.
Common Mistakes
- Only adding
--no-sandboxwithout--disable-dev-shm-usage. In Docker, the shared memory issue will still cause crashes or blank screenshots. - Adding arguments after the driver is created. Options must be set before
webdriver.Chrome()is called. - Using the wrong case or hyphen. Arguments are case-sensitive;
--no-sandboxis correct,--nosandboxis not. - Forgetting to close the driver. Leaving drivers open can cause resource leaks that mimic sandbox errors on subsequent runs.
Tips
- If you’re in a CI pipeline, set these arguments in the test setup code, not in each test method.
- Use a
try/finallyblock to always quit the driver, even if the screenshot fails. - For Docker, also consider adding
--ipc=hostto yourdocker runcommand to increase shared memory, which reduces the need for--disable-dev-shm-usage. - Keep your Chrome and ChromeDriver versions in sync by using a package manager or a version matrix in your CI.
Troubleshooting
If the error persists after following the steps:
- Check if the error message changed. A new error like “cannot find Chrome binary” means the sandbox issue is fixed, but the path is wrong. Use
--binary=/usr/bin/google-chromeif needed. - Run Chrome manually with the same flags. Open a terminal and type
google-chrome --no-sandbox --disable-dev-shm-usageto see if Chrome itself starts. If it doesn’t, the problem is with Chrome installation, not Selenium. - Check file permissions. Ensure the user running the script has execute permissions on the Chrome binary and ChromeDriver. Use
chmod +x chromedriverif necessary. - Try a different user. If you’re running as root, create a non-root user and run the script with that user. This is safer and often fixes sandbox errors without disabling the sandbox.
- Look at the full stack trace. Sometimes the error is actually about a missing dependency (like
libnss3) that appears as a sandbox error. Install missing libraries using your package manager.
Frequently Asked Questions
Is it safe to disable the Chrome sandbox?
Disabling the sandbox reduces Chrome’s isolation from the rest of the system. It’s generally safe in a controlled test environment or a disposable container, but avoid doing it on your personal machine for browsing untrusted websites.
Why does this happen only when taking screenshots?
Screenshots often trigger a new renderer process, and if the sandbox is misconfigured, that process fails. The error may not appear during navigation but surfaces when Chrome tries to render the page for capture.
Does this fix work for Firefox or Edge?
No, this is Chrome-specific. Firefox uses a different sandboxing mechanism, and Edge (which is Chromium-based) may need similar flags but with different names. For Edge, use --no-sandbox as well, but test separately.
Can I use these flags in production?
If you’re running a web scraping service in a container, yes, it’s common. But for regular browser use, keep the sandbox enabled. Only disable it when you have no other option.
Will this affect screenshot quality?
No, the flags only affect the process environment, not the rendering output. Your screenshots will be identical in quality and resolution.
How do I know which flag is actually fixing the issue?
Add one flag at a time and test. Start with --no-sandbox, then add --disable-dev-shm-usage, and so on. This helps you understand the root cause and avoid unnecessary flags.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Fix Apps Not Downloading From Play Store
- Secure a Samsung Galaxy After a Zero-Day Disclosure
- What to Check Before Installing Linux Alongside Windows
- Fix OEM Activation Error 0xc004c060 in Windows 10
- Why Does My App Run One Query for the List and One per Item?
- Fix Google Chrome Has Stopped Working