Join And Get Free Trial!

How to Bypass reCaptcha v3 in Python with Code Example#

logoNextCaptcha
August 01,2024

Overview#

Automating website interactions can greatly streamline testing and repetitive tasks. However, a frequently arising problem is reCAPTCHA recognition, which aims to differentiate between human users and bots. This guide will show you how to solve reCAPTCHA v3 challenges using Python and the NextCaptcha service, which uses human solvers to handle these CAPTCHAs in an ethical and legal manner.

Preparation#

Before you begin, you'll need to:
  • Sign up for a NextCaptcha account: Go to NextCaptcha and sign up to get your API key.

  • Install necessary Python packages: Ensure you have requests installed. You can install it using pip if you don't have it already:

      pip install requests

Finding the Sitekey#

To solve a reCAPTCHA, you need the site key which is embedded in the HTML of the page where the reCAPTCHA is used. Here's how you can find it:
  • Navigate to the page with the reCAPTCHA.

  • Inspect the page's source: Right-click on the page and select "Inspect" or "View Page Source".

  • Search for the site key: Look for the data-sitekey attribute in the HTML. It will look something like this:

      <div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>

    Copy this YOUR_SITE_KEY value.

Solving the Captcha#

Now that you have the site key, you can use the NextCaptcha API to solve the reCAPTCHA challenge.
  • Send a request to NextCaptcha to solve the reCAPTCHA v3:

    import requests
     
    # Your NextCaptcha API key
    API_KEY = 'YOUR_NextCAPTCHA_API_KEY'
     
    # The URL of the page with the reCAPTCHA
    url = 'URL_OF_THE_PAGE_WITH_RECAPTCHA'
     
    # The site key for reCAPTCHA v3
    site_key = 'YOUR_SITE_KEY'
     
    # Requesting the reCAPTCHA solution
    response = requests.post('https://api.nextcaptcha.com/createTask', data={
        "clientKey": API_KEY,
        "task": {
            "type":"RecaptchaV3TaskProxyless",
            "websiteURL":url,
            "websiteKey":site_key
        }
    })
     
    request_id = response.json().get('taskId')
     

  • Poll for the solution:

    import time
     
    solution = None
    while solution is None:
        time.sleep(1)  # Wait a few seconds before checking again
        response = requests.post('https://api.nextcaptcha.com/getTaskResult', data={
    		  "clientKey": "api key",
    		  "taskId": request_id
    		})
        if response.json().get('errorId') == 0:
            solution = response.json().get('solution')
     
    # The reCAPTCHA token
    recaptcha_token = solution["gRecaptchaResponse"]
    print(f'reCAPTCHA token: {recaptcha_token}')
     

Submitting the Solved Captcha#

After obtaining the reCAPTCHA token, you can submit it along with the form or request that required the CAPTCHA solution. Here’s an example of how you might submit the form:
# URL where the form is submitted
form_submit_url = 'URL_TO_SUBMIT_FORM'
 
# Form data including the reCAPTCHA token
form_data = {
    'some_form_field': 'value',
    'g-recaptcha-response': recaptcha_token
}
 
# Submit the form
response = requests.post(form_submit_url, data=form_data)
print(response.text)
 

Full Code for Automatic reCaptcha Solving#

Combining all the steps, here is the complete code:

import requests
import time
 
# Your NextCaptcha API key
API_KEY = 'YOUR_NextCAPTCHA_API_KEY'
 
# The URL of the page with the reCAPTCHA
url = 'URL_OF_THE_PAGE_WITH_RECAPTCHA'
 
# The site key for reCAPTCHA v3
site_key = 'YOUR_SITE_KEY'
 
# Requesting the reCAPTCHA solution
response = requests.post('https://api.nextcaptcha.com/createTask', data={
    "clientKey": API_KEY,
    "task": {
        "type":"RecaptchaV3TaskProxyless",
        "websiteURL":url,
        "websiteKey":site_key
    }
})
 
task_id = response.json().get('taskId')
 
solution = None
while solution is None:
    time.sleep(1)  # Wait a few seconds before checking again
    response = requests.post('https://api.nextcaptcha.com/getTaskResult', data={
		  "clientKey": "api key",
		  "taskId": task_id
		})
    if response.json().get('errorId') == 0:
        solution = response.json().get('solution')
 
# The reCAPTCHA token
recaptcha_token = solution["gRecaptchaResponse"]
print(f'reCAPTCHA token: {recaptcha_token}')
 
# URL where the form is submitted
form_submit_url = 'URL_TO_SUBMIT_FORM'
 
# Form data including the reCAPTCHA token
form_data = {
    'some_form_field': 'value',
    'g-recaptcha-response': recaptcha_token
}
 
# Submit the form
response = requests.post(form_submit_url, data=form_data)
print(response.text)
By following this guide, you can automate the process of solving reCAPTCHA v3 challenges in a way that respects the terms of service of the sites you interact with.