- Published on
🚀 How to Solve Captcha in Selenium Using 2captcha 🤖
- Authors
- Name
- Chetan Jain
Overview
When using Selenium, it is common to encounter captchas, especially when automating website tasks such as form submissions of sign-up and sign-in pages.
This article provides a comprehensive guide on how to solve captchas in Selenium using 2captcha, a captcha solving service.
Additionally, if you prefer to watch interactive video version of the article, please visit https://youtu.be/X_tYgPUk1ws.
What is 2Captcha
2Captcha is a cost-effective captcha solving service that helps us solve captchas with help of human workers. The usage of 2captcha is simple, We send the unsolved captcha details to 2Captcha, which are solved by humans and the solved captcha code is sent back to us for usage in our applications.
2captcha is a popular captcha service that helps solves a wide range of captchas such as FunCaptcha, hCaptcha, reCaptcha and the pricing of 2captcha is also very reasonable. For example, the cost of solving 1000 Google reCaptcha, ranges from $1 to $3.
Setup 2Captcha
In this tutorial we will use 2captcha to solve captcha challenges. Follow these Steps to create an Account on 2captcha and obtain 2captcha API Key:
- Visit https://2captcha.com/ and create an Account by clicking the “Sign Up” Button
- Add funds to your 2captcha account by clicking the "Add Funds" button. Note that you can pay via your card, PayPal, or any other listed payment method and the minimum deposit amount is $3.
- Store the API Key in a secure place to be used later.
Solving reCaptcha
Now is the fun part where we will solve the reCaptcha available on https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php using 2captcha. At a higher level, we will take the following Steps:
- Install the required dependencies
- Find the Site Key of the reCaptcha challenge on the target website
- Solve the reCaptcha using 2Captcha
- Submit Solved Captcha
Installing Required Libraries
In this project, we will be using the following libraries
- 2captcha-python: Official Python SDK for easy integration with 2captcha API .
- selenium: Selenium is a browser automation library.
- webdriver-manager: This library simplifies the download and usage of drivers for Selenium.
To install these libraries, run the following command:
python -m pip install 2captcha-python selenium webdriver-manager
Next, let's create a main.py
file where we will write our captcha-solving code.
touch main.py
Find Site Key
The Site Key is a unique identifier given by Google to all Captcha forms, which uniquely identifies the Captcha. To solve the Captcha, we need to send the Site Key to 2captcha.
To find Site Key of our Target Site https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php. Follow these steps:
Visit https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php
Open the development tools by pressing Ctrl/Cmd + Shift + I.
Search for
data-sitekey
and copy its value.Store the Site Key in a secure place to be used later.
Solve the Captcha
Next, we will write Selenium code to visit the target page and solve the Captcha using 2captcha.
The following code does just that, remember to replace 2CAPTCHA_API_KEY
with your 2captcha API key and SITE_KEY
with the Site Key you stored earlier.
from selenium.webdriver.common.by import By
from twocaptcha import TwoCaptcha
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
# Instantiate the WebDriver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
# Load the target page
captcha_page_url = "https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php"
driver.get(captcha_page_url)
# Solve the Captcha
print("Solving Captcha")
solver = TwoCaptcha("2CAPTCHA_API_KEY")
response = solver.recaptcha(sitekey='SITE_KEY', url=captcha_page_url)
code = response['code']
print(f"Successfully solved the Captcha. The solve code is {code}")
In this Code, we initialize the TwoCaptcha
object with the 2captcha API Key and solve the Captcha by calling the recaptcha
method passing in the site key and the current page URL.
The recaptcha
method returns a dictionary containing the solved Captcha code which is logged to console.
Note that The Captcha solving process may take some time, so please be patient
Submit Solved Captcha
Next, we will find the g-recaptcha-response
element, set its value to the solved Captcha code, and submit the form.
# Set the solved Captcha
recaptcha_response_element = driver.find_element(By.ID, 'g-recaptcha-response')
driver.execute_script(f'arguments[0].value = "{code}";', recaptcha_response_element)
# Submit the form
submit_btn = driver.find_element(By.CSS_SELECTOR, 'button[type="submit"]')
submit_btn.click()
# Pause the execution so you can see the success screen after submission before closing the driver
input("Press enter to continue")
driver.close()
Final Code
The following is the final code of the Tutorial to solve Captcha using 2captcha
from selenium.webdriver.common.by import By
from twocaptcha import TwoCaptcha
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
# Instantiate the WebDriver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
# Load the target page
captcha_page_url = "https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php"
driver.get(captcha_page_url)
# Solve the Captcha
print("Solving Captcha")
solver = TwoCaptcha("2CAPTCHA_API_KEY")
response = solver.recaptcha(sitekey='SITE_KEY', url=captcha_page_url)
code = response['code']
print(f"Successfully solved the Captcha. The solve code is {code}")
# Set the solved Captcha
recaptcha_response_element = driver.find_element(By.ID, 'g-recaptcha-response')
driver.execute_script(f'arguments[0].value = "{code}";', recaptcha_response_element)
# Submit the form
submit_btn = driver.find_element(By.CSS_SELECTOR, 'button[type="submit"]')
submit_btn.click()
# Pause the execution so you can see the screen after submission before closing the driver
input("Press enter to continue")
driver.close()
Replace 2CAPTCHA_API_KEY
, SITE_KEY
with their values and run the code, the Captcha will be solved and you will see the following success screen:
Conclusion
In this article we've learnt how to solve captcha with Selenium using 2captcha. To summarize, the general steps for solving captcha in Selenium are as follows:
- Create an account on 2captcha, add funds and note the API Key
- Note the Site key of the target Captcha.
- Submit the Site Key and Page URL to 2Captcha to solve the Captcha.
- Set the solved Captcha code on the appropriate element and submit the form.
It's worth noting that the process of solving other Captchas, such as FunCaptcha by Arkose Labs, is similar. To learn more about solving different types of Captchas, please visit https://2captcha.com/lang/python.
FAQs
Where is the final Code?
Please find the final code at following GitHub Repository https://github.com/omkarcloud/selenium-2captcha-recaptcha-solver-demo.
What is the Cost of Solving Captchas?
There are various types of Captchas available, such as FunCaptcha, reCaptcha, and hCaptcha. The cost of solving a Captcha depends on the type being solved.
For example, if you're solving reCaptcha by Google, which happens to be the most popular Captcha, the price ranges from $1 to $3 per 1000 Captcha solves.
To find more detailed cost for solving captchas, please visit https://2captcha.com/pricing.