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.
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.
- 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.
The project is structured as follows:
utils/actions.py
: Contains thePageActions
class, which encapsulates common browser actions (clicking, switching frames, etc.).utils/helpers.py
: Contains theCaptchaHelper
class, responsible for solving captchas, executing JS, and handling captcha error messages.js_scripts/
: JavaScript files that extract captcha data and track image updates.
git clone [email protected]:2captcha/selenium-recaptcha-solver-using-grid.git
cd selenium-recaptcha-solver-using-grid
- 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
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.
python main.py
- Browser Initialization: A browser is opened using Selenium WebDriver.
- Captcha Data Retrieval: JavaScript extracts the image tiles from reCAPTCHA and sends them to the 2Captcha service for solving.
- Captcha Submission: Once a solution is received from 2Captcha, Selenium simulates clicking on the correct image tiles based on the solution.
- Final Submission: The solution is submitted once the captcha is solved.
- 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.
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.
get_captcha_data.js
: Extracts captcha image tiles for solving. The source code of the script is located here https://gist.github.com/kratzky/20ea5f4f142cec8f1de748b3f3f84bfctrack_image_updates.js
: Monitors requests to check if captcha images are updated.