Skip to main content

How To Solve CAPTCHAs with Python

CAPTCHAs are designed to prevent automated bot access by posing problems that are typically easy for humans but challenging for computers. However, there are legitimate reasons to automate CAPTCHA solving, such as accessibility for users who have difficulty with CAPTCHAs or testing purposes.

In this guide, we'll divide into some of these methods and going to walk through:

TLDR: Solving CAPTCHAs Efficiently with Python

For those interested in a straightforward method to solve CAPTCHAs with Python, you can utilize the combination of the selenium library for web automation and pytesseract for OCR capabilities.

Let's see how you can use these for a compact solution:

  • First make sure you have Python installed along with selenium, pytesseract, and Pillow. Usage of Selenium helps in automation browser interactions, while Pillow and pytesseract handle images and text extraction from them. You can install them using pip:
pip install pytesseract selenium pillow
  • You need to download a web driver compatible with your browser, such as Chrome Driver and specify its path in the script.

For example, basic code setup can look like this:

from selenium import webdriver
from PIL import Image
import pytesseract
from io import BytesIO

# Set up the web driver
browser = webdriver.Chrome(executable_path='path_to_chromedriver')

# Open the webpage that contains the CAPTCHA

# Find the CAPTCHA image by its ID and take a screenshot
captcha_element = browser.find_element_by_id('captcha_image_id')
captcha_image = captcha_element.screenshot_as_png

# Load the image into PIL for processing and use pytesseract to extract the text
image =
captcha_text = pytesseract.image_to_string(image, config='--psm 8 --oem 3')

print("CAPTCHA Text:", captcha_text)

The provided Python script is designed to automate the process of solving CAPTCHAs using two primary tools: Selenium and Pytesseract. Here's a detailed explanation of how each part of the code contributes to solving a CAPTCHA:

  • browser = webdriver.Chrome(executable_path='path_to_chromedriver') helps in automating the browser, so Selenium can navigate to pages, find CAPTCHA images, and interact with any necessary elements on the page. The CAPTCHA is captured directly from the screen using Selenium's screenshot_as_png function.

  • After loading the captured image into Pytesseract, pytesseract.image_to_string(image, config='--psm 8 --oem 3') analyzes the image and extracts the text from it. The configurations (--psm 8 --oem 3) optimize Pytesseract to recognize single words, which is typical for CAPTCHAs, enhancing accuracy.

  • By displaying the CAPTCHA text with print("CAPTCHA Text:", captcha_text), this confirms that the CAPTCHA has been processed and solved, which is the end goal of the script.

  • Finally browser.quit() ensures that the browser is closed after the script has run prevents resource leakage, which is important in automated tasks to maintain efficiency.

This Python script provides great start for a direct solution for solving CAPTCHAs by using Selenium to access and capture the CAPTCHA image from a web page, and then employing Pytesseract to extract and output the text.

You will need to adjust this script based on your needs.

Understanding CAPTCHAs and Their Challenges

CAPTCHAs, or Completely Automated Public Turing test to tell Computers and Humans Apart, are tools used by websites to ensure that the entity interacting with them is a human rather than a bot.

The primary challenge they pose to automated systems is their design purpose: to be solvable only by humans.

This characteristic makes CAPTCHAs a significant barrier in tasks that involve web automation and scraping, where bots need to perform operations similar to humans. CAPTCHAs come in various forms, each presenting unique challenges. For example:

  • Text-based CAPTCHAs: These display distorted text that users must enter correctly. They often include characters that are visually similar to test the user's ability to distinguish subtle differences.
  • Image-based CAPTCHAs: Users select images that fit a specific description, such as a command like Select all images with traffic lights, requiring the ability to visually interpret and analyze complex scenes.
  • Audio CAPTCHAs: Intended for users with visual impairments, these play audio clips of distorted numbers and letters that the user must enter correctly.
  • Interactive CAPTCHAs: These require users to perform a task, such as dragging a slider or making a specific gesture with the mouse, testing the user's ability to interact with elements in a human-like manner.

Efficient CAPTCHA solving enhances the effectiveness of automated processes in various domains such as data scraping, and automated testing of web applications.

For businesses, overcoming CAPTCHA can mean the difference between accessing essential web data and facing significant operational delays. However, as mentioned earlier, since CAPTCHAs are designed to prevent automated access, they pose significant obstacles for tasks like web scraping, where bots are used to collect large amounts of data from websites.

They disrupt the scraping process, requiring developers to either manually solve CAPTCHAs or implement advanced solutions to bypass them. This challenge increases the complexity and cost of developing effective web automation and scraping tools.

What are CAPTCHAs?

CAPTCHAs are security mechanisms used on websites to distinguish between human users and automated bots. These tests are typically easy for humans but challenging for machines or bots.

For instance:

  • Text-based CAPTCHAs require users to enter distorted or overlapping letters and numbers that appear with visual noise and line interference, designed to confound standard optical character recognition software.
  • Image-based CAPTCHAs ask users to identify specific images from a set, like finding all pictures containing cars, testing the ability to process complex visual information.
  • Checkbox CAPTCHAs, often recognized as reCAPTCHA, involve a simple checkbox but may analyze the associated mouse movements to verify they are human-like.

For example, this is how different CAPTCHAs look like:

Text-Based Captcha


Checkbox Captcha

Can CAPTCHAs Be Bypassed?

CAPTCHAs can indeed be bypassed, although it poses a significant challenge. Designed primarily to prevent automated systems from performing sensitive or potentially harmful actions, CAPTCHAs incorporate complexities that are typically difficult for computers to decode. The trick isn't just in solving them but in doing so consistently and efficiently enough to be practical in a real-world application.

To effectively handle CAPTCHAs, preemptive measures can often be more beneficial than direct confrontation. For example, modifying the behavior of the automated system to mimic human interaction patterns can reduce the likelihood of triggering a CAPTCHA.

These might include varying the timing of requests, using cookies consistently as a normal user would, or interacting with other elements of the page before making a request.

If a CAPTCHA does appear, one tactic is to simply retry the action —a page reload or a new form submission might bypass the CAPTCHA if the website's security triggers aren't consistently activated.

Approaches To Dealing With CAPTCHAs

