Solving hCaptcha with Puppeteer and Python
Tags: automation, python, beginners, tutorial
Published: 2023-07-19 11:16:54


---
canonical_url: https://baransel.dev/post/how-to-bypass-hcaptcha/
---

> [Sign up to my newsletter for more tutorials!](https://baransel.dev/newsletter/).

> Disclaimer: This information is for educational purposes only.

## What does Captcha mean?

Captcha mechanisms have become an integral part of web security, ensuring that the interaction between humans and websites remains genuine. However, as web developers and automation enthusiasts, we often encounter challenges when trying to bypass captchas programmatically. In this blog post, we will explore an effective solution using Puppeteer and Python in combination with [CapSolver](https://www.capsolver.com/), a powerful captcha-solving service.

## What is hCaptcha?

Before diving into the implementation details, let's briefly discuss hCaptcha. Developed as an alternative to Google's reCAPTCHA, hCaptcha is a popular captcha service that presents users with various challenges to verify their humanity. It involves tasks such as selecting specific images, solving puzzles, or answering questions. hCaptcha offers enhanced security while maintaining user privacy, making it a preferred choice for many websites.

## What is CapSolver?

[CapSolver](https://www.capsolver.com/) offers automated solutions for different types of captchas, simplifying the process for developers and automation enthusiasts.

* reCAPTCHA v2/v3/Enterprise: CapSolver can handle various versions of Google's reCAPTCHA, including v2, v3, and reCAPTCHA Enterprise. These captchas are widely used to verify user interactions and detect bots on websites.
* FunCaptcha: FunCaptcha is a type of captcha that presents users with interactive and gamified challenges to prove their human identity. CapSolver can effectively solve FunCaptcha challenges.
* DataDome: DataDome is an anti-bot solution that utilizes sophisticated algorithms to protect websites from malicious bots. CapSolver can help bypass DataDome captchas, ensuring smooth automation.
* Anti-bot Solution: CapSolver supports solving captchas implemented through custom anti-bot solutions. These captchas are specifically designed to detect and block automated bot activities.
* hCaptcha Normal/Enterprise: hCaptcha is a popular alternative to Google's reCAPTCHA and is known for its enhanced security and user privacy features. CapSolver can handle both hCaptcha Normal and hCaptcha Enterprise challenges.
* GeeTest V3: GeeTest V3 is a type of captcha that presents users with interactive puzzles or challenges to verify their human identity. CapSolver can effectively solve GeeTest V3 captchas.
* ImageToText: ImageToText captchas involve recognizing and transcribing text from images. CapSolver has the capability to decipher these image-based captchas accurately.

Please note that while [CapSolver](https://www.capsolver.com/) supports these captcha methods at the time of writing, it's advisable to consult CapSolver's official documentation or contact their support team for the most up-to-date and comprehensive information on supported captchas.

## Signing up for CapSolver and getting the API key

To get started with CapSolver, [sign up](https://dashboard.capsolver.com/passport/register) for a free account on their website. Once you have signed up, you will receive an API key that you can use to access CapSolver's API.


## Using Pyppeteer Python with CapSolver: A Step-by-Step Guide

### Step 1: Prerequisites
Before diving into the implementation, ensure that you have the following prerequisites in place:

Install `pyppeteer` & `capsolver-api` python packages:

```bash
python3 -m pip install pyppeteer capsolver-api
```

### Step 2: Importing the required libraries
Once you have the prerequisites in place, import the required libraries:

```python
import asyncio
from pyppeteer import launch
from capsolver_api import HCaptchaTask
```

### Step 3: Launching the browser

Next, launch the browser using Puppeteer:

```python
browser = await launch(headless=False)
```

### Step 4: Creating a new page

Once the browser is launched, create a new page:

```python
page = await browser.newPage()
```

### Step 5: Navigating to the website

Next, navigate to the website that presents the hCaptcha challenge:

```python
url = 'https://accounts.hcaptcha.com/demo'
await page.goto(url)
```

### Step 6: Finding the website key

Once the page is loaded, find the website key:

```python
element = await page.querySelector('#hcaptcha-demo')
website_key = await page.evaluate('(element) => element.getAttribute("data-sitekey")', element)
```

### Step 7: Solving the captcha

Once the page is loaded, solve the captcha using CapSolver:

```python
capsolver = HCaptchaTask('your_capsolver_api_key')

task_id = capsolver.create_task(task_type='HCaptchaTaskProxyLess',
                                website_url=url,
                                website_key=website_key
                                )

captcha_key = capsolver.get_solution(task_id)
```

### Step 8: Submitting the captcha

Once the captcha is solved, submit the captcha:

```python
await page.type('textarea[name="h-captcha-response"]', captcha_key)
await page.click('input[type="submit"]')
```

## Full code

```python
import asyncio
from pyppeteer import launch
from capsolver_api import HCaptchaTask


async def main():
    url = 'https://accounts.hcaptcha.com/demo'
    browser = await launch(headless=False)
    page = await browser.newPage()
    await page.goto(url)
    element = await page.querySelector('#hcaptcha-demo')
    website_key = await page.evaluate('(element) => element.getAttribute("data-sitekey")', element)

    capsolver = HCaptchaTask('your_capsolver_api_key')

    task_id = capsolver.create_task(task_type='HCaptchaTaskProxyLess',
                                    website_url=url,
                                    website_key=website_key
                                    )

    captcha_key = capsolver.get_solution(task_id)

    await page.waitForSelector('iframe')
    await page.type('textarea[name="h-captcha-response"]', captcha_key)
    await page.click('input[type="submit"]')
    await page.waitFor(2000)
    await page.screenshot({'path': 'solve.png'}) # screenshot of the solved captcha

asyncio.get_event_loop().run_until_complete(main())
```

## Conclusion

In this blog post, we explored how to use [CapSolver](https://www.capsolver.com/) to solve hCaptcha challenges using Puppeteer and Python. CapSolver offers a simple and effective solution for solving different types of captchas, including hCaptcha, FunCaptcha, reCAPTCHA, and more. It's a powerful tool that can help developers and automation enthusiasts bypass captchas and automate their workflows.