playwright-captcha

repository·main·Indexed 43 days ago

https://github.com/techinz/playwright-captcha

A Python library for automating captcha solving in Playwright, Patchright, and Camoufox. Version 0.1.4 supports Cloudflare Turnstile, Cloudflare Interstitial, and reCAPTCHA V2 & V3 using either a browser-based ClickSolver or API-based solvers like TwoCaptchaSolver and TenCaptchaSolver.

Tokens
2.5K
Snippets
10
Records
11
Agent score
6%

What's inside playwright-captcha

  1. Configure FrameworkType for different browsers

    main

    When initializing solvers, you must specify the FrameworkType to ensure compatibility with the browser engine being used.

    • FrameworkType.PLAYWRIGHT: Standard Playwright browsers.
    • FrameworkType.PATCHRIGHT: For use with the patchright library.
    • FrameworkType.CAMOUFOX: For use with the camoufox library.
  2. Configure TwoCaptchaSolver options

    main

    You can customize the retry logic and manual data overrides for the TwoCaptchaSolver:

    • framework: FrameworkType (PLAYWRIGHT, PATCHRIGHT, or CAMOUFOX).
    • page: The Playwright page object.
    • async_two_captcha_client: An instance of the AsyncTwoCaptcha client.
    • max_attempts: Number of solving attempts.
    • attempt_delay: Delay between attempts in seconds.
    • sitekey: (Optional) Manually specify the site key if automatic detection fails.
    # API solver with custom settings (TwoCaptcha)
    solver = TwoCaptchaSolver(framework=framework, 
                                page=page, 
                                async_two_captcha_client=captcha_client,
                                max_attempts=3,
                                attempt_delay=10,
                                
                                # also you can specify the captcha data like sitekey manually if it fails to detect automatically
                                sitekey='sitekey'
    )
  3. Configure ClickSolver options

    main

    You can customize the retry logic for the ClickSolver using the following parameters:

    • framework: FrameworkType (PLAYWRIGHT, PATCHRIGHT, or CAMOUFOX).
    • page: The Playwright page object.
    • max_attempts: Number of solving attempts (default behavior applies if not set).
    • attempt_delay: Delay between attempts in seconds.
    # Click solver with custom settings
    solver = ClickSolver(
        framework=framework, # Framework type (PLAYWRIGHT, PATCHRIGHT, CAMOUFOX)
        page=page,
        max_attempts=5,      # Number of solving attempts
        attempt_delay=3      # Delay between attempts (seconds)
    )
  4. Use the Click Solver for automated clicking

    main

    The ClickSolver uses browser automation to find and click captcha elements. This method works best with stealthy browser patches like Camoufox or Patchright to avoid detection.

    Workflow:

    1. Initialize the ClickSolver using an async context manager before navigating to the target page.
    2. Navigate to the page containing the captcha.
    3. Call solve_captcha specifying the captcha_container (usually the page object) and the captcha_type.
    import asyncio
    from playwright.async_api import async_playwright
    from playwright_captcha import CaptchaType, ClickSolver, FrameworkType
    
    async def solve_captcha():
        async with async_playwright() as playwright:
            browser = await playwright.chromium.launch(headless=True)
            page = await browser.new_page()
            
            framework = FrameworkType.PLAYWRIGHT
            
            # Create solver before navigating to the page
            async with ClickSolver(framework=framework, page=page) as solver:
                # Navigate to your target page
                await page.goto('https://example.com/with-captcha')
                
                # Solve the captcha
                await solver.solve_captcha(
                    captcha_container=page,
                    captcha_type=CaptchaType.CLOUDFLARE_TURNSTILE
                )
            
            # Continue with your automation...
    
    asyncio.run(solve_captcha())
  5. Use API Solvers (TwoCaptcha or TenCaptcha)

    main

    API Solvers extract captcha data (like site keys) and send them to external services for solving.

    Supported Solvers:

    • TwoCaptchaSolver: Supports Cloudflare Interstitial, Cloudflare Turnstile, reCAPTCHA v2, and reCAPTCHA v3.
    • TenCaptchaSolver: Supports reCAPTCHA v2 and reCAPTCHA v3.

    Workflow:

    1. Initialize your external service client (e.g., AsyncTwoCaptcha).
    2. Initialize the solver using an async context manager before navigating to the target page.
    3. Navigate to the page.
    4. Call solve_captcha with the appropriate CaptchaType.
    import asyncio
    import os
    from playwright.async_api import async_playwright
    from twocaptcha import AsyncTwoCaptcha
    from playwright_captcha import CaptchaType, TwoCaptchaSolver, FrameworkType
    
    async def solve_with_2captcha():
        # Initialize 2Captcha client
        captcha_client = AsyncTwoCaptcha(os.getenv('TWO_CAPTCHA_API_KEY'))
        
        async with async_playwright() as playwright:
            browser = await playwright.chromium.launch(headless=False)
            page = await browser.new_page()
                    
            framework = FrameworkType.PLAYWRIGHT
            
            async with TwoCaptchaSolver(framework=framework, 
                                        page=page, 
                                        async_two_captcha_client=captcha_client) as solver:
                # Navigate to your target page
                await page.goto('https://example.com/with-recaptcha')
            
                # Solve reCAPTCHA v2
                await solver.solve_captcha(
                    captcha_container=page,
                    captcha_type=CaptchaType.RECAPTCHA_V2
                )
            
            # Continue with your automation...
    
    asyncio.run(solve_with_2captcha())
  6. Set API keys via environment variables

    main

    If using API-based solvers, you can provide your credentials via environment variables:

    • TWO_CAPTCHA_API_KEY: For TwoCaptcha services.
    • TEN_CAPTCHA_API_KEY: For TenCaptcha services.
    TWO_CAPTCHA_API_KEY=your_2captcha_api_key_here
    TEN_CAPTCHA_API_KEY=your_10captcha_api_key_here
  7. Extend or use BaseSolver and ApiSolverBase

    main

    The library provides two base classes for defining solver behavior:

    • BaseSolver: The fundamental base class for all solvers in the library.
    • ApiSolverBase: A specialized base class for solvers that communicate with external captcha-solving APIs (like 2Captcha).
    from playwright_captcha import BaseSolver, ApiSolverBase
  8. Use TwoCaptchaSolver for API-based captcha solving

    main

    The TwoCaptchaSolver is an implementation of an API-based solver that interfaces with the 2Captcha service to solve captchas. It inherits from ApiSolverBase and is used when you want to delegate captcha solving to an external service rather than performing browser-based clicks.

    from playwright_captcha import TwoCaptchaSolver