Dealing with CAPTCHAs effectively requires a strategic approach, often involving either prevention or direct resolution:

1. Mimicking Human Behavior:

  • The first strategy involves configuring automated systems to emulate human browsing behaviors as closely as possible. This includes maintaining realistic intervals between page requests, using consistent IP addresses that don't switch too frequently, and managing cookies and session data effectively.
  • Human-like interactions with the website, such as randomly moving the mouse or scrolling through pages before making a request, can also help in avoiding detection as a bot.
  • Websites track behavioral patterns, and systems that appear to behave like typical users are less likely to be presented with CAPTCHAs.

2. Solving the CAPTCHA Directly:

  • When CAPTCHAs do appear, the next approach is to solve them using either an in-house developed system or a third-party service.
  • Developing an in-house solution generally involves using OCR technologies to interpret text-based CAPTCHAs or machine learning models for more complex image or interactive based CAPTCHAs.
  • This can be technically demanding and resource-intensive but offers control over the process and can be tailored specifically to the types of CAPTCHAs encountered.
  • Alternatively, third-party CAPTCHA solving services offer a practical solution, especially for businesses that need to handle large volumes of CAPTCHAs.
  • These services use human labor or advanced AI systems to solve CAPTCHAs sent to them in real-time, providing the solved CAPTCHA back in a few seconds. This method incurs a cost per CAPTCHA but is highly effective, especially where high accuracy is required.

Both approaches require careful consideration of the legal and ethical implications, ensuring that any method used to bypass or solve CAPTCHAs complies with the relevant laws and website terms of service.

Developing a nuanced understanding of both strategies allows for more effective planning and implementation of web automation and scraping projects.

How to Solve CAPTCHAs?

Solving CAPTCHAs is a common requirement in various automated tasks, especially when dealing with web data extraction, user behavior simulation, and automated testing.

There are several methods available, ranging from utilizing free and open-source software to relying on robust, paid third-party services. Each approach has its strengths and suits different needs and contexts.

Free & Open Source CAPTCHA Solving Libraries

Tesseract OCR

Open-source libraries offer a cost-effective way to tackle simple CAPTCHAs, especially those that are text-based. For example, Tesseract OCR is an open-source OCR engine available under the Apache 2.0 license, widely used for image-to-text conversions in various languages.

It supports a wide range of languages and can be trained to recognize other languages or specialized fonts and is highly configurable, allowing fine control over OCR processing steps.

You can install Tesseract with:

pip install pytesseract

To use Tesseract in Python, you typically need an image processing library like PIL to open and preprocess the images.

Let's take a look at an example:

from PIL import Image
import pytesseract

# Open an image file
img ='path_to_image.jpg')

# Use Tesseract to convert the image to text
text = pytesseract.image_to_string(img)



Next, EasyOCR is a more recent addition, designed to be straightforward to use for OCR tasks. It is particularly effective in recognizing text from noisy backgrounds and unconventional layouts and supports over 80 languages and includes a deep learning model that can handle more complex images and text layouts.

For the installation, you can use:

pip install easyocr

EasyOCR can be used with just a few lines of code to recognize text from images. For example:

import easyocr

# Create a reader instance for English
reader = easyocr.Reader(['en'])

# Read from an image
results = reader.readtext('path_to_image.jpg')

for result in results:

Python Imaging Library (PIL)

Python Imaging Library, or known as Pillow library is essential for opening, manipulating, and saving many different image file formats in Python. PIL is highly useful for image preprocessing in OCR tasks, such as resizing, cropping, and converting to grayscale.

You can use following command for the installation:

pip install Pillow

PIL can be used to preprocess images before they are passed to an OCR engine:

from PIL import Image, ImageFilter

# Open an image file
img ='path_to_image.jpg')

# Apply image filters
img = img.convert('L') # Convert to grayscale
img = img.filter(ImageFilter.SHARPEN) # Sharpen image

# Save or display the processed image'processed_image.jpg')


Python libraries such as OpenCV can be used for such image manipulations before feeding the image to an OCR tool. This method is particularly appealing for developers with technical proficiency who prefer to keep their solutions in-house without additional costs. OpenCV can be used for more complex image transformations and enhancements than PIL.

For the installation, use the following command:

pip install opencv-python

Here is how you can use OpenCV for image manipulations, especially in preparing images for OCR:

import cv2

# Load an image
img = cv2.imread('path_to_image.jpg')

# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Apply Gaussian Blur
blurred = cv2.GaussianBlur(gray, (5, 5), 0)

# Save the processed image
cv2.imwrite('processed_image.jpg', blurred)

Each of these libraries plays a vital role in the pipeline of recognizing text from images, particularly in the context of solving CAPTCHAs, where preprocessing and accurate text recognition are crucial.

