Cloudflare blocking headless Chrome with undetected chromedriver in Selenium

I’m working on a web scraping project that needs to run in headless mode for deployment purposes. The script works fine with a regular browser window, but as soon as I enable headless mode, Cloudflare starts blocking my requests.

The error I get is:

selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: There is no admin configuration!
Message: unexpected alert open: {Alert text : There is no admin configuration!}
(Session info: headless chrome=105.0.5195.127)

When I check the page content, I can see it’s definitely a Cloudflare access denied message:

page_content = browser.find_element(By.CSS_SELECTOR, 'div').get_attribute('textContent')
print(page_content)

# Output shows:
# 'Access denied - You cannot access this site. Please contact support if needed.
# Ray ID: 74d6c9316f273b7c - Timestamp: 2022-09-20 01:28:42 UTC
# Your IP: ##.###.##.### - URL: example-site.com/page
# Error code: 1020 - Server: FL_106F56
# User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'

My current setup looks like this:

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import undetected_chromedriver as uc

TARGET_URL = 'https://example-betting-site.com/sports/live'

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--window-size=1920,1080')
chrome_options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.5195.127 Safari/537.36')

browser = uc.Chrome(options=chrome_options)
browser.get(TARGET_URL)

I already switched to undetected chromedriver which solved the initial Cloudflare issues when running with a visible browser window. But now headless mode is causing the same problem again. Anyone know how to get around this?

Yeah, this is super common with Cloudflare’s detection. Even undetected chromedriver can’t hide all the signs they’re looking for - they’ve gotten really good at catching automation despite stealth techniques. What worked for me was using Xvfb on Linux instead of true headless mode. The browser thinks it has a display but your server doesn’t need a GUI. Just install xvfb-run and wrap your script with it. You could also try residential proxies with session rotation, but that gets complicated and expensive. Some people have luck adding random delays between requests and mimicking human browsing patterns, though results depend on how that specific site has Cloudflare configured.

I’ve hit this exact problem before. Cloudflare flags specific headless Chrome fingerprints that undetected chromedriver can’t hide. Headless mode changes the WebGL renderer string and other browser properties Cloudflare watches for. What fixed it for me was switching to playwright-stealth instead of selenium with undetected chromedriver. Playwright handles headless detection way better. You can also try adding these to your current setup: chrome_options.add_argument(‘–disable-blink-features=AutomationControlled’) and chrome_options.add_experimental_option(‘excludeSwitches’, [‘enable-automation’]). Other things that helped: use a different user agent string (yours might be flagged) and try --window-size=1366,768 instead of 1920x1080 since it’s a more common resolution.

Cloudflare’s gotten way smarter lately. Try running Chrome with --disable-web-security and --disable-features=VizDisplayCompositor flags. Also ditch that custom user-agent - it’s probably getting flagged. Let undetected chromedriver handle it automatically. I’ve seen people have better luck with puppeteer-extra-plugin-stealth for headless scraping, but switching from Selenium is a pain.