DEV Community

luisgustvo
luisgustvo

Posted on

Undetectable Web Automation: Camoufox Anti-Detect Browser & CapSolver CAPTCHA Bypass Guide

TL;DR: Achieve undetectable web automation at scale. Learn to combine Camoufox for advanced browser fingerprint evasion and CapSolver for automatically solving complex CAPTCHAs (Turnstile, reCAPTCHA v2/v3). High success rates guaranteed.

Introduction: The Dual Challenge of Modern Web Automation

Web automation is a critical component for data scraping, market research, and automated testing. However, the landscape has become increasingly hostile, with modern websites employing two primary defense layers to block automated scripts: browser fingerprinting and CAPTCHA challenges.

A successful automation setup must address both:

  1. Fingerprint Evasion: The script must appear to be a genuine, human-operated browser, spoofing all detectable parameters (WebGL, canvas, screen size, etc.).
  2. CAPTCHA Resolution: The script must be able to automatically and reliably solve challenges like Cloudflare Turnstile and reCAPTCHA.

This guide details how to integrate Camoufox, an open-source anti-detect browser, with CapSolver, an AI-powered CAPTCHA solving service, to create a seamless, undetectable automation pipeline.

Why This Combination Works

Tool Primary Function Anti-Bot Layer Bypassed
Camoufox Native-level browser fingerprint spoofing Behavioral and Fingerprint Detection
CapSolver AI-driven CAPTCHA solving API Challenge-Response Systems (CAPTCHAs)

Deep Dive into Camoufox: The Stealth Browser

Camoufox is a specialized, minimalist build of Firefox designed for maximum stealth in automation. Unlike many commercial anti-detect solutions that rely on easily detectable JavaScript overrides, Camoufox implements its fingerprint spoofing at the C++ native level.

Core Features for Undetectable Automation

  • Native Fingerprint Spoofing: Spoofs critical browser properties (navigator, WebGL, WebRTC, fonts, screen dimensions) directly within the browser engine, making detection significantly harder.
  • Humanized Interaction: Includes a built-in algorithm for realistic, human-like mouse movements, avoiding the tell-tale signs of robotic cursor paths.
  • BrowserForge Integration: Utilizes real-world device distribution data to generate highly realistic and diverse browser fingerprints.
  • Proxy-Aware Geolocation: Automatically sets the browser's timezone, locale, and geolocation based on the connected proxy IP address.

Installation and Basic Setup

Camoufox is easily installed via pip and requires downloading the custom browser binary:

# Install the Python package with geoip support
pip install -U camoufox[geoip]

# Download the Camoufox browser binary
camoufox fetch
Enter fullscreen mode Exit fullscreen mode

Basic Python Usage:

from camoufox.sync_api import Camoufox

# The 'humanize=True' flag enables human-like mouse movements
with Camoufox(humanize=True) as browser:
    page = browser.new_page()
    page.goto("https://example.com")
    # Your automation logic continues here...
Enter fullscreen mode Exit fullscreen mode

Deep Dive into CapSolver: The AI-Powered Solver

CapSolver is a robust, AI-driven service that provides solutions for virtually all modern CAPTCHA types. It offers a simple, unified API for submitting challenges and retrieving tokens.

Supported CAPTCHA Challenges

CapSolver is designed to handle the most challenging anti-bot systems:

  • Cloudflare Turnstile: The modern, non-intrusive challenge that has replaced many older systems.
  • reCAPTCHA v2 & v3: Supports both the image-based and score-based verification methods.
  • AWS WAF: Challenges deployed by Amazon Web Services.
  • Imagetotext and more.

Getting Started

  1. Sign Up: Create an account on the CapSolver website.
  2. Fund Your Account: Add credits to use the solving service.
  3. Get API Key: Retrieve your unique API key from the dashboard.

Pro Tip: Use the code CAMOUFOX when registering for CapSolver to receive bonus credits and jumpstart your automation!


Integration Methods: Combining Stealth and Solving

There are two primary ways to integrate CapSolver with Camoufox: via API (recommended for control) or via a browser extension (for simplicity).

Method 1: Direct API Integration (Recommended)

This method provides the most control and flexibility, allowing you to trigger the CAPTCHA solving process precisely when needed within your code.

Setup Requirements:

pip install camoufox[geoip] httpx
Enter fullscreen mode Exit fullscreen mode

Core Asynchronous API Pattern:

The following utility functions handle the core logic of submitting a task to CapSolver and polling for the result.

import asyncio
import httpx

CAPSOLVER_API_KEY = "YOUR_API_KEY"
CAPSOLVER_API = "https://api.capsolver.com"


async def create_task(task_payload: dict) -> str:
    """Create a CAPTCHA solving task and return the task ID."""
    async with httpx.AsyncClient() as client:
        response = await client.post(
            f"{CAPSOLVER_API}/createTask",
            json={
                "clientKey": CAPSOLVER_API_KEY,
                "task": task_payload
            }
        )
        result = response.json()
        if result.get("errorId") != 0:
            raise Exception(f"CapSolver error: {result.get('errorDescription')}")
        return result["taskId"]


