Skip to content

Solving reCAPTCHA v2 using clicks using the grid method. Python + Selenium Example

License

Notifications You must be signed in to change notification settings

2captcha/selenium-recaptcha-solver-using-grid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

reCAPTCHA Solver Using 2Captcha and Selenium

This project automates bypass Google reCAPTCHA v2 with image challenges (3x3 and 4x4) using the 2Captcha service (captcha solver) and Selenium WebDriver. The script programmatically interacts with reCAPTCHA, retrieves data for solving, sends it to reCAPTCHA solver for processing, and then submits the solution.

bypass_recaptcha_v2_selenium_final2.gif

2Captcha service allows you to bypass any CAPTCHA using Selenium. You can find more details on Selenium captcha solver page.

If you are using JavaScript, we also have a similar example for JavaScript Puppeteer, the example is in the reCAPTCHA Solver using 2Captcha and Puppeteer repository.

Features

  • Selenium WebDriver: Interacts with the browser and manipulates elements on the reCAPTCHA page.
  • 2Captcha API: Solves image-based captchas using artificial intelligence.
  • Handles both 3x3 and 4x4 captchas with custom logic for each.
  • Modular design with separated logic into helper classes for easy code maintenance and future expansion.
  • Tracks image updates and handles captcha error messages efficiently using custom error handling.

Code Structure

The project is structured as follows:

  • utils/actions.py: Contains the PageActions class, which encapsulates common browser actions (clicking, switching frames, etc.).
  • utils/helpers.py: Contains the CaptchaHelper class, responsible for solving captchas, executing JS, and handling captcha error messages.
  • js_scripts/: JavaScript files that extract captcha data and track image updates.

Usage

Clone:

git clone [email protected]:2captcha/selenium-recaptcha-solver-using-grid.git
cd selenium-recaptcha-solver-using-grid

Requirements

  • Python >= 3.6
  • Installed Selenium WebDriver (for Chrome)
  • [2Captcha account][2captcha] and API key
  • Required Python libraries:
    • selenium
    • twocaptcha-python
pip install -r requirements.txt

Configure:

Set the APIKEY environment variable. You can get the APIKEY value in your personal [2captcha account][2captcha].

export APIKEY=your_api_key

You can also set the value of APIKEY directly in the code. To do this, modify the apikey value in the following file: main.py, line 10.

Example Command

python main.py

How It Works

  1. Browser Initialization: A browser is opened using Selenium WebDriver.
  2. Captcha Data Retrieval: JavaScript extracts the image tiles from reCAPTCHA and sends them to the 2Captcha service for solving.
  3. Captcha Submission: Once a solution is received from 2Captcha, Selenium simulates clicking on the correct image tiles based on the solution.
  4. Final Submission: The solution is submitted once the captcha is solved.

Captcha Solving Logic

  • 3x3 Captchas: Previous captcha ID (previousID) is saved to speed up solving when images are updated.
  • 4x4 Captchas: No previousID is saved, and each solution is processed from scratch.
  • Error Handling: Messages like “Please try again” are handled, and the solving process is retried if needed.

Modular Design

The project follows a modular design for better maintainability:

  • PageActions Class: Handles general browser interactions like switching to iframes, clicking elements, and returning focus to the main content.
  • CaptchaHelper Class: Encapsulates captcha-specific logic, such as solving the captcha via 2Captcha API, handling error messages, and executing JavaScript in the browser.

JavaScript Scripts