undetected-chromedriver is a Python library that patches Selenium's ChromeDriver to reduce bot detection signals. It handles Chrome version matching, removes navigator.webdriver flags, and modifies other browser fingerprints that sites check. When a site still presents a CAPTCHA despite stealth measures, CaptchaAI solves it and returns a token you inject into the page.
This guide shows how to combine both tools — use undetected-chromedriver for stealth browsing and CaptchaAI for any CAPTCHAs that still appear.
What you need
| Requirement | Details |
|---|---|
| CaptchaAI API key | captchaai.com |
| Python 3.8+ | |
| Chrome browser | Installed on your system |
Install dependencies:
pip install undetected-chromedriver requests
Basic setup
import undetected_chromedriver as uc
import requests
import time
def create_stealth_browser():
"""Create an undetected Chrome browser instance."""
options = uc.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--window-size=1920,1080")
driver = uc.Chrome(options=options)
return driver
Solving reCAPTCHA v2 in a stealth session
Step 1: Navigate and extract the sitekey
API_KEY = "YOUR_API_KEY"
def extract_recaptcha_sitekey(driver):
"""Extract reCAPTCHA v2 sitekey from the page."""
try:
element = driver.find_element("css selector", "[data-sitekey]")
return element.get_attribute("data-sitekey")
except Exception:
# Try finding in iframe src
iframes = driver.find_elements("css selector", "iframe[src*='recaptcha']")
for iframe in iframes:
src = iframe.get_attribute("src")
if "k=" in src:
return src.split("k=")[1].split("&")[0]
return None
Step 2: Solve with CaptchaAI
def solve_recaptcha_v2(sitekey, pageurl):
"""Submit reCAPTCHA v2 to CaptchaAI and return the token."""
submit = requests.post("https://ocr.captchaai.com/in.php", data={
"key": API_KEY,
"method": "userrecaptcha",
"googlekey": sitekey,
"pageurl": pageurl,
"json": 1
}).json()
if submit.get("status") != 1:
raise RuntimeError(f"Submit error: {submit.get('request')}")
task_id = submit["request"]
time.sleep(20)
for _ in range(30):
result = requests.get("https://ocr.captchaai.com/res.php", params={
"key": API_KEY, "action": "get", "id": task_id, "json": 1
}).json()
if result.get("status") == 1:
return result["request"]
if result.get("request") != "CAPCHA_NOT_READY":
raise RuntimeError(f"Solve error: {result['request']}")
time.sleep(5)
raise TimeoutError("Solve timed out")
Step 3: Inject the token and submit
def inject_recaptcha_token(driver, token):
"""Inject the solved token into the page and submit."""
driver.execute_script(f'''
document.getElementById("g-recaptcha-response").innerHTML = "{token}";
document.getElementById("g-recaptcha-response").style.display = "block";
''')
# If there's a callback function, trigger it
driver.execute_script(f'''
if (typeof ___grecaptcha_cfg !== 'undefined') {{
var clients = ___grecaptcha_cfg.clients;
for (var key in clients) {{
var client = clients[key];
if (client && client.callback) {{
client.callback("{token}");
}}
}}
}}
''')
Complete workflow: Login with reCAPTCHA
import undetected_chromedriver as uc
import requests
import time
API_KEY = "YOUR_API_KEY"
def solve_recaptcha(sitekey, pageurl):
submit = requests.post("https://ocr.captchaai.com/in.php", data={
"key": API_KEY, "method": "userrecaptcha",
"googlekey": sitekey, "pageurl": pageurl, "json": 1
}).json()
if submit.get("status") != 1:
raise RuntimeError(f"Submit error: {submit.get('request')}")
task_id = submit["request"]
time.sleep(20)
for _ in range(30):
result = requests.get("https://ocr.captchaai.com/res.php", params={
"key": API_KEY, "action": "get", "id": task_id, "json": 1
}).json()
if result.get("status") == 1:
return result["request"]
if result.get("request") != "CAPCHA_NOT_READY":
raise RuntimeError(f"Solve error: {result['request']}")
time.sleep(5)
raise TimeoutError("Solve timed out")
def main():
driver = uc.Chrome()
try:
# Navigate to target page
driver.get("https://example.com/login")
time.sleep(3)
# Fill in form fields
driver.find_element("id", "username").send_keys("user")
driver.find_element("id", "password").send_keys("pass")
# Extract sitekey
element = driver.find_element("css selector", "[data-sitekey]")
sitekey = element.get_attribute("data-sitekey")
pageurl = driver.current_url
print(f"Sitekey: {sitekey}")
# Solve CAPTCHA
token = solve_recaptcha(sitekey, pageurl)
print(f"Token: {token[:50]}...")
# Inject token
driver.execute_script(
f'document.getElementById("g-recaptcha-response").innerHTML = "{token}";'
)
# Submit form
driver.find_element("id", "submit-btn").click()
time.sleep(3)
print(f"Current URL: {driver.current_url}")
finally:
driver.quit()
if __name__ == "__main__":
main()
Solving Cloudflare Turnstile
For sites with Cloudflare Turnstile widgets, extract the sitekey and solve via the Turnstile method:
def solve_turnstile(sitekey, pageurl):
submit = requests.post("https://ocr.captchaai.com/in.php", data={
"key": API_KEY, "method": "turnstile",
"sitekey": sitekey, "pageurl": pageurl, "json": 1
}).json()
if submit.get("status") != 1:
raise RuntimeError(f"Submit error: {submit.get('request')}")
task_id = submit["request"]
time.sleep(10)
for _ in range(30):
result = requests.get("https://ocr.captchaai.com/res.php", params={
"key": API_KEY, "action": "get", "id": task_id, "json": 1
}).json()
if result.get("status") == 1:
return result["request"]
if result.get("request") != "CAPCHA_NOT_READY":
raise RuntimeError(f"Solve error: {result['request']}")
time.sleep(5)
raise TimeoutError("Solve timed out")
# Inject Turnstile token
def inject_turnstile_token(driver, token):
driver.execute_script(f'''
var input = document.querySelector('[name="cf-turnstile-response"]');
if (input) input.value = "{token}";
''')
Troubleshooting
| Issue | Cause | Fix |
|---|---|---|
| Chrome version mismatch | UC can't find matching driver | Update Chrome or specify version_main in uc.Chrome() |
| CAPTCHA still appears | Site uses advanced fingerprinting | This is expected — solve with CaptchaAI |
| Token injection fails | Wrong element ID or callback | Check the page's reCAPTCHA implementation |
WebDriverException |
Chrome crashed | Add --no-sandbox and --disable-dev-shm-usage |
FAQ
Why use undetected-chromedriver with CaptchaAI?
Undetected-chromedriver reduces bot detection signals so fewer CAPTCHAs appear. When CAPTCHAs still trigger, CaptchaAI solves them. The combination gives the highest success rate.
Does undetected-chromedriver work in headless mode?
Yes, but headless mode may trigger more CAPTCHAs. Use options.add_argument("--headless=new") for Chrome's new headless mode.
Can I use this with Selenium Grid?
Yes, but undetected-chromedriver needs to patch the local driver binary. For distributed setups, pre-patch the driver on each node.
What if the site detects automation despite UC?
Some sites use advanced detection beyond what UC patches. CaptchaAI handles the resulting CAPTCHAs regardless of detection method.
Add CaptchaAI to your browser automation
Get your API key at captchaai.com. Combine stealth browsing with reliable CAPTCHA solving.
Discussions (0)
Join the conversation
Sign in to share your opinion.
Sign InNo comments yet.