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?