Not able to bypass Cloudflare on Ubuntu server (headless)
I have this simple code that I am running on an ubuntu server using DigitalOcean's droplets.
The program runs fine locally on my Mac with Intel chip. It runs in headless mode perfectly and is able to bypass Cloudflare and get the cf_clearance cookie, But I am not sure why it just fails to pass Cloudflare on the server.
I have spun new server instances and installed python, selenium, seleniumbase, chromedriver and everything I (think) need, but it just doesn't work on the Ubuntu server.
Am I missing something here? I have been trying to fix this for days now and no luck. I would really appreciate any help here
Here's my code:
from seleniumbase import Driver
import time
driver = Driver(uc=True, headless=True)
url = "https://2captcha.com/demo/cloudflare-turnstile-challenge"
driver.uc_open_with_reconnect(url, reconnect_time=30)
driver.sleep(15)
print(f"Page Title: {driver.title}")
print(f"Current URL: {driver.current_url}")
cf_Cookie = driver.get_cookie("cf_clearance")
if cf_Cookie:
print(f" cf_clearance cookie Value: {cf_Cookie['value']}")
else:
print("cf_clearance cookie not found. CloudFlare challenge likely still active.")
driver.quit()
I have GitHub Actions examples bypassing Cloudflare on Ubuntu here:
- https://github.com/mdmintz/undetected-testing/actions/runs/16586216994/job/46911820632
- https://github.com/mdmintz/undetected-testing/actions/runs/16586761429/job/46913528098
- https://github.com/mdmintz/undetected-testing/actions/runs/16586338954/job/46912304399
Note that you may want to use CDP Mode instead of plain UC Mode. CDP Mode gives you full stealth, (but WebDriver is disconnected to achieve this).
Using headless mode with UC Mode / CDP Mode is a duplicate of https://github.com/seleniumbase/SeleniumBase/issues/3820#issuecomment-2973786318 / https://github.com/seleniumbase/SeleniumBase/discussions/3161#discussioncomment-10726235: UC Mode and CDP Mode don't support headless mode because those use PyAutoGUI for lots of things, and PyAutoGUI doesn't work with a headless browser. (Headless mode isn't needed on Linux because of the special virtual display included.)
To activate the special virtual display on Linux, use SB(xvfb=True, uc=True). This is needed to run headed browsers in a GUI-less environment. Note that Driver() does not have the special virtual display that you need! You must use SB() for that.
If you encounter a CF CAPTCHA that isn't automatically bypassed, use sb.uc_gui_click_captcha() to get through.
Even if not using PyAutoGUI during UC Mode / CDP Mode, a headless browser can still leave a detectable fingerprint.
Hi Michael. Thank you for getting back.
I have tried this now on my Ubuntu OS and it's still not working
from seleniumbase import SB
import time
url = "https://2captcha.com/demo/cloudflare-turnstile-challenge"
with SB(uc=True, xvfb=True) as sb:
sb.activate_cdp_mode(url)
sb.sleep(15)
sb.uc_gui_click_captcha()
sb.sleep(15)
page_title = sb.cdp.get_title()
current_url = sb.cdp.get_current_url()
print(f"Page Title: {page_title}")
print(f"Current URL: {current_url}")
cf_cookie = None
all_cookies = sb.cdp.get_all_cookies()
for cookie in all_cookies:
if cookie.name == 'cf_clearance':
cf_cookie = cookie
break
if cf_cookie:
print(f" cf_clearance cookie Value: {cf_cookie.value}")
else:
print("cf_clearance cookie not found. CloudFlare challenge likely still active.")
On the Ubuntu server I tried running using xvfb-run -a python3 getcookie.py and it's still not able to bypass and get the cookie.
If your Ubuntu server has a non-residential IP address, then you might get blocked for that.
Headless mode leaves a detectable fingerprint no matter what you patch at the JS level. The issue isn't just navigator.webdriver, it's the whole hardware profile — canvas, audio context, GPU renderer string, all of it looks wrong when it's headless on a server. I switched to a Chromium build that's modified at the source level and runs inside a container with a real virtual display. The fingerprint is consistent and realistic. Haven't had to fight Cloudflare since.
I had a similar problem. It worked fine with "local" and headless=False passed the test, but not with headless=True. I found this solution:
https://github.com/seleniumbase/SeleniumBase/discussions/2525#discussioncomment-8537288
Now it works fine in both modes.
Python 3.9.25 seleniumbase 4.50.5