DEV Community

Markus
Markus

Posted on

Automating reCAPTCHA in Selenium: A Complete Guide

Image description
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

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

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

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

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

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!

Top comments (0)