Automating reCAPTCHA in Selenium: A Complete Guide
Tags: webdev, beginners, tutorial, python
Published: 2023-11-15 11:29:30


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdfk7g6hsptdzn9ep1d4.jpg)
Welcome, Dev.to community! Today, I’m sharing a comprehensive walkthrough on automating reCAPTCHA verification within your Selenium tests. This tutorial is especially useful if you're dealing with reCAPTCHA challenges in your automation projects.

> I spied this manual in this [article](https://2captcha.com/p/selenium-captcha-solver)

## Getting Ready

Before diving into the code, make sure you've got all the tools. Install Python, and then set up your Selenium environment. Here's what you need:

Selenium: Our mainstay for browser automation, grab it from PyPI.
2Captcha Python SDK: To integrate with the 2Captcha API, install it from PyPI.
Webdriver-manager: This tool will handle your drivers with ease. Get it from PyPI.
To install all components in one go, use:

```
python -m pip install 2captcha-python selenium webdriver-manager
```

## Locating the Sitekey

In reCAPTCHA lingo, the site key is the golden ticket. It's how we identify the CAPTCHA challenge to solve. So, let's find it on our demo page.

## The Code to Solve reCAPTCHA

Here's where the rubber meets the road. I’ve written a Python script that navigates to the target page, interacts with reCAPTCHA, and uses the 2Captcha service to solve it. Keep your 2Captcha API key and the site key you saved earlier handy—you'll need them here.

```
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

captcha_page_url = "https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php"
driver.get(captcha_page_url)

# captcha solving

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}")
```

## Implementing the Solution

After solving the CAPTCHA, we need to submit the resolution. Here's how we do it in the script:

```
recaptcha_response_element = driver.find_element(By.ID, 'g-recaptcha-response')
driver.execute_script(f'arguments[0].value = "{code}";', recaptcha_response_element)

submit_btn = driver.find_element(By.CSS_SELECTOR, 'button[type="submit"]')
submit_btn.click()

input("Press enter to continue")
driver.close()
```

## Wrapping Up

And that's it! Here is final code you can use to solve reCaptcha:

```
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()
```

I hope this guide helps you automate one of the web's common nuisances. If you have any thoughts or questions, drop them in the comments below!