Debug Headless Chrome Access Denied Errors in Selenium Python
In this article
When running Selenium with headless Chrome, an "Access Denied" error usually means the browser is being blocked by a website's anti-bot protection, a proxy, or a misconfigured Chrome option. This guide walks you through diagnosing and fixing these errors step by step, covering common causes like user-agent strings, missing headers, and Chrome flags.
What You'll Need
- Python 3.6+ installed
- Selenium installed (
pip install selenium) - ChromeDriver matching your Chrome version
- Google Chrome browser installed
- Basic knowledge of Python and Selenium
Step-by-Step
- Reproduce the error in non-headless mode. Run your script without
--headlessto see if the error only appears in headless mode. If it does, the issue is likely related to headless-specific browser fingerprints. If it appears in both modes, the problem is elsewhere (e.g., network, proxy, or site blocking). - Check the exact error message. Print the full exception traceback. Look for HTTP status codes (403, 401, 429) or Selenium-specific errors like
WebDriverException. This helps narrow down whether it's a permission issue, rate limiting, or a bot detection block. - Set a realistic user-agent. Headless Chrome sends a default user-agent that includes "HeadlessChrome", which many sites block. Override it with a normal Chrome user-agent string. Example:
options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36') - Add common headers. Some sites check for headers like Accept-Language or Accept-Encoding . Use Chrome DevTools Protocol (CDP) to set headers before navigation:
- Disable automation flags. Remove the
--enable-automationflag and setexcludeSwitchesto hide Chrome's automation banner:options.add_experimental_option('excludeSwitches', ['enable-automation']) - Use a real Chrome profile. Launch Chrome with a non-default user-data-dir and disable the default headless flags that reveal automation:
options.add_argument('--user-data-dir=/path/to/profile') - Test with a different WebDriver version. Sometimes ChromeDriver versions mismatch Chrome, causing unexpected behavior. Ensure both are up-to-date and compatible.
- Check for proxy or firewall interference. If you're behind a corporate proxy, configure Selenium to use it explicitly via
--proxy-serveror environment variables. Headless mode may not pick up system proxy settings automatically. - Add a small delay before navigation. Some sites block immediate requests. Use
time.sleep(2)beforedriver.get()to mimic human behavior. - Try headless=new mode. Chrome 112+ supports a new headless mode that behaves more like the full browser. Use
options.add_argument('--headless=new')instead of the old--headless.
Common Mistakes
- Forgetting to set a user-agent, leaving the default "HeadlessChrome" string.
- Using an outdated ChromeDriver that doesn't match the installed Chrome version.
- Overriding headers incorrectly, causing malformed requests.
- Assuming the error is site-specific when it's actually a network issue.
- Not clearing the browser cache between runs, leading to stale cookies or tokens.
Tips
- Use a headless browser detection test site (e.g., a page that shows your user-agent and headers) to verify what the site sees.
- Keep your Chrome and ChromeDriver versions in sync; check them with
chrome --versionandchromedriver --version. - If the site uses Cloudflare or similar, consider adding a random delay and varying your user-agent across sessions.
- Use
--disable-blink-features=AutomationControlledto remove thenavigator.webdriverflag. - Log all requests and responses using Selenium's
driver.requests(if using selenium-wire) to see the exact HTTP status codes.
Troubleshooting
If the error persists after trying the steps above:
- Check if the site blocks all automated browsers. Try a simple script that just navigates to a known non-protected site (e.g., example.com) to confirm your setup works.
- Inspect the page source. Sometimes the error is a custom message, not a standard HTTP error. Use
driver.page_sourceto see what the site returns. - Try a different browser. Switch to Firefox with geckodriver to see if the issue is Chrome-specific.
- Disable JavaScript temporarily. Some sites block based on JS execution; disable it via
--disable-javascriptto test. - Check your network. Use
curlor a regular browser to access the same URL from your machine to rule out IP blocks. - Update Selenium. Older versions may not support the latest Chrome features. Run
pip install --upgrade selenium.
Frequently Asked Questions
Why does headless Chrome get "Access Denied" when regular Chrome works?
Headless Chrome has a different fingerprint (user-agent, JavaScript behavior, and missing extensions). Many sites detect this and block it. Overriding the user-agent and disabling automation flags often resolves it.
Does setting a user-agent always fix the issue?
Not always. Some sites also check for other signals like navigator.webdriver or TLS fingerprints. You may need to combine user-agent override with other flags.
Can I use headless mode with an existing Chrome profile?
Yes, but be careful: using a real profile can carry cookies and extensions that might trigger different behavior. It's often better to create a fresh profile for testing.
Is there a way to see what the site sees?
You can use driver.execute_script("return navigator.userAgent") and driver.execute_script("return navigator.webdriver") to inspect the values from within the browser.
What if the site uses CAPTCHA?
CAPTCHA challenges are not "Access Denied" errors. If you encounter a CAPTCHA, you'll need to handle it manually or use a CAPTCHA-solving service, which is beyond the scope of this guide.
Does using Selenium with headless Chrome violate website terms?
It can, depending on the site's terms of service. Always check the website's robots.txt and terms before automated access. This guide is for educational purposes and legitimate testing.