![]()
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
- Understanding CAPTCHAs and Their Challenges
- How to Solve CAPTCHAs?
- Solving Text CAPTCHAs With Python
- Solving reCAPTCHAs With Python
- Solving hCAPTCHAs With Python
- Solving Audio CAPTCHAs With Python
- How To Avoid Triggering CAPTCHAs
- How To Avoid CAPTCHAs Using ScrapeOps Proxy Aggregator
- The Legal & Ethical Implications of Bypassing CAPTCHAs
Need help scraping the web?
Then check out ScrapeOps, the complete toolkit for web scraping.
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, andPillow. Usage ofSeleniumhelps in automation browser interactions, whilePillowandpytesseracthandle 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
browser.get('https://example.com/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 = Image.open(BytesIO(captcha_image))
captcha_text = pytesseract.image_to_string(image, config='--psm 8 --oem 3')
print("CAPTCHA Text:", captcha_text)
browser.quit()
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, soSeleniumcan 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'sscreenshot_as_pngfunction. -
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)optimizePytesseractto 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:



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 = Image.open('path_to_image.jpg')
# Use Tesseract to convert the image to text
text = pytesseract.image_to_string(img)
print(text)
EasyOCR
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:
print(result[1])
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 = Image.open('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
img.save('processed_image.jpg')
OpenCV
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-OCRfor 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-basedconvolutional 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
Kerasand 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
OpenCVto 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, andOpenCVto 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
reCAPTCHAv2,v2 callback,v2 invisible,v3, and Enterprise versions, as well ashCaptchas. - 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.
- This service is well-equipped to handle a variety of CAPTCHA types including
- 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
reCAPTCHAat 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:

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:

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 = Image.open(filename)
# Convert to black and white
black_and_white = original.convert("L")
black_and_white.save("black_and_white.png")
# Apply the first threshold
first_threshold = black_and_white.point(lambda p: p > th1 and 255)
first_threshold.save("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.save("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.save("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.Thresholdingis a pplied twice to highlight the text against the background, making it more distinguishable.Gaussian Blurhelps 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 7instruct 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 = 'http://2captcha.com/in.php'
# 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 = requests.post(post_url, files=files, data=data)
if response.ok:
print("CAPTCHA sent successfully!")
captcha_id = response.text.split('|')[1]
else:
print("Error in sending CAPTCHA:", response.text)
return None
# URL for retrieving the solved CAPTCHA
get_url = f'http://2captcha.com/res.php?key={api_key}&action=get&id={captcha_id}'
# Check for the solution every 5 seconds
for i in range(60):
time.sleep(5)
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...")
else:
print("Error in CAPTCHA solving:", result.text)
return None
# Example usage, make sure to enter your API key
api_key = 'YOUR_2CAPTCHA_API_KEY'
captcha_solution = solve_captcha(api_key, 'path_to_captcha.jpg')
print(f"Solved CAPTCHA text: {captcha_solution}")
solve_captchafunction 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.


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:

Now, let's move onto code:
from selenium_recaptcha_solver import RecaptchaSolver
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.options 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()
options.add_argument("--window-size=1920,1080")
# Setting a custom User-Agent
options.add_argument(f'--user-agent={test_ua}')
options.add_argument('--no-sandbox')
options.add_argument("--disable-extensions")
# 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
test_driver.get('https://www.google.com/recaptcha/api2/demo')
# 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
solver.click_recaptcha_v2(iframe=recaptcha_iframe)
Then, we can see selenium-recaptcha-solver solves the reCAPTCHA:

- The script first configures
ChromeDriverand initialize theSelenium WebDriverfor Chrome with the specified options. - An instance of
RecaptchaSolveris 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
PAGE_URL = 'https://www.google.com/recaptcha/api2/demo'
def solve_recaptcha(api_key, site_key, page_url):
# 2Captcha URL to submit the solve request
solve_url = 'http://2captcha.com/in.php'
# 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 = requests.post(solve_url, 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'http://2captcha.com/res.php?key={api_key}&action=get&id={request_id}&json=1'
# Wait a few seconds between checks
time.sleep(10)
# 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...')
time.sleep(5)
else:
print('Error solving CAPTCHA:', result_data['request'])
break
else:
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.