Skip to content

dx-bear/reCAPTCHA-solver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

How to Scrape Google reCAPTCHA-Protected Websites with Python

๐ŸŽฏ Join Scravity Now !

โœ” Free credits
โœ” Priority access
โœ” Early features

In this tutorial, you'll learn how to solve Google reCAPTCHA puzzles using Python completely free โœ…

  • No third-party services.
  • No CAPTCHA-solving services.
  • Just you, your code, and a little help from me. ๐Ÿค๐Ÿ’ป We will test on this demo [ https://www.google.com/recaptcha/api2/demo ], so let's get started!

๐Ÿ“ Step 1: Create a Project Directory.

First, let's create a new directory to hold all our project files. Open your terminal or command prompt and run:

mkdir recaptcha-test

Then navigate into the directory:

cd recaptcha-test

๐Ÿ“Œ Example: _recaptcha-test__โ€Šโ€”โ€Šfeel free to name it whatever you like!_

๐Ÿš€ Step 2: Set Up the Project with uv.

We'll use uv to manage our project. It's a fast Python package manager that handles virtual environments automatically. ๐Ÿโšก you can learn more about here. First, make sure you're inside the recaptcha-test directory we created:

cd recaptcha-test

Now, initialize a new Python project:

uv init

This command creates a pyproject.toml file and sets up a virtual environment for youโ€Š. โ€Šno extra steps needed! ๐ŸŽ‰

๐Ÿ“ Note: If you prefer using pip, you can create a virtual environment manually with:

_python -m venv venv_

But I recommend _uv_โ€”โ€Šit's fast and simple. For more details, check out the uv getting started guide.

We'll need a Python package called SeleniumBase to interact with the reCAPTCHA puzzle. In your terminal, run:

uv add seleniumbase

This command installs the package and automatically updates your pyproject.toml and uv.lock files. โšก

You'll also need Playwright for browser automation:

uv add playwright

๐ŸŒ Install Chromium Browser

We'll be using Chromium instead of Chrome for this tutorial. I'll explain why Chromium is preferred laterโ€Š, but trust me, it makes a difference! ๐Ÿ”

Download and install Chromium from the official source: ๐Ÿ‘‰ Download Chromium

Quick download steps:

  1. Go to the Chromium snapshots directory
  2. Choose your platform: Mac, Win, or Linux ๐Ÿ–ฅ๏ธ
  3. Open the LAST_CHANGE file to find the latest build number
  4. Download the zip file for that build number

๐Ÿ”Œ Activate the Virtual Environment

After installation is complete, activate the virtual environment:

๐Ÿง Linux / macOS:

source .venv/bin/activate

๐ŸชŸ Windows (Command Prompt):

.venv\Scripts\activate

๐ŸชŸ Windows (PowerShell):

.venv\Scripts\Activate.ps1  

You'll know it's working when you see (.venv) appear at the beginning of your terminal prompt.

๐Ÿ“‚ Project Structure (After Installation)

Your folder structure should now look like this:

drwxr-xr-x    - dxbear 22 Mar 14:29  ๐Ÿ“ .  
drwxr-xr-x    - dxbear 22 Mar 14:28  ๐Ÿ“ ..  
drwxr-xr-x    - dxbear 22 Mar 14:28  ๐Ÿ“ .git  
drwxr-xr-x    - dxbear 22 Mar 14:30  ๐Ÿ“ .venv  
.rw-r--r--  109 dxbear 22 Mar 14:28  ๐Ÿ“„ .gitignore  
.rw-r--r--    5 dxbear 22 Mar 14:28  ๐Ÿ .python-version  
.rw-r--r--   92 dxbear 22 Mar 14:28  ๐Ÿ main.py  
.rw-r--r--  256 dxbear 22 Mar 14:30  โš™๏ธ pyproject.toml      โฌ…๏ธ Updated with SeleniumBase  
.rw-r--r--    0 dxbear 22 Mar 14:28  ๐Ÿ“– README.md  
.rw-r--r-- 115k dxbear 22 Mar 14:30  ๐Ÿ”’ uv.lock              โฌ…๏ธ Updated with locked versions

โœ… SeleniumBase and its dependencies are now ready to use!

๐Ÿ’ป Step 3: Writing the Code

Let's open main.py in our project and start coding step by step. ๐Ÿ“

๐Ÿ“ฆ Import Required Packages

First, we need to import the necessary packages from SeleniumBase and Playwright:

from seleniumbase import cdp_driver  
from playwright.async_api import async_playwright

We'll also need asyncio to run our code asynchronously. This step is optionalโ€Šโ€”โ€Šyou can use synchronous code if you prefer, but async gives us better performance! โšก

import asyncio

๐Ÿ›ก๏ธ Why SeleniumBase's CDP Driver?

The cdp_driver from SeleniumBase is a stealth driver that emulates human browser behavior. ๐Ÿ•ต๏ธโ€โ™‚๏ธ Unlike regular Playwrightโ€Š where bots are easily detectedโ€Š, โ€ŠSeleniumBase patches the Playwright browser to bypass bot detection. This is crucial for solving reCAPTCHA, as Google is very good at identifying automated scripts. It's still not enough to bypass reCAPTCHA on its own, but we'll explain how to handle that. What's important first is not triggering bot detection in the first place.

๐Ÿ” Key Insight: Standard Playwright can be flagged as a bot almost immediately. The cdp_driver makes your browser appear as a real human user, giving us a much higher success rate with reCAPTCHA

๐Ÿ”ง Initialize the Drivers

Inside our main function, we'll start by initializing the CDP driver:

async def main():  
    # Start the stealth CDP driver  
    driver = await cdp_driver.start_async()  
    endpoint_url = driver.get_endpoint_url()

๐Ÿ”Œ Connect via CDP (Chrome DevTools Protocol)

Now we'll connect to the browser using CDP. This is a powerful protocol for web scraping and browser automation! ๐ŸŽฏ

async def main():  
    # Start the stealth CDP driver  
    driver = await cdp_driver.start_async()  
    endpoint_url = driver.get_endpoint_url()  
  
    # Connect to the browser using CDP (Chrome DevTools Protocol)  
    async with async_playwright() as p:  
        browser = await p.chromium.connect_over_cdp(endpoint_url)  
        context = browser.contexts[0]  # Create browser context  
        page = context.pages[0]        # Create page object  
          
        # Navigate to Google's official reCAPTCHA demo page  
        await page.goto("https://www.google.com/recaptcha/api2/demo")  
          
        # Wait 10 seconds to observe the page before closing  
        await driver.sleep(10)  
  
if __name__ == "__main__":  
    loop = asyncio.new_event_loop()  
    loop.run_until_complete(main())

๐ŸŒŸ Why CDP Instead of Traditional Selenium/WebDriver?

BenefitDetails๐Ÿฅท Stealth Mode CDP interacts directly with the browser at the protocol level, making it much harder for services like reCAPTCHA to detect automation.๐ŸŽฎ Fine-Grained Control Intercept network requests, manipulate the DOM, capture performance metrics, and simulate network conditionsโ€Š. โ€Šall through a single protocol.๐Ÿ“ฆ No Extra Dependencies Unlike Selenium, CDP doesn't require separate browser drivers that need version matching and constant updates.โšก Async Support CDP works beautifully with async/await patterns, making your scraping scripts faster and more efficient.๐Ÿ‘ค True Browser Emulation Since we're connecting to an actual Chromium instance via CDP, we inherit all browser features without the "tells" that give away automation tools.

๐Ÿงช What This Code Does

  1. Starts a stealth CDP driver ๐Ÿ•ต๏ธโ€โ™‚๏ธโ€Šโ€”โ€ŠThe cdp_driver launches a Chromium browser with stealth patches to avoid bot detection.
  2. Connects via CDP ๐Ÿ”Œโ€Šโ€”โ€ŠWe connect Playwright to the same browser instance using Chrome DevTools Protocol.
  3. Creates browser context and page ๐Ÿ“„โ€Šโ€”โ€ŠWe access the existing browser context and page (or create new ones if needed).
  4. Navigates to the reCAPTCHA demo page ๐ŸŒโ€Šโ€”โ€ŠGoogle provides an official demo page for testing: ๐Ÿ‘‰ https://www.google.com/recaptcha/api2/demo
  5. Waits 10 seconds โฑ๏ธโ€Šโ€”โ€ŠThis gives us time to see the page and reCAPTCHA widget before the browser closes.

๐Ÿ“ Complete Code So Far

import asyncio  
from seleniumbase import cdp_driver  
from playwright.async_api import async_playwright  
  
async def main():  
    # Start the stealth CDP driver  
    driver = await cdp_driver.start_async()  
    endpoint_url = driver.get_endpoint_url()  
  
    # Connect to the browser using CDP (Chrome DevTools Protocol)  
    async with async_playwright() as p:  
        browser = await p.chromium.connect_over_cdp(endpoint_url)  
        context = browser.contexts[0]  # Create browser context  
        page = context.pages[0]        # Create page object  
          
        # Navigate to Google's official reCAPTCHA demo page  
        await page.goto("https://www.google.com/recaptcha/api2/demo")  
          
        # Wait 10 seconds to observe the page before closing  
        await driver.sleep(10)  
  
if __name__ == "__main__":  
    loop = asyncio.new_event_loop()  
    loop.run_until_complete(main())

๐Ÿงฉ Step 4: Solving the CAPTCHA

If you run this code:

uv run main.py

You'll notice two things:

  1. It loads and opens Chrome by default ๐ŸŒโ€Šโ€”โ€Š> If Chrome is installed, it will use it. If not, it will ask you to install it.
  2. We haven't solved any CAPTCHA yet โŒโ€Šโ€”โ€Š> The reCAPTCHA puzzle remains untouched; we just opened the page!

๐ŸŽฏ The Trick: Using a Chrome Extension

Here's the key insight you must understand: ๐Ÿง 

SeleniumBase itself has no capability to bypass or solve Google reCAPTCHA. So we'll use a Chrome extension that does the heavy lifting for us!

๐Ÿงฉ Why Chromium Instead of Chrome?

To load any extension, you MUST use Chromium, not Chrome. Why? Because Chrome no longer allows loading external extensions in automated environments. Chromium gives us the freedom to load custom extensions without restrictions. This is why I had you install Chromium earlier! ๐ŸŽฏ

๐Ÿค– The reCAPTCHA Solver Extension

We'll load a modified version of a Chrome extension called reCAPTCHA solver.

By "modified," I mean I've tweaked it to run automatically as soon as the page loadsโ€Šโ€”โ€Šso you don't have to click anything to activate it. โœจ

๐Ÿ”ฌ How It Works:

The extension uses tiny AI models trained to:

  • Detect image-based CAPTCHA challenges ๐Ÿ”
  • Identify objects like traffic lights, buses, crosswalks, etc. ๐Ÿšฆ๐ŸšŒ๐Ÿšถ
  • Automatically solve the puzzles for you โšก

๐Ÿ“ Create a Folder for Chrome Extensions

First, let's create a folder to hold our Chrome extension:

mkdir chrome-extensions

Or create it manually in your project directory. ๐Ÿ“‚

๐Ÿ”ฝ Download the Modified reCAPTCHA Solver Extension

Head over to my GitHub repository to download the extension: ๐Ÿ‘‰ [ https://github.com/dx-bear/reCAPTCHA-solver ] You have two options:

Option 1โ€Šโ€”โ€ŠDownload the extension only ๐Ÿ“ฆ

Look for the recaptcha-solver folder in the repository and download it directly.

Option 2โ€Šโ€”โ€ŠClone the entire repository ๐ŸŒ€

git clone [your-repo-url]  

Then copy the extension folder into your chrome-extensions directory.

๐Ÿ“‚ Project Structure After Adding the Extension

recaptcha-test/  
โ”œโ”€โ”€ .venv/  
โ”œโ”€โ”€ chrome-extensions/              ๐Ÿ“ New folder  
โ”‚   โ””โ”€โ”€ recaptcha-solver/           ๐Ÿ“ Extension files  
โ”‚       โ”œโ”€โ”€ manifest.json  
โ”‚       โ”œโ”€โ”€ background.js  
โ”‚       โ””โ”€โ”€ ... (other extension files)  
โ”œโ”€โ”€ .gitignore  
โ”œโ”€โ”€ .python-version  
โ”œโ”€โ”€ main.py  
โ”œโ”€โ”€ pyproject.toml  
โ”œโ”€โ”€ README.md  
โ””โ”€โ”€ uv.lock

๐Ÿ”Œ Update the Code to Load the Extension

Now let's modify main.py to load the reCAPTCHA solver extension into Chromium. ๐ŸŽฏ First, import os to handle file paths:

import os  
import asyncio  
from seleniumbase import cdp_driver  
from playwright.async_api import async_playwright

Now, set the absolute path to our extension and configure the driver:

async def main():  
    # Get the absolute path to the extension folder  
    ext = os.path.abspath("./chrome-extensions/recaptcha-solver/0.1.0_0/")  
      
    # Start the stealth CDP driver with Chromium and the extension loaded  
    driver = await cdp_driver.start_async(  
        use_chromium=True,   # Use Chromium instead of Chrome  
        extension_dir=ext,   # Load our custom extension  
    )  
      
    endpoint_url = driver.get_endpoint_url()  
  
    # Connect to the browser using CDP  
    async with async_playwright() as p:  
        browser = await p.chromium.connect_over_cdp(endpoint_url)  
        context = browser.contexts[0]  
        page = context.pages[0]  
          
        # Navigate to Google's reCAPTCHA demo page  
        await page.goto("https://www.google.com/recaptcha/api2/demo")  
          
        # Wait for the extension to do its magic! โœจ  
        await driver.sleep(10)  
  
if __name__ == "__main__":  
    loop = asyncio.new_event_loop()  
    loop.run_until_complete(main())

๐Ÿ“ What's Happening Here?

Parameter Purpose use_chromium=TrueTells SeleniumBase to launch Chromium instead of Chrome ๐Ÿงฉextension_dir=ext Loads our modified reCAPTCHA solver extension into the browser ๐Ÿ”Œos.path.abspath() Converts the relative path to an absolute path so the browser can find it ๐Ÿ“

๐Ÿš€ What to Expect

When you run the script:

uv run main.py

Here's what will happen:

  1. First Runโ€Šโ€”โ€ŠDownloading Files ๐Ÿ“ฅโ€Šโ€”โ€ŠThe extension may take a few seconds to download some AI model files. These will be saved in a folder called downloaded_files in the same directory. This only happens once!
  2. Chromium Launches ๐ŸŒโ€Šโ€”โ€ŠA Chromium browser window will open automatically with our extension pre-loaded.
  3. Page Loads ๐Ÿ“„โ€Šโ€”โ€ŠThe script navigates to Google's reCAPTCHA demo page.
  4. Automatic Solving โœจโ€Šโ€”โ€ŠHere's the magicโ€Šโ€”โ€Šyou'll see the reCAPTCHA widget open and solve itself automatically! No clicking, no manual interaction needed.

About

Simple Example to solve reCAPTCHA

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors