You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Undetected Chromedriver无头模式下无法绕过Cloudflare机器人防护的解决方案咨询

How to Bypass Cloudflare Bot Protection in Headless Mode with Undetected Chromedriver?

I'm trying to run my Python Selenium bot using undetected-chromedriver (imported as uc) in headless mode, but unfortunately, I can't get past Cloudflare's bot protection. Here's my current configuration code:

options = uc.ChromeOptions()
options.headless=True
options.add_argument("--window-size=1920,1080")
options.add_argument('--ignore-certificate-errors')
options.add_argument('--allow-running-insecure-content')
options.add_argument('--headless')
scrap = uc.Chrome(use_subprocess=True,options=options)

Cloudflare Bot Protection Verification Page


Answer

Hey there, I’ve dealt with this exact headache a bunch of times—headless mode even with undetected-chromedriver can leave tiny fingerprints that Cloudflare’s systems pick up instantly. Let’s walk through some fixes that consistently work:

  • Upgrade to Chrome’s new headless mode: The old --headless flag is basically a red flag for Cloudflare. Chrome 112+ introduced a far more undetectable headless mode that behaves nearly identical to a regular browser window. Swap out your headless settings like this:

    # Replace the old headless lines
    options.headless = False
    options.add_argument("--headless=new")
    
  • Add realistic browser fingerprint arguments: Cloudflare checks for all sorts of small details to spot bots. Add these lines to mimic a real human’s browser:

    # Set a valid user agent (update to match your Chrome version)
    options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36")
    # Set language to match common user settings
    options.add_argument("--lang=en-US,en;q=0.9")
    # Disable automation flags that give bots away
    options.add_argument("--disable-blink-features=AutomationControlled")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    
  • Slow down to human speed: Bots move way faster than people, and Cloudflare notices this. Add short delays between actions, or use explicit waits instead of fixed sleeps to wait for elements to load naturally:

    import time
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    scrap.get("your-target-url.com")
    # Wait for Cloudflare's check to finish (adjust timeout as needed)
    WebDriverWait(scrap, 20).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "main"))  # Wait for your target page's main content
    )
    # Add a short delay before interacting
    time.sleep(1.5)
    
  • Use a residential proxy: If your IP is being flagged (common with datacenter IPs), switch to a residential proxy—these come from real home networks, so Cloudflare is less likely to block them. Add it to your options like this:

    options.add_argument('--proxy-server=http://your-residential-proxy:port')
    
  • Keep undetected-chromedriver updated: The maintainers are constantly pushing updates to bypass new Cloudflare detection methods. Run this command to make sure you’re on the latest version:

    pip install --upgrade undetected-chromedriver
    

Give these a try—combining the new headless mode with fingerprint tweaks usually does the trick for me. If you’re still stuck, sometimes adding a small scroll or random mouse movement can help sell the "human" act even more!


内容的提问来源于stack exchange,提问作者Osc4r

火山引擎 最新活动