DEV Community

Cover image for Solving hCaptcha with Puppeteer and Python
Baransel
Baransel

Posted on • Originally published at baransel.dev

Solving hCaptcha with Puppeteer and Python

Sign up to my newsletter for more tutorials!.

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, 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 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 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 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:

python3 -m pip install pyppeteer capsolver-api
Enter fullscreen mode Exit fullscreen mode

Step 2: Importing the required libraries

Once you have the prerequisites in place, import the required libraries:

import asyncio
from pyppeteer import launch
from capsolver_api import HCaptchaTask
Enter fullscreen mode Exit fullscreen mode

Step 3: Launching the browser

Next, launch the browser using Puppeteer:

browser = await launch(headless=False)
Enter fullscreen mode Exit fullscreen mode

Step 4: Creating a new page

Once the browser is launched, create a new page:

page = await browser.newPage()
Enter fullscreen mode Exit fullscreen mode

Step 5: Navigating to the website

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

url = 'https://accounts.hcaptcha.com/demo'
await page.goto(url)
Enter fullscreen mode Exit fullscreen mode

Step 6: Finding the website key

Once the page is loaded, find the website key:

element = await page.querySelector('#hcaptcha-demo')
website_key = await page.evaluate('(element) => element.getAttribute("data-sitekey")', element)
Enter fullscreen mode Exit fullscreen mode

Step 7: Solving the captcha

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

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)
Enter fullscreen mode Exit fullscreen mode

Step 8: Submitting the captcha

Once the captcha is solved, submit the captcha:

await page.type('textarea[name="h-captcha-response"]', captcha_key)
await page.click('input[type="submit"]')
Enter fullscreen mode Exit fullscreen mode

Full code

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())
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this blog post, we explored how to use CapSolver 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.

Oldest comments (1)

Collapse
 
sreno77 profile image
Scott Reno

What about the opposite? Is there a captcha package that AI can't easily beat? On my website captcha doesn't stop spam because of stuff like this.