async def get_task_result(task_id: str, max_attempts: int = 120) -> dict:
    """Poll for task result until solved or timeout."""
    async with httpx.AsyncClient() as client:
        for _ in range(max_attempts):
            response = await client.post(
                f"{CAPSOLVER_API}/getTaskResult",
                json={
                    "clientKey": CAPSOLVER_API_KEY,
                    "taskId": task_id
                }
            )
            result = response.json()

            if result.get("status") == "ready":
                return result["solution"]
            elif result.get("status") == "failed":
                raise Exception(f"Task failed: {result.get('errorDescription')}")

            await asyncio.sleep(1)

    raise TimeoutError("CAPTCHA solving timed out")


async def solve_captcha(task_payload: dict) -> dict:
    """Complete CAPTCHA solving workflow."""
    task_id = await create_task(task_payload)
    return await get_task_result(task_id)
Enter fullscreen mode Exit fullscreen mode

Method 2: Browser Extension Integration

For simpler, less code-intensive automation, you can use the CapSolver browser extension.

Installation Steps:

  1. Download the CapSolver extension from the official website.
  2. Extract the extension files to a local directory.
  3. Load the extension into Camoufox using the addons parameter:
from camoufox.sync_api import Camoufox

with Camoufox(
    addons=["/path/to/capsolver-extension"],
    headless=False  # Note: Extensions typically require headed mode
) as browser:
    page = browser.new_page()
    # The extension will automatically detect and solve CAPTCHAs as they appear
Enter fullscreen mode Exit fullscreen mode

Practical Code Examples

Example 1: Solving Cloudflare Turnstile

This example demonstrates how to use the API integration pattern to solve a Turnstile challenge and inject the resulting token into the page for form submission.

import asyncio
from camoufox.async_api import AsyncCamoufox
import httpx

# Assume the utility functions (create_task, get_task_result, solve_captcha) from Method 1 are defined here

CAPSOLVER_API_KEY = "YOUR_API_KEY"
CAPSOLVER_API = "https://api.capsolver.com"


async def solve_turnstile(site_key: str, page_url: str) -> str:
    """Solve Cloudflare Turnstile and return the token."""
    # This is a simplified version of the utility functions for demonstration
    task_payload = {
        "type": "AntiTurnstileTaskProxyLess",
        "websiteURL": page_url,
        "websiteKey": site_key,
    }

    # In a real script, you would call the utility functions here
    # solution = await solve_captcha(task_payload)
    # return solution["token"]

    # Placeholder for actual solving logic
    print("Submitting Turnstile task to CapSolver...")
    await asyncio.sleep(5) # Simulate network delay
    return "CF_TOKEN_SIMULATED_SUCCESS_1234567890"


async def main():
    target_url = "https://example.com/protected-page"
    turnstile_site_key = "0x4XXXXXXXXXXXXXXXXX"  # Replace with the actual site key

    async with AsyncCamoufox(
        humanize=True,
        headless=False,
        os="windows" # Spoofing a Windows OS
    ) as browser:
        page = await browser.new_page()
        await page.goto(target_url)

        # Wait for the Turnstile input field to appear
        await page.wait_for_selector('input[name="cf-turnstile-response"]', timeout=10000)

        # Solve the CAPTCHA using CapSolver
        token = await solve_turnstile(turnstile_site_key, target_url)
        print(f"Got Turnstile token: {token[:50]}...")

        # Inject the token into the hidden input field
        await page.evaluate(f'''
            document.querySelector('input[name="cf-turnstile-response"]').value = "{token}";

            // Trigger any required JavaScript callback function
            const callback = document.querySelector('[data-callback]');
            if (callback) {{
                const callbackName = callback.getAttribute('data-callback');
                if (window[callbackName]) {{
                    window[callbackName]('{token}');
                }}
            }}
        ''')

        # Submit the form or proceed with the protected action
        await page.click('button[type="submit"]')
        await page.wait_for_load_state("networkidle")

        print("Successfully bypassed Turnstile and proceeded!")


if __name__ == "__main__":
    asyncio.run(main())
Enter fullscreen mode Exit fullscreen mode

Example 2: Solving reCAPTCHA v2

For reCAPTCHA v2, the process is similar: solve the challenge off-site and inject the resulting token.

import asyncio
from camoufox.async_api import AsyncCamoufox
import httpx

# Assume the utility functions (create_task, get_task_result, solve_captcha) are defined

CAPSOLVER_API_KEY = "YOUR_API_KEY"
CAPSOLVER_API = "https://api.capsolver.com"


async def solve_recaptcha_v2(site_key: str, page_url: str) -> str:
    """Solve reCAPTCHA v2 and return the token."""
    task_payload = {
        "type": "ReCaptchaV2TaskProxyLess",
        "websiteURL": page_url,
        "websiteKey": site_key,
    }

    # In a real script, you would call the utility functions here
    # solution = await solve_captcha(task_payload)
    # return solution["gRecaptchaResponse"]

    # Placeholder for actual solving logic
    print("Submitting reCAPTCHA v2 task to CapSolver...")
    await asyncio.sleep(5) # Simulate network delay
    return "RECAPTCHA_TOKEN_SIMULATED_SUCCESS_9876543210"


