DEV Community

Cover image for How to solve captcha in Selenium
2captcha
2captcha

Posted on • Updated on

How to solve captcha in Selenium

We wrote an in-depth article - "How to solve captcha in Selenium"

Have made a brief instruction below, and with the full text you can read at our article - easiest selenium captcha solver.

The following is a step by step explanation of the reCAPTCHA bypass process at https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php:

Install the required components

Find the site key parameter of the reCAPTCHA on the target webpage
Solve the reCAPTCHA using API
Submit solved captcha
Installing components
You need to install the following libraries:

2Captcha: Official Python SDK for easy integration with 2Captcha API
Selenium
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
Enter fullscreen mode Exit fullscreen mode

Next, you need to find a site key parameter and create a Python file where you write captcha-solving code.

Find site key

The site key is a unique identifier given by Google to all reCAPTCHA an forms, which uniquely identifies the captcha. To solve the captcha, you need to send the site key to the captcha.

To find site key of webpage 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 to be used when submitting a solve request.

Solve the сaptcha

Next, 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}")
Enter fullscreen mode Exit fullscreen mode

In this code, we initialize the TwoCaptcha object with the 2captcha API Key and solve the Captcha by calling the recaptchamethod passing in the site key and the current page URL.

The recaptchamethod returns a answer containing the solved captcha code which is logged to the 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()
Enter fullscreen mode Exit fullscreen mode

Final code

The following is the final code of the tutorial to solve captcha using API.

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()
Enter fullscreen mode Exit fullscreen mode

Replace 2CAPTCHA_API_KEY, SITE_KEY with their values and run the code, the captcha will be solved and you will see the success screen.

Top comments (0)