How to Solve Captcha in Puppeteer using CapSolver
Tags: solvecaptcha, captch, javascript, react
Published: 2023-08-24 20:15:48

## How to Solve Captcha in Puppeteer using CapSolver

![How to Solve Captcha in Puppeteer using CapSolver](https://raw.githubusercontent.com/omkarcloud/puppeter-captcha-solving-tutorial/master/images/featured-image.png)

## 🎯 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](https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php) using CapSolver.

![Captcha Form](https://raw.githubusercontent.com/omkarcloud/puppeter-captcha-solving-tutorial/master/images/recaptcha-v2-checkbox.png)

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](https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php).
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](https://pypi.org/project/capsolver-python/)**: The official Python SDK for easy integration with the CapSolver API.
- **[pyppeteer](https://pypi.org/project/pyppeteer/)**: pyppeteer is a Python port of Puppeteer.

Install these dependencies by running the following command:

```
python -m pip install pyppeteer capsolver-python
```

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

```bash
touch main.py
```

## 🔑 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](https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php) by following these steps:

1. Visit the [Captcha Form](https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php).

![Captcha Form](https://raw.githubusercontent.com/omkarcloud/puppeter-captcha-solving-tutorial/master/images/recaptcha-v2-checkbox.png)

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

![Site Key](https://raw.githubusercontent.com/omkarcloud/puppeter-captcha-solving-tutorial/master/images/site-key.png)

4. 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](https://www.capsolver.com/).

![Sign Up](https://raw.githubusercontent.com/omkarcloud/puppeter-captcha-solving-tutorial/master/images/sign-up.gif)

2. 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](https://raw.githubusercontent.com/omkarcloud/puppeter-captcha-solving-tutorial/master/images/add-funds.gif)

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

![Store API Key](https://raw.githubusercontent.com/omkarcloud/puppeter-captcha-solving-tutorial/master/images/store-api-key.png)

## 💡 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:**

```python
# 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)
```

**Solving the captcha using CapSolver:**

```python
# 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")
```

**Setting the solved captcha on the form and submitting it:**

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

## 🚀 Putting it all Together

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

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

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](https://raw.githubusercontent.com/omkarcloud/puppeter-captcha-solving-tutorial/master/images/solved-captcha-success-page.png)

## ✅ 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](https://www.capsolver.com/#pricing).

### 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.

```python
browser = await launch({'args': ['--proxy-server=ip:port'], 'headless': False})
```

- Set the proxy and user agent after creating the proxy task as follows:

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