DEV Community

beropex
beropex

Posted on

How to Solve Captcha in Puppeteer using CapSolver

How to Solve Captcha in Puppeteer using CapSolver

How to Solve Captcha in Puppeteer using CapSolver

🎯 Overview

This tutorial will guide you through the process of solving Google reCaptchas in Puppeteer using CapSolver.

CapSolver is a Captcha Solving Service that helps you solve Captchas.

We use AI-powered Captcha Solving Algorithms, which result in faster solving speed and significantly reduced costs, providing an excellent developer experience.

The goal will be to solve the captcha located at recaptcha-demo.appspot.com using CapSolver.

Captcha Form

During the tutorial, we will take the following steps to solve the above Captcha:

  1. Install the required dependencies.
  2. Find the site key of the Captcha Form.
  3. Set up CapSolver.
  4. Solve the captcha.

📦 Install Required Dependencies

To get started, we need to install the following dependencies for this tutorial:

  • capsolver-python: The official Python SDK for easy integration with the CapSolver API.
  • pyppeteer: pyppeteer is a Python port of Puppeteer.

Install these dependencies by running the following command:

python -m pip install pyppeteer capsolver-python
Enter fullscreen mode Exit fullscreen mode

Now, Create a file named main.py where we will write the Python code for solving captchas.

touch main.py
Enter fullscreen mode Exit fullscreen mode

🔑 Get Site Key of Captcha Form

The Site Key is a unique identifier provided by Google that uniquely identifies each Captcha.

To solve the Captcha, it is necessary to send the Site Key to CapSolver.

Let's find the Site Key of the Captcha Form by following these steps:

  1. Visit the Captcha Form.

Captcha Form

  1. Open Chrome Dev Tools by pressing Ctrl/Cmd + Shift + I.
  2. Go to the Elements tab and search for data-sitekey. Copy the attribute's value.

Site Key

  1. Store the Site Key in a secure place as it will be used in a later section when we submit the captcha to CapSolver.

🤖 Setup CapSolver

To solve captchas using CapSolver, you need to create a CapSolver account, add funds to your account, and obtain an API Key. Follow these steps to set up your CapSolver account:

  1. Sign up for a CapSolver account by visiting capsolver.com.

Sign Up

  1. Add funds to your CapSolver account using PayPal, Crypto Currencies, or other listed payment methods. Please note that the minimum deposit amount is $6, and additional taxes apply.

Add Funds

  1. Now, copy the API Key provided by CapSolver and store it securely for later usage.

Store API Key

💡 Solving the Captcha

Now, we will proceed to solving the captcha using CapSolver. The overall process involves three steps:

  1. Launching the browser and visiting the captcha page using pyppeteer.
  2. Solving the captcha using CapSolver.
  3. Submitting the captcha response.

Read the following Code Snippets to understand these steps.
Launching the browser and visiting the captcha page:

# Launch the browser.
browser = await launch({'headless': False})

# Load the target page.
captcha_page_url = "https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php"
page = await browser.newPage()
await page.goto(captcha_page_url)
Enter fullscreen mode Exit fullscreen mode

Solving the captcha using CapSolver:

# Solve the reCAPTCHA using CapSolver.
capsolver = RecaptchaV2Task("YOUR_API_KEY")

site_key = "6LfW6wATAAAAAHLqO2pb8bDBahxlMxNdo9g947u9"
task_id = capsolver.create_task(captcha_page_url, site_key)
result = capsolver.join_task_result(task_id)

# Get the solved reCAPTCHA code.
code = result.get("gRecaptchaResponse")
Enter fullscreen mode Exit fullscreen mode

Setting the solved captcha on the form and submitting it:

# Set the solved reCAPTCHA code on the form.
recaptcha_response_element = await page.querySelector('#g-recaptcha-response')
await page.evaluate(f'(element) => element.value = "{code}"', recaptcha_response_element)

# Submit the form.
submit_btn = await page.querySelector('button[type="submit"]')
await submit_btn.click()
Enter fullscreen mode Exit fullscreen mode