Addittionally, here are some open-source CAPTCHA-solving libraries that you can use in your applications:

  • arunpatala/captcha: This project leverages the Torch machine learning library to tackle the challenge of Simple Captcha, a Java-based CAPTCHA software. The author has constructed a dataset comprising 10,000 samples, each containing five characters that incorporate all possible effects and noises from the library, making it a robust tool for training models to break CAPTCHAs under varied and challenging conditions.

  • zakizhou/CAPTCHA: Developed using TensorFlow, this repository features a small convolutional neural network designed to recognize CAPTCHAs that consist solely of four digits set against noisy backgrounds. This focused approach helps streamline the training process, making it a useful resource for those looking to start with neural networks for CAPTCHA recognition.

  • nladuo/captcha-break: This CAPTCHA-breaking solution combines OpenCV for image processing, Tesseract-OCR for text extraction, and a machine learning algorithm to effectively decipher CAPTCHAs. The integration of these technologies allows it to handle a variety of CAPTCHA types effectively.

  • ypwhs/captcha_break: Utilizing Keras, this project employs a deep convolutional neural network to identify CAPTCHA verification codes. The deep learning model is particularly adept at handling more complex image patterns and learning from vast amounts of labeled data, enhancing its effectiveness over time.

  • ptigas/simple-captcha-solver: This repository offers a straightforward method for solving simple CAPTCHAs. The process involves moving each letter across the image, calculating and summing the pixel differences for each position. Positions scoring highest for each letter indicate the most likely matches, which are then sorted to reconstruct the CAPTCHA text effectively.

  • rickyhan/SimGAN-Captcha: SimGAN-Captcha introduces an innovative approach using Generative Adversarial Networks (GANs) to solve CAPTCHAs without requiring manually labeled training sets. By synthesizing training data pairs, this method provides a scalable solution for training classifiers to recognize and solve CAPTCHAs.`

  • arunpatala/captcha.irctc: This library is finely tuned to solve CAPTCHAs on the IRCTC website, a critical portal for booking train tickets in India. It employs deep learning techniques to achieve an impressive 98% accuracy rate, demonstrating its effectiveness in high-demand real-world applications.

  • JackonYang/captcha-tensorflow: This TensorFlow-based project utilizes a CNN model to solve image CAPTCHAs with an accuracy of 90%. It showcases the power of neural networks in learning and adapting to the intricacies of varied CAPTCHA types.

  • skyduy/CNN_keras: With a focus on recognizing single letters from a dataset of about 5000 samples, this Keras-based convolutional neural network achieves a high accuracy rate of 95%. It's particularly suited for applications where high precision in letter recognition is crucial.

  • PatrickLib/captcha_recognize: This image recognition project does not require segmentation and achieves remarkably high accuracy rates 99.7% with 50,000 training samples and 52.1% with 100,000 samples. It demonstrates the scalability of machine learning approaches to CAPTCHA solving.

  • zhengwh/captcha-svm: This project employs a support vector machine (SVM) to identify and solve simple verification strings presented in CAPTCHAs, showcasing how traditional machine learning models can still be effective in specific CAPTCHA scenarios.

  • chxj1992/captcha_cracker: Using Keras and a convolutional neural network, this implementation focuses on the recognition function of verification codes, offering a simple yet effective approach to solving CAPTCHAs.

  • chxj1992/slide_captcha_cracker: TThis project uses OpenCV to implement a simple edge detection algorithm that locates sliding verification code puzzles within a background image, demonstrating a practical application of image processing techniques in CAPTCHA solving.

  • JasonLiTW/simple-railway-captcha-solver#english-version: Designed for the Taiwan railway booking website, this solver employs a simple convolutional neural network to achieve high accuracy in solving the platform's CAPTCHA, exemplifying targeted solutions for specific websites. Currently, the accuracy of a single digit on the validation set is about 98.84%, and the overall accuracy is 91.13%.

  • lllcho/CAPTCHA-breaking: This project combines Python, Keras, and OpenCV to break simple CAPTCHAs, illustrating the integration of multiple technologies for effective CAPTCHA solving.

  • ecthros/uncaptcha: Targeting Google's audio reCAPTCHA, this project achieves 85% accuracy by using advanced audio processing techniques to identify spoken numbers, challenging the robustness of one of the most common CAPTCHA systems.

  • dessant/buster: TBuster is a free tool that assists in solving difficult CAPTCHAs by completing reCAPTCHA audio challenges using speech recognition. Available as a browser extension for Chrome, Edge, and Firefox, it provides a user-friendly solution to bypass CAPTCHA challenges effectively.

  • kerlomz/captcha_trainer: This project offers a deep learning-based solution capable of handling complex verification scenarios such as character adhesion, overlap, and noise. It demonstrates the applicability of advanced neural networks in solving the most challenging CAPTCHA types currently in use.

These libraries and projects represent a broad spectrum of approaches to CAPTCHA solving, from simple algorithms to complex neural network applications, providing valuable resources for anyone facing CAPTCHA-related challenges in their digital environments. For the detailed info, make sure to visit their Github repositories.

3rd Party CAPTCHA Solving Services

Addittionally, you can use 3rd party services in your applications. These services employ either human workers or advanced algorithms to decode CAPTCHAs.

When integrated into your automation scripts, such as those written for Puppeteer, they allow you to seamlessly pass the CAPTCHA image for solving, retrieve the answer, and input it back into the target webpage automatically.

This is particularly useful for maintaining workflow efficiency in tasks that require interaction with CAPTCHA-protected sites.

Here are some of the popular services available:

  • 2Captcha:
    • This service is well-equipped to handle a variety of CAPTCHA types including reCAPTCHA v2, v2 callback, v2 invisible, v3, and Enterprise versions, as well as hCaptchas.
    • Pricing starts at $1.00 for every 1,000 CAPTCHAs solved.
    • 2Captcha is known for its fast response time, with solutions typically returned in less than 12 seconds, making it a reliable option for real-time applications.
  • Anti-Captcha:
    • Anti-Captcha distinguishes itself by employing a global network of human workers to solve CAPTCHAs, ensuring a high degree of accuracy.
    • The cost is competitively set starting at $0.50 per 1,000 images solved.
    • This human-powered approach guarantees a 100% solution rate, ideal for applications where automated methods fall short, particularly with complex image-based CAPTCHAs or sophisticated puzzles.
  • DeathByCaptcha:
    • Offering a comprehensive CAPTCHA-solving service, this service can tackle any type of CAPTCHA presented.
    • Their pricing varies depending on the complexity and type of CAPTCHA, with standard solutions costing between $0.99 to $2 per 1,000 CAPTCHAs.
    • They also offer specialized services for more challenging CAPTCHAs such as Cloudflare Turnstile at $2.89 per 1,000 CAPTCHAs and reCAPTCHA at the same price.
    • Solving hCAPTCHAs costs $3.99 per 1,000, reflecting the additional complexity and effort required.

These services provide essential tools for businesses and developers facing the challenge of CAPTCHAs in their automated processes, allowing for seamless interaction with CAPTCHA-protected websites without manual intervention.

Integrating these services into your scripts can significantly streamline operations and reduce downtime caused by CAPTCHA interruptions.

Solving Text CAPTCHAs With Python

Alphanumeric CAPTCHAs are a type of CAPTCHA that requires users to identify and enter a combination of letters and numbers from a distorted or obscured image displayed onscreen.

These CAPTCHAs are designed to prevent automated bots from accessing web services, completing registrations, or submitting forms online by presenting a challenge that is typically easier for humans to solve than machines.

Here is the some of the examples of Alphanumeric CAPTCHAs:

Alphanumeric CAPTCHAs

This is how Alphanumeric CAPTCHAs work:

  • Image Generation: The CAPTCHA system generates an image that includes a random string of letters and numbers. These characters are often distorted, twisted, or overlaid with visual noise such as dots, lines, or varying background colors to complicate OCR by automated systems.
  • User Interaction: The user is prompted to enter the characters as they appear in the image. This task involves visual perception and interpretation, skills that humans usually perform well compared to current automated systems.
  • Validation: Once the user submits their input, the system checks if the entered text matches the CAPTCHA text. If it does, the user is allowed to proceed; if not, they may be asked to try again, potentially with a new CAPTCHA.

This method leverages the fact that, although OCR technology has advanced, interpreting highly distorted or overlaid text amidst various visual interferences still poses significant challenges for automation.

The effectiveness of alphanumeric CAPTCHAs lies in their ability to balance complexity to defeat bots and simplicity to allow human users to pass through.

Solving Text CAPTCHAs With Open Source Solution

In this section, we'll explore a practical implementation of solving text CAPTCHAs using Python with the help of open-source libraries such as PIL, SciPy, NumPy, and Pytesseract. Each of these libraries plays a crucial role in processing and interpreting the CAPTCHA images.

Let's test the following image to see the results:

Example Text CAPTCHA Code

Below is a complete Python script that demonstrates how to solve a text CAPTCHA using the mentioned libraries:

from PIL import Image, ImageFilter
from scipy.ndimage import gaussian_filter
import numpy as np
import pytesseract

def solve_captcha(filename):
# Thresholds and blurring sigma
th1 = 140
th2 = 140
sig = 1.5

# Load and save the original image
original =
# Convert to black and white
black_and_white = original.convert("L")"black_and_white.png")

# Apply the first threshold
first_threshold = black_and_white.point(lambda p: p > th1 and 255)"first_threshold.png")

# Apply Gaussian blur
blur = np.array(first_threshold) # Create an image array
blurred = gaussian_filter(blur, sigma=sig)
blurred = Image.fromarray(blurred)"blurred.png")

# Apply the final threshold
final = blurred.point(lambda p: p > th2 and 255)
final = final.filter(ImageFilter.EDGE_ENHANCE_MORE)
final = final.filter(ImageFilter.SHARPEN)"final.png")

# Perform OCR using Tesseract
result = pytesseract.image_to_string(final, lang='eng', config='--psm 7')

# Print the result
print(f"Captured CAPTCHA: {result}")
return result

# Example usage
result = solve_captcha('path_to_image.jpg')

As a result, we can see:

Captured CAPTCHA: uLNr6M

Let's examine this script to understand what it does:

  • The image is loaded and saved at various stages for debugging and visualization of the process.
  • Image Conversion to Grayscale simplifies the image, reducing it to a basic form easier for processing.
  • Thresholding is a pplied twice to highlight the text against the background, making it more distinguishable.
  • Gaussian Blur helps in smoothing the image, reducing the noise and improving the effectiveness of the final thresholding.
  • Tesseract is used to recognize text from the processed image. Specific configurations like --psm 7 instruct Tesseract to treat the image as a single line of text, which is typical in CAPTCHAs.

This script effectively demonstrates how to programmatically solve a text CAPTCHA by preprocessing the image to enhance text readability and then using OCR to extract the text.

It's important to adapt the threshold values and sigma based on the specific CAPTCHA images you are working with to optimize accuracy.

Solving Text CAPTCHAs With Paid Service

To automate the process of solving text CAPTCHAs, one effective method is using a paid service such as 2Captcha. 2Captcha provides an API that you can pay to have CAPTCHAs solved by human workers.

This service is particularly useful for developers and companies running tests that require CAPTCHA solutions on websites protected by CAPTCHA systems.

First, you need to sign up at 2Captcha to get an API key. This key will allow you to interact with their service programmatically. Next, make sure you have the requests library installed in your Python environment, as it is essential for sending HTTP requests:

pip install requests

Now, let's use the 2Captcha API to solve a text CAPTCHA:

import requests
import time

def solve_captcha(api_key, captcha_file):
# URL for posting the CAPTCHA image
post_url = ''

# Prepare files and data for POST request
with open(captcha_file, 'rb') as file:
files = {'file': file}
data = {'key': api_key, 'method': 'post'}

# Post the CAPTCHA image to 2Captcha
response =, files=files, data=data)
if response.ok:
print("CAPTCHA sent successfully!")
captcha_id = response.text.split('|')[1]
print("Error in sending CAPTCHA:", response.text)
return None

# URL for retrieving the solved CAPTCHA
get_url = f'{api_key}&action=get&id={captcha_id}'

# Check for the solution every 5 seconds
for i in range(60):
result = requests.get(get_url)
if result.text.split('|')[0] == 'OK':
print("CAPTCHA solved!")
return result.text.split('|')[1]
elif result.text == 'CAPCHA_NOT_READY':
print("Solution not ready, waiting...")
print("Error in CAPTCHA solving:", result.text)
return None

# Example usage, make sure to enter your API key
captcha_solution = solve_captcha(api_key, 'path_to_captcha.jpg')
print(f"Solved CAPTCHA text: {captcha_solution}")

  • solve_captcha function takes your API key and the path to the CAPTCHA image file. It sends the CAPTCHA to 2Captcha using their API and checks periodically until the solution is available.
  • The script handles various responses to ensure that you know the status of your CAPTCHA solving request.
  • Polling logic is added to wait for a solution with a timeout, reducing the number of requests if the CAPTCHA isn’t ready yet.

By integrating this script into your projects, you can automate the process of solving CAPTCHAs efficiently. This approach is particularly useful in scenarios where you need to handle a large volume of CAPTCHAs or when manual solving is too cumbersome.

Solving reCAPTCHAs With Python

As the images shown below, reCAPTCHA is a free service from Google that helps protect websites from spam and abuse. A significant evolution of the traditional CAPTCHA, which requires users to enter text from distorted images, reCAPTCHA leverages advanced risk analysis techniques to tell humans and bots apart.

Checkbox Captcha

Checkbox Captcha Passed

reCAPTCHAs are more sophisticated than the original CAPTCHAs, involving tests that are easier for humans but challenging for bots. There are several types of reCAPTCHAs:

  • reCAPTCHA v2: Includes a checkbox saying I am not a robot and may also involve image challenges where users select images matching a specific description.
  • Invisible reCAPTCHA: Performs background checks when users interact with the website and only prompts them to solve a CAPTCHA if it detects suspicious activity.
  • reCAPTCHA v3: Scores user interactions with a website without any user interaction, allowing website owners to take actions (e.g., block, challenge, throttle) based on this score.

The system works by analyzing user interactions and web traffic patterns with sophisticated algorithms to identify potential bots. The challenges presented are dynamically adjusted based on this analysis.

Solving reCAPTCHAs With Open Source Solution

Let's now cover how to interact with Google's reCAPTCHA using selenium-recaptcha-solver, an open-source solution for automating the interaction with Google's reCAPTCHA v2. This tool is specifically useful when you need to test or automate workflows in development environments where dealing with CAPTCHAs is a necessary hurdle.

Let's dissect the code snippet below to understand how selenium-recaptcha-solver works along with Selenium to interact with a reCAPTCHA on a demo page.

Website initially looks like this:

Recaptcha Example

Now, let's move onto code:

from selenium_recaptcha_solver import RecaptchaSolver
from import By
from selenium import webdriver
from import Options

# User-Agent to mimic a real user visit
test_ua = 'Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36'

options = Options()


# Setting a custom User-Agent


# Initialize the Chrome WebDriver with the specified options
test_driver = webdriver.Chrome(options=options)

# Create an instance of RecaptchaSolver using the initialized WebDriver
solver = RecaptchaSolver(driver=test_driver)

# Navigate to Google's reCAPTCHA demo page

# Locate the reCAPTCHA iframe using its title attribute and switch to it
recaptcha_iframe = test_driver.find_element(By.XPATH, '//iframe[@title="reCAPTCHA"]')

# Use the solver to click on the reCAPTCHA checkbox

Then, we can see selenium-recaptcha-solver solves the reCAPTCHA:

Recaptcha example solved

  • The script first configures ChromeDriver and initialize the Selenium WebDriver for Chrome with the specified options.
  • An instance of RecaptchaSolver is created, passing the WebDriver as an argument. This object will handle interactions with the reCAPTCHA.
  • The script then navigates to a demo page that contains a reCAPTCHA and locates its iframe and passes it to the solver's method to perform the click operation.

Using the selenium-recaptcha-solver library offers a streamlined approach to handle reCAPTCHAs during automated testing. It is important to note that this method should only be used in compliance with the terms of service of the website and within legal and ethical boundaries. For detailed info, you can check the repository of this library here.

Solving reCAPTCHAs With Paid Service

For situations where an open-source solution isn't feasible due to the sophistication of the CAPTCHA system, using a paid service like 2Captcha can be an effective alternative. 2Captcha provides a way to bypass reCAPTCHAs by employing real human solvers, which means it can handle any CAPTCHA that a regular human can solve, including Google's reCAPTCHA.

First, ensure you have the requests library installed:

pip install requests

Now, here is a Python example that demonstrates how to solve a reCAPTCHA using 2Captcha. Before you start, make sure you have registered with 2Captcha and have your API key.

import requests
import time

# Replace this with your 2Captcha API key
API_KEY = 'your_2captcha_api_key_here'

# Example: Google's Site Key for the reCAPTCHA to be solved
SITE_KEY = '6Lc_aCMTAAAAABx7u2W0WPXnVbI_v6ZdbM6rYf16'

# The URL of the page where the CAPTCHA is located

def solve_recaptcha(api_key, site_key, page_url):
# 2Captcha URL to submit the solve request
solve_url = ''

# Parameters for the solve request
params = {
'key': api_key,
'method': 'userrecaptcha',
'googlekey': site_key,
'pageurl': page_url,
'json': 1

# Sending the CAPTCHA solve request
response =, params=params)
request_result = response.json()

if request_result['status'] == 1:
request_id = request_result['request']
print('CAPTCHA sent; solving in progress...')
retrieve_url = f'{api_key}&action=get&id={request_id}&json=1'

# Wait a few seconds between checks

# Polling for the solution
while True:
result = requests.get(retrieve_url)
result_data = result.json()

if result_data['status'] == 1:
# CAPTCHA Solved
return result_data['request']
elif result_data['request'] == 'CAPCHA_NOT_READY':
print('Solution not ready, waiting...')
print('Error solving CAPTCHA:', result_data['request'])
print('Error submitting CAPTCHA:', request_result['request'])

# Example usage
captcha_solution = solve_recaptcha(API_KEY, SITE_KEY, PAGE_URL)
print('Solved reCAPTCHA token:', captcha_solution)

  • In this code, the script communicates with the 2Captcha API, sending the reCAPTCHA challenge details and polling for the solution.
  • Polling logic is added to wait for a solution with a timeout, reducing the number of requests if the CAPTCHA isn’t ready yet.
  • The script handles various potential errors, such as an error in submitting the reCAPTCHA or in retrieving the solution.

Using 2Captcha for solving reCAPTCHAs is a robust solution especially when dealing with CAPTCHAs that require more sophisticated interaction than can be provided by OCR or other automated tools.

This method, however, involves costs per CAPTCHA solved and should be used in compliance with the terms of service of the target website.

Solving hCAPTCHAs With Python

hCAPTCHA is a popular CAPTCHA service that serves as an alternative to Google’s reCAPTCHA. It is a type of CAPTCHA that challenges users to prove they are human by recognizing patterns or objects in images.

The service is designed to be easy for humans but difficult for bots, leveraging advanced machine learning models to generate and grade tests.

The functioning of hCAPTCHAs is similar to that of reCAPTCHAs but with different implementation specifics:

  • Image-Based Challenges: Users are presented with a grid of images and asked to select all images that fit a certain description, such as Select all images with trucks.
  • Privacy Focus: hCAPTCHA emphasizes user privacy and claims to collect minimal personal data, positioning itself as a more privacy-conscious option.

hCAPTCHAs are widely used due to their effectiveness in distinguishing humans from bots and their ease of integration into websites. They serve both security purposes and, through the hCAPTCHA network, help companies improve machine learning models by using CAPTCHA solutions to train data sets.

Here is some examples of a hCAPTCHA:

hCaptcha Example 1

hCaptcha Example 2

Solving hCAPTCHAs With Open Source Solution

NopeCHA offers automated CAPTCHA solving capabilities for various types of CAPTCHAs, including hCAPTCHA. It was designed to integrate with browser automation tools like Selenium and Puppeteer, allowing users to manage CAPTCHA challenges efficiently during web automation tasks.

The NopeCHA Extension provides a method to automatically handle hCAPTCHAs by integrating directly into browser-based automation workflows. It leverages advanced algorithms to interpret and solve CAPTCHAs, significantly streamlining the process in automated environments.

Let's now take a look at setting up the NopeCHA Extension with Selenium, demonstrating its use on a sample hCAPTCHA challenge page:

import portpicker
import requests
from selenium import webdriver
from import Service

options =
# Basic options to enhance privacy and avoid CAPTCHA detection
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)

# Downloading and loading the NopeCHA extension
with open('ext.crx', 'wb') as f:

driver = webdriver.Chrome(options=options, service=Service(
'/usr/bin/chromedriver', port=portpicker.pick_unused_port()))

# Setup the NopeCHA extension with your API key: make sure to replace this with your own
NOPECHA_KEY = 'your_nopecha_api_key'

# Navigate to a page with an hCAPTCHA challenge
render(driver, duration=30)

Make sure to create have an API key by navigating their website first. This script configures Selenium to use Chrome in a way that avoids detection as an automated test environment. It also downloads and installs the NopeCHA Extension to handle CAPTCHAs automatically. For the detailed information, you can visit the repository in here.

Solving hCAPTCHAs With Paid Service

For scenarios where an open-source solution isn't practical or robust enough to solve hCAPTCHAs, paid services like 2Captcha provide a reliable alternative. This can be especially useful for businesses or developers needing to handle large volumes of CAPTCHA challenges where automation is crucial.

For this purposes, let's use 2Captcha to solve an hCAPTCHA. This script uses the 2Captcha API to submit hCAPTCHA challenges and retrieve solutions:

import requests
import time

def solve_hcaptcha(api_key, site_key, page_url):
# The API URL to submit the hCAPTCHA for solving
submit_url = ''
params = {
'key': api_key,
'method': 'hcaptcha',
'sitekey': site_key,
'pageurl': page_url,
'json': 1

# Sending the hCAPTCHA solve request
response =, data=params).json()
if response['status'] == 1:
captcha_id = response['request']
print("hCAPTCHA submitted successfully. ID:", captcha_id)

# Polling to get the solution of hCAPTCHA
retrieve_url = f'{api_key}&action=get&id={captcha_id}&json=1'
while True:
result = requests.get(retrieve_url).json()
if result['status'] == 1:
print("hCAPTCHA solved:", result['request'])
return result['request']
elif result['request'] == 'CAPCHA_NOT_READY':
print("Solution not ready, retrying in 5 seconds...")
print("Failed to solve hCAPTCHA:", result['request'])
print("Error submitting hCAPTCHA:", response['request'])

# Example usage: make sure to adjust this to suit your needs
api_key = 'your_2captcha_api_key'
site_key = 'your_hcaptcha_site_key' # Site key for hCAPTCHA on the target page
page_url = '' # URL of the page with hCAPTCHA
captcha_solution = solve_hcaptcha(api_key, site_key, page_url)
print("Solved hCAPTCHA response:", captcha_solution)

  • You need to replace your_2captcha_api_key and your_hcaptcha_site_key with your actual 2Captcha API key and the hCAPTCHA site key from the page where you are solving the CAPTCHA.
  • The script handles JSON responses for easier parsing and checks the status to determine if the CAPTCHA was solved successfully or if additional waiting is needed.

Using 2Captcha allows for bypassing hCAPTCHAs efficiently when open-source solutions do not meet the needs, especially in commercial or heavy-usage scenarios. However, do not forget to use such services ethically and in compliance with the terms of service of the target website.

Solving Audio CAPTCHAs With Python

Audio CAPTCHAs are an accessibility alternative to visual CAPTCHAs, designed to ensure that the user interacting with a website is human and not an automated bot.

This form of CAPTCHA is particularly beneficial for users who have visual impairments. They work by playing a sound clip that contains a sequence of letters, numbers, or both, which the user must correctly enter to pass the verification.

The audio is often distorted or mixed with background noise to prevent automated systems from easily deciphering the sounds. The purpose is to challenge tasks that are easy for humans but difficult for machines, such as understanding distorted or overlapped speech.

The general steps involved in an audio CAPTCHA process are:

  • User Request: The user requests an audio CAPTCHA, often by clicking on an audio icon.
  • Audio Playback: The CAPTCHA system plays a sound file containing the spoken characters mixed with noises.
  • User Input: The user listens to the audio and inputs the characters they hear into a provided text box.
  • Verification: The system verifies the entered text against the expected answer.

Here is an example of how audio CAPTCHA might look like:

Audio Captcha Example

Solving Audio CAPTCHAs With Open Source Solution

Audio CAPTCHAs present a unique challenge compared to visual CAPTCHAs, as they require audio processing and speech recognition capabilities.

Open source solutions, such as the combination of speech-to-text libraries and audio processing tools, can be particularly effective. One effective tool for this task is the SpeechRecognition library in Python.

It supports several speech recognition engines and APIs, including Google Speech Recognition, which we will use for our example. This library is well-documented and widely used in the community, making it a reliable choice for handling audio CAPTCHAs.

First, you need to install the SpeechRecognition library. You can do this using pip:

pip install SpeechRecognition

For this example, ensure you have an audio file that represents a CAPTCHA. This file should contain spoken letters or numbers that the script will attempt to decode. Now, let's write the Python script to decode the audio from the CAPTCHA.

import speech_recognition as sr

# Initialize the recognizer
r = sr.Recognizer()

# Load the audio file
audio_file = "path_to_your_audio_captcha.wav"

# Using the audio file as the audio source
with sr.AudioFile(audio_file) as source:
# Listen for the data
audio_data = r.record(source)
# Convert from speech to text)
text = r.recognize_google(audio_data)
print("CAPTCHA text is:", text)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
  • The script starts by importing the speech_recognition module. Then, initializes a recognizer object, which is used to process the audio.
  • The script then loads an audio file containing the CAPTCHA, the audio file is opened and read, and r.record() captures the data from the entire file.
  • Finally, recognize_google() is used to perform speech recognition on the audio data. The recognized text is printed out. This function requires an internet connection as it sends the audio data to Google's servers.

This script is a basic example of solving an audio CAPTCHA using open-source tools in Python. Depending on the complexity and quality of the audio CAPTCHA, you may need to adjust or preprocess the audio file to improve accuracy. Additionally, handling errors and exceptions is crucial for robust applications, especially in production environments where CAPTCHAs vary widely.

Solving Audio CAPTCHAs With Paid Service

For those who prefer a more reliable and potentially more accurate solution for solving audio CAPTCHAs, paid services like 2Captcha again offer a convenient alternative. 2Captcha is a service that provides automated CAPTCHA solving using human labor and advanced algorithms. They handle a wide range of CAPTCHA types, including audio CAPTCHAs.

This service is especially useful in scenarios where audio CAPTCHAs are difficult to decode using automated methods due to noise, distortion, or complex audio patterns.

Below is a Python script that demonstrates how to integrate with the 2Captcha service to solve an audio CAPTCHA.

import requests

# Your 2Captcha API key
API_KEY = 'your_2captcha_api_key_here'

# URL to the 2Captcha API where we send the audio file
submit_url = ''

# URL to check the status and get the result
result_url = ''

# Path to your audio CAPTCHA file
audio_captcha_file_path = 'path_to_your_audio_captcha.mp3'

# First, submit the CAPTCHA file
with open(audio_captcha_file_path, 'rb') as audio_file:
files = {'file': audio_file}
data = {
'key': API_KEY,
'method': 'post',
'json': 1
response =, files=files, data=data)
captcha_id = response.json().get('request')

if captcha_id:
# Check the solution status
params = {
'key': API_KEY,
'action': 'get',
'id': captcha_id,
'json': 1
# Polling the result
while True:
res = requests.get(result_url, params=params)
result_data = res.json()
if result_data['status'] == 1:
# CAPTCHA solved
captcha_text = result_data['request']
print("Solved CAPTCHA text:", captcha_text)
print("CAPTCHA is still being solved, waiting...")
time.sleep(5) # Wait 5 seconds before checking again
print("Failed to submit CAPTCHA")

  • The script submits an audio CAPTCHA file using a POST request with multipart/form-data encoding.
  • After submitting, it repeatedly checks for the result using the CAPTCHA ID returned by the service. The script polls the service every 5 seconds to see if the CAPTCHA has been solved. Once solved, it prints out the decoded text of the CAPTCHA.

This approach offloads the burden of CAPTCHA solving to a dedicated service, ensuring higher accuracy and reliability, especially useful in applications requiring high volumes of CAPTCHA solving. As always, make sure to check the 2Captcha for details!

How To Avoid Triggering CAPTCHAs

Another approach to dealing with CAPTCHAs, particularly useful in web scraping or automated data collection, is to prevent them from being triggered in the first place. Many websites do not display CAPTCHAs by default; instead, they appear when the site's algorithms detect behavior that seems non-human. Understanding and mimicking human browsing patterns can help in avoiding these triggers.

Let's take a look at some of the ways to avoid triggering CAPTCHAs:

  • Respect Robots.txt: Always check and comply with the website’s robots.txt file, which tells bots which parts of the site they are allowed to interact with. Following these guidelines can reduce the likelihood of being flagged as suspicious.

  • Limit Request Rate: Space out your requests to imitate human browsing speed. Making requests too quickly is a common trigger for CAPTCHAs because it suggests automated access. Implement delays or random intervals between requests.

  • Use Realistic User Agents: Websites can detect if the user agent in your headers is a common web browser or a scripting tool like Python's requests library. Always set a user agent that mimics a popular browser to help blend in with regular traffic.

  • Rotate IPs and Use Proxies: If a single IP address makes too many requests, it can be flagged. Use IP rotation or proxies to distribute the requests across multiple IPs. This is particularly important when scraping data from websites with strict limitations.

  • Maintain Good Session Practices: Some sites may track how users interact with the site and use session-based triggers for CAPTCHAs. Maintain a consistent session by using cookies appropriately, and mimic the typical behavior seen by logged-in users.

  • Mimic Human Cursor Movements: Some advanced systems analyze cursor movements to differentiate between humans and bots. If feasible, simulate human-like interactions with the page, like random mouse movements and clicks.

  • Referer Header: Many websites check the Referer header of HTTP requests to see if traffic is coming from expected locations. Make sure to set these headers appropriately when making requests.

  • Avoid Faulty Requests: Ensure that all requests to the server are successful (i.e., they return a 200 status code). Too many errors or not found responses can alert a site to unusual activity.

  • Use Browser Automation Sparingly: Tools like Selenium or Puppeteer can automate browsers in ways that are very human-like, but they can also be detected by sophisticated CAPTCHA systems. Use them judiciously and in combination with the above practices to stay under the radar.

  • Engage With the Content: Some sites may look for interaction with content, like scrolling through pages or clicking on links, before deciding to trigger a CAPTCHA. Adjust your bots to interact with the page as a human would occasionally.

By incorporating these strategies, you can significantly reduce the likelihood of triggering CAPTCHAs, allowing for smoother and less disruptive interactions with websites.

How To Avoid CAPTCHAs Using ScrapeOps Proxy Aggregator

Using a service like the ScrapeOps Proxy Aggregator can significantly reduce the likelihood of encountering anti-bot CAPTCHAs during web scraping activities.

ScrapeOps simplifies the process of proxy management by handling the selection and rotation of proxies automatically. As a result, you can focus on the data you want to extract rather than on navigating around anti-scraping measures.

ScrapeOps offers an all-in-one proxy API that integrates over 20 different proxy providers. This aggregation allows users to access a vast range of IPs and geolocations, enhancing the ability to mimic real user behavior more accurately and thereby avoiding detection by sophisticated anti-bot systems.

By distributing requests across numerous proxies, ScrapeOps helps to maintain a low profile, preventing your scraping activities from triggering CAPTCHAs or getting blocked.

Let's take a look at how we can scrape an Amazon page first when detected and getting through with no issues with ScrapeOps Proxy Aggregator.

First, set up a simple Amazon scraper and see if it runs into any CAPTCHAs:

import requests

import requests

custom_headers = {
"Accept-language": "en-GB,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"User-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15",

url = ""

response = requests.get(url, headers=custom_headers)

with open('res.html', 'w') as file:

This a simple script that sends a request to Amazon and fetches the HTML of the page, then saves it as a file for inspection. However, we can see the issue when we open up the resulting HTML file:

Amazon Text Captcha

Not looking good! Let’s now use the ScrapeOps to avoid Amazon CAPTCHA while scraping:

import requests
import json

def make_request(api_key, target_url):
"""Make a request through ScrapeOps Proxy Aggregator and return the response."""
proxy_url = ''
full_url = f"{proxy_url}?api_key={api_key}&url={requests.utils.quote(target_url)}"
custom_headers = {
"Accept-language": "en-GB,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"User-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15",
response = requests.get(full_url, headers=custom_headers)
return response

def save_html_to_json(html_content, file_name):
"""Save HTML content to a JSON file."""
with open(file_name, 'w') as json_file:
json.dump({'html_content': html_content}, json_file, indent=4)
print(f"HTML content successfully saved in JSON format to {file_name}.")

def scrape_web_page(api_key, target_url, json_file_path):
"""Scrape a web page and save the content to a JSON file."""
response = make_request(api_key, target_url)
if response.status_code == 200:
save_html_to_json(response.text, json_file_path)
print("Failed to retrieve the page:", response.status_code)

# Your ScrapeOps API key and the target Amazon product URL
api_key = 'your_scrapeops_api_key'
target_url = ""
json_file_path = 'res.json'

# Scrape the webpage and save the result
scrape_web_page(api_key, target_url, json_file_path)

If we look at our results, we can see that the page was scraped successfully without any CAPTCHA solving:

Amazon Product Page

  • Proxy Integration: Compared to the first version, the script now makes requests through the ScrapeOps Proxy Aggregator by appending the target URL to the aggregator's API endpoint. This setup manages all proxy requirements, including rotation and selection, ensuring that your scraping activity remains uninterrupted and less likely to trigger CAPTCHAs.
  • Response Handling: If the response is successful, the HTML content of the page is saved both as an HTML file or in a JSON format, depending on your requirements.

Make sure to replace your_scrapeops_api_key with your actual ScrapeOps API key. This integration allows you to scrape data effectively while minimizing the risk of triggering security mechanisms such as CAPTCHAs. Do not forget to check ScrapeOps for the details!

Bypassing CAPTCHAs raises significant legal and ethical concerns. CAPTCHAs serve as a fundamental security measure on websites, designed to prevent automated software from engaging in harmful activities such as data scraping, spamming, and more aggressive forms of cyber-attacks like denial-of-service (DoS) attacks.

These mechanisms help protect sensitive user information, including account credentials and credit card details, from unauthorized access and exploitation.

Ethically, bypassing CAPTCHAs undermines the integrity and security frameworks that websites put in place to safeguard their services and their users.

The act of circumventing these controls can contribute to the destabilization of systems and compromise user trust. It is essential to consider the broader impact of such actions, which may facilitate activities that exploit or harm individuals or organizations.

The use of bots or other automated tools to bypass CAPTCHAs often violates the terms of service agreed upon by users when registering on most platforms. Websites typically include clauses that prohibit such behavior, and violating these terms can lead to various penalties including account suspension or termination.

In some jurisdictions, more severe legal actions could be pursued, especially if the bypass leads to significant financial loss or breaches of privacy.

It is crucial for users and developers to respect the terms of service and privacy policies of websites. These documents not only outline permissible behaviors but also reflect compliance with broader legal requirements, such as those related to data protection and privacy laws (e.g., GDPR, CCPA). Ignoring these terms can result in legal repercussions and damage reputations.

Some of the potential consequences of misusing CAPTCHA solving tools:

  • Account Actions: Immediate penalties can include the suspension or permanent banning of user accounts, particularly on platforms where security and user verification are priorities.
  • Legal Penalties: Depending on the severity and impact of the misuse, entities affected by unauthorized data access or service disruptions may pursue legal action, potentially leading to fines or other civil penalties.
  • Reputational Damage: Organizations or individuals found to be bypassing CAPTCHAs may suffer reputational harm, which can deter partnership opportunities and erode user trust.

Understanding and adhering to the ethical guidelines and legal restrictions associated with CAPTCHA solving is essential for maintaining the security and integrity of online services. It is important for users and developers to consider the potential consequences of their actions and choose to engage with technology responsibly.


In this article, we have explored various strategies to programmatically solve CAPTCHAs using Python, leveraging powerful open-source libraries. From the simple application of Optical Character Recognition (OCR) to more complex image preprocessing techniques and paid tools, Python provides a robust toolkit for addressing the challenges posed by CAPTCHAs.

Our exploration revealed the depth and complexity of strategies required to solve CAPTCHAs, from using the capabilities of tools like Selenium, pytesseract and its variants to employing advanced solutions like ScrapeOps Proxy Aggregator.

Stay curious and continue learning about new image processing techniques, and OCR improvements. Engage with the community through forums, contribute to open source projects, and keep testing your solutions against new types of CAPTCHAs. Through responsible practice and continuous learning, you can contribute positively to the field while navigating its ethical landscape.

In conclusion, solving CAPTCHAs with Python is a perfect example of where programming meets real-world challenges. It is a field that combines technical skills with ethical considerations, offering a rich area for development and personal growth. Whether for educational purposes, accessibility enhancements, or authorized testing, continue to approach CAPTCHA solving with curiosity and respect for digital boundaries.

More Python Web Scraping Guides

If you would like to learn more about Web Scraping with Python, then be sure to check out The Python Web Scraping Playbook.

Or check out one of our more in-depth guides: