Pull to refresh

How to Bypass reCaptcha in Selenium Automatically with Code Example

Level of difficultyEasy
Reading time4 min
Views1.5K
Original author: Alex Gerasimchuk

I based my approach on an English manual that caught my eye just a couple of days ago, and I decided to test it (since it's written by a captcha recognition service that I use, why not - by the way, guys from 2captcha - I accept thanks in the form of green bills, if you're interested))))

I've made some tentative attempts at automation and encountered a frequently arising problem: reCaptcha recognition.

Of course, I understand that there are many guides, manuals, and articles written on this topic, but let's agree - it's interesting to describe one's own experience.

Thus, without getting too lengthy and boring about why Selenium is necessary, how important it is for automation, when it appeared and who invented it, let's get to the crux:

To solve the problem, we'll use a demo page kindly provided by reCaptcha itself - https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php:

Preparation

In the first stage, everything needs to be prepared.

For the demonstration, I downloaded the following components into a special folder on my computer (having previously installed Python, of course - but I won't describe how I did that, I hope you can figure it out).

So, what we will need is:

  1. The Selenium library for browser automation itself, which can be found here - https://pypi.org/project/selenium/

  2. The official Python SDK for integrating with the 2Captcha API, available here: https://pypi.org/project/2captcha-python/

  3. A library that simplifies the downloading and use of drivers for Selenium, called webdriver-manager, can be found here - https://pypi.org/project/webdriver-manager/

Everything installs very simply, just copy the provided command and paste it into the console, as shown in the video above.

You can install everything separately, as I did, or you can use a universal command.

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

Finding the Sitekey

Since we're dealing with reCaptcha, it's important to understand what a site key is. A site key is a unique identifier that Google assigns to all its forms with reCAPTCHA, which we use to identify the captcha on a website.

It is this identifier that we need so that the captcha recognition service can understand what it's dealing with, and it is this identifier that we will be sending to 2captcha.

Let's find the sitekey on the demo page.

  1. Go to https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php

  2. Open the developer tools - press Ctrl/Cmd + Shift + I or right-click and select "Inspect".

  3. Find the data-sitekey (press ctrl + F, enter sitekey, and press enter) and copy the value of the parameter.

  4. Save the value to use it when sending a request to solve the captcha on the page.

Solving the Captcha

Of course, to solve the captcha, you need to write some code - you can either write it yourself or simply take it from an existing manual, like this one. The purpose of this code is to navigate to the target page and solve the captcha through the API. This is exactly what we aim to achieve.

I should mention that there are a few parameters in the code that you need to change to your own, here they are, from top to bottom:

2CAPTCHA_API_KEY

2CAPTCHA_API_KEY - your API key, which you can get from your 2captcha account.

SITE_KEY - this is what we saved in the previous step.

I am very much counting on you being able to find these parameters in the provided code on your own.

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

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

# решение капчи

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

This piece of code initializes a TwoCaptcha object with our API key and is supposed to solve the reCAPTCHA by calling the recaptcha method. For this, we pass the site key and the URL of the page.

This is not the complete code, keep reading!

Submitting the Solved Captcha

The next piece of code I'm not exactly sure how to explain to you properly, as I didn't fully understand it myself, so I've just translated it and left it here:

Next, we find the g-recaptcha-response element, insert the obtained value to solve the captcha, and submit the form.

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

Full Code for Automatic reCaptcha Solving

Essentially, here's what we end up with as a result.

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

To demonstrate the functionality of the code, I'll create a text file, name it script.py, and run it in the console.

Tags:
Hubs:
Rating0
Comments1

Articles