🚀 Putting it all Together

Below is the complete code for the tutorial, which will solve the captcha using CapSolver.

import asyncio
from pyppeteer import launch
from capsolver_python import RecaptchaV2Task

# Following code solves a reCAPTCHA v2 challenge using CapSolver.
async def main():
    # Launch Browser.
    browser = await launch({'headless': False})

    # Load the target page.
    captcha_page_url = "https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php"
    page = await browser.newPage()
    await page.goto(captcha_page_url)

    # Solve the reCAPTCHA using CapSolver.
    print("Solving captcha")
    capsolver = RecaptchaV2Task("YOUR_API_KEY")

    site_key = "6LfW6wATAAAAAHLqO2pb8bDBahxlMxNdo9g947u9"
    task_id = capsolver.create_task(captcha_page_url, site_key)
    result = capsolver.join_task_result(task_id)

    # Get the solved reCAPTCHA code.
    code = result.get("gRecaptchaResponse")
    print(f"Successfully solved the reCAPTCHA. The solve code is {code}")

    # Set the solved reCAPTCHA code on the form.
    recaptcha_response_element = await page.querySelector('#g-recaptcha-response')
    await page.evaluate(f'(element) => element.value = "{code}"', recaptcha_response_element)

    # Submit the form.
    submit_btn = await page.querySelector('button[type="submit"]')
    await submit_btn.click()

    # Pause the execution so you can see the screen after submission before closing the driver
    input("Captcha Submission Successfull. Press enter to continue")

    # Close Browser.
    await browser.close()

if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(main())
Enter fullscreen mode Exit fullscreen mode

Paste the above code into your main.py file. Replace YOUR_API_KEY with your API Key and run the code.

You will observe that the captcha will be solved, and you will be greeted with a success page 🥳.

Solved Captcha Success Page

✅ Conclusion

Congratulations! You have successfully learned how to solve captchas using CapSolver. Here are the key learnings from this tutorial:

  • CapSolver offers a fast and cost-effective solution for captcha solving.
  • The general process to solve the captcha involves:
    • Creating an account on CapSolver and adding funds to it.
    • Finding the Site Key of the target captcha form.
    • Submitting the Site Key and Page URL to CapSolver to solve the captcha.
    • Setting the solved captcha response on the appropriate element and submitting the form.

Thank you for your time. 🙏 We wish you good luck on your automation journey! 🚀

🤔 FAQs

What is the Cost of Solving Captchas?

We employ AI-powered captcha solving algorithms instead of human solvers, resulting in significantly lower captcha solving costs. Hence, our rates are highly competitive in the market.

For example, the cost of solving Google v2/v3 captchas ranges from $0.8 to $1.

To learn more about our pricing in detail, we encourage you to visit our pricing page here.

I am successfully able to solve reCaptcha located at recaptcha-demo.appspot.com but when I try to solve anothet reCaptcha, it does not get solved. Why?

In this tutorial, we solved Google reCaptcha V2, but your target website is most likely using Google reCaptcha V3.

When solving Google reCaptcha V3, it is important to ensure that the IP address and user agent used to submit the captcha match the ones used to solve it.

To maintain the same IP address, you can use a proxy service. We recommend using residential IPs from reputable providers such as Asocks, MetaProxies, or BrightData.

Once you have residential proxies available, you can update the code as follows to solve the captcha successfully:

  • Use a proxy when launching Puppeteer.
browser = await launch({'args': ['--proxy-server=ip:port'], 'headless': False})
Enter fullscreen mode Exit fullscreen mode
  • Set the proxy and user agent after creating the proxy task as follows:
capsolver = RecaptchaV2Task("YOUR_API_KEY")

capsolver.set_proxy(proxy_address='proxy_ip', proxy_port='proxy_port', proxy_login='user', proxy_password='password')

browser_useragent = browser.userAgent()
capsolver.set_user_agent(browser_useragent)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)