async def main():
    target_url = "https://example.com/recaptcha-page"
    recaptcha_site_key = "6LXXXXXXXXXXXXXXXXXXXXXX" # Replace with the actual site key

    async with AsyncCamoufox(
        humanize=True,
        headless=True, # Headless mode is often sufficient for token injection
        os="linux"
    ) as browser:
        page = await browser.new_page()
        await page.goto(target_url)

        # Solve the CAPTCHA
        token = await solve_recaptcha_v2(recaptcha_site_key, target_url)
        print(f"Got reCAPTCHA token: {token[:50]}...")

        # Inject the token into the hidden textarea
        await page.evaluate(f'''
            document.getElementById('g-recaptcha-response').value = "{token}";
        ''')

        # Submit the form
        await page.click('#submit-button')
        await page.wait_for_load_state("networkidle")

        print("Successfully bypassed reCAPTCHA v2!")


if __name__ == "__main__":
    asyncio.run(main())
Enter fullscreen mode Exit fullscreen mode

Best Practices for Robust Automation

To maximize the stability and longevity of your automation scripts, consider these advanced techniques:

1. Dynamic Fingerprint Generation

While Camoufox handles the core spoofing, you can further enhance stealth by dynamically generating specific screen sizes and other parameters using browserforge.

from camoufox.async_api import AsyncCamoufox
from browserforge.fingerprints import Screen

# Constrain to common screen sizes for realism
screen = Screen(
    min_width=1280,
    max_width=1920,
    min_height=720,
    max_height=1080
)

async with AsyncCamoufox(
    os="windows",
    screen=screen, # Pass the generated screen object
) as browser:
    # Automation continues with a highly realistic, randomized screen size
    pass
Enter fullscreen mode Exit fullscreen mode

2. Humanized Delays

Avoid triggering rate limits and behavioral flags by introducing random, human-like delays between actions.

import asyncio
import random

async def human_delay():
    """Introduce a random delay to mimic human reaction time."""
    await asyncio.sleep(random.uniform(1.0, 3.0))

# Use between actions
await page.click('button')
await human_delay()
await page.fill('input', 'text')
await human_delay()
Enter fullscreen mode Exit fullscreen mode

3. Robust Error Handling and Retries

Network issues, temporary service outages, or brief anti-bot spikes can cause failures. Implement retry logic for CAPTCHA solving to ensure resilience.

async def solve_with_retry(task_payload: dict, max_retries: int = 3) -> dict:
    """Solve CAPTCHA with retry logic."""
    for attempt in range(max_retries):
        try:
            # Assuming solve_captcha is the utility function defined earlier
            return await solve_captcha(task_payload) 
        except TimeoutError:
            if attempt < max_retries - 1:
                print(f"Timeout, retrying... ({attempt + 1}/{max_retries})")
                await asyncio.sleep(5)
            else:
                raise
        except Exception as e:
            # Handle specific errors like insufficient balance
            if "balance" in str(e).lower():
                raise  
            if attempt < max_retries - 1:
                await asyncio.sleep(2)
            else:
                raise
Enter fullscreen mode Exit fullscreen mode

Conclusion

The integration of Camoufox anti-detect browser and CapSolver CAPTCHA solver provides a comprehensive, two-pronged defense against modern anti-bot systems. By handling both native-level fingerprint evasion and AI-powered CAPTCHA resolution, this powerful combination allows developers to build reliable, scalable, and truly undetectable web automation pipelines.

Ready to supercharge your web automation?

Don't forget to use code CAMOUFOX when signing up at CapSolver to receive bonus credits!


Frequently Asked Questions (FAQ)

Q: Which CAPTCHA types have the highest success rate?

A: CapSolver supports all major types, but Cloudflare Turnstile and reCAPTCHA v2/v3 generally have the highest success rates due to the service's optimization for these common challenges.

Q: Can I run this setup in headless mode?

A: Yes, Camoufox fully supports headless mode while maintaining its fingerprint spoofing capabilities. Headless mode is ideal for token-based challenges like reCAPTCHA v3. For visual challenges (like reCAPTCHA v2 image selection), running in headed mode may sometimes yield better results.

Q: How do I locate the site key for a CAPTCHA?

A: You can find the site key by inspecting the page source:

  • Turnstile: Look for the data-sitekey attribute or elements containing cf-turnstile.
  • reCAPTCHA: Look for the data-sitekey attribute on the g-recaptcha div element.

Q: What should I check if CAPTCHA solving fails?

A: Common troubleshooting steps include:

  1. Verify your CapSolver API key and ensure your account has sufficient balance.
  2. Confirm that the site_key and page_url submitted to the API are correct.
  3. For reCAPTCHA v3, ensure the action parameter and minimum score threshold are correctly configured.
  4. Implement the retry logic discussed in the best practices section.

Q: Does Camoufox support Selenium?

A: Camoufox is built on Playwright's architecture. While the CapSolver API integration pattern can be adapted to any framework, Camoufox itself is designed to be used with its native Python API, which is based on Playwright.

Top comments (0)