The necessary information can be found in our repository here.
In this guide, we will show you how to solve FunCaptcha challenges using Python and the 2Captcha API. FunCaptcha is a type of CAPTCHA that requires human-like interaction to solve. Let’s break the solution into easy-to-follow steps.
Prerequisites
Before we begin, make sure you have the following:
- Python installed on your machine.
- A valid API key from 2Captcha.
- The necessary Python libraries:
seleniumbase
,twocaptcha
, andrequests
.
To install the required libraries, run:
pip install seleniumbase twocaptcha requests
Step 1: Setting Up the Python Environment
We start by importing the necessary libraries for interacting with the webpage and solving the CAPTCHA.
import os
import requests
from seleniumbase import Driver
from selenium.webdriver.common.by import By
from twocaptcha import TwoCaptcha
-
seleniumbase
: A wrapper for Selenium that makes browser automation easier. -
twocaptcha
: The Python client for 2Captcha API that will help us solve the CAPTCHA. -
requests
: Used to fetch the required attributes for solving FunCaptcha.
Step 2: Initialize 2Captcha API and WebDriver
Next, set up the 2Captcha solver and the Selenium WebDriver. The API_KEY is retrieved from your environment variables. Make sure to replace it with your actual API key.
API_KEY = os.environ["APIKEY"] # Replace with your 2Captcha API key
solver = TwoCaptcha(API_KEY)
agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"
driver = Driver(uc=True, proxy=False, headless=False, agent=agent)
-
uc=True
allows usage of undetected ChromeDriver to bypass bot detection mechanisms. -
headless=False
enables the browser UI for debugging purposes. Change to True if you want the browser to run in the background.
Step 3: Open the Website
Now, let’s load the website that has the FunCaptcha challenge. You can replace the URL with the target website URL.
url = "https://example.com/login" # Replace with your target website URL
driver.open(url)
Step 4: Input Login Credentials
If the CAPTCHA appears after login, we need to input credentials. For now, we’ll just simulate entering an email and password. Replace these values with your actual login details.
driver.type("#username", "[email protected]")
driver.type("#password", "your_password")
Step 5: Detect FunCaptcha Frame
Next, we check if FunCaptcha is present on the page by detecting the CAPTCHA frame. If it’s found, we proceed to solve it.
try:
frame1 = driver.find_element(By.ID, "captcha-internal")
print("Captcha found")
If the CAPTCHA is detected, we move on to the next step; otherwise, we skip the CAPTCHA-solving process.
Step 6: Extract FunCaptcha Attributes
Once the CAPTCHA frame is identified, we need to extract the sitekey
, surl
, and data_blob
that are required to solve the FunCaptcha challenge.
sitekey = "your-sitekey-here" # Replace with the actual site key
driver.switch_to.frame(frame1)
driver.switch_to.frame("arkoseframe")
frame3 = driver.find_element(By.CLASS_NAME, "r34K7X1zGgAi6DllVF3T.show.active.inline")
arkose_client = requests.get(frame3.get_attribute("src").split("#")[0].replace("html", "js")).text.split(";")[0]
arkose_client = arkose_client.split(" ")[1]
print("arkoseClient: ", arkose_client)
- The
sitekey
is required to identify the CAPTCHA. - The
arkose_client
is fetched from the JavaScript source and used to solve the CAPTCHA.
Step 7: Extract Data for CAPTCHA Solution
Using JavaScript, we retrieve additional information needed for solving the CAPTCHA.
driver.switch_to.frame(frame3)
data_all = driver.execute_script(f"return {arkose_client}.state")
data_blob = data_all["config"]["data"]["blob"]
surl = data_all["config"]["pageLevel"]["surl"]
print("surl: ", surl)
print("data_blob: ", data_blob)
-
data_blob
: A unique data blob representing the CAPTCHA challenge. -
surl
: A URL that provides information about the page level.
Step 8: Solve FunCaptcha Using 2Captcha API
Now that we have all the required data, we send it to the 2Captcha API to solve the FunCaptcha.
result = solver.funcaptcha(
sitekey=sitekey,
url=driver.current_url,
surl=surl,
**{'data[blob]': data_blob},
)
solution = result["code"] # Extract the solution
print("Captcha solved!")
The API returns the CAPTCHA solution (code
) which we will use to complete the form.
Step 9: Insert the CAPTCHA Solution
Now that we have the CAPTCHA solution, we need to insert it into the appropriate fields in the form.
driver.execute_script("document.getElementsByName('verification-token')[0].value = arguments[0];", solution)
driver.execute_script("document.getElementsByName('fc-token')[0].value = arguments[0];", solution)
driver.switch_to.default_content()
el = driver.find_element(By.NAME, "captchaUserResponseToken")
driver.execute_script(f"arguments[0].setAttribute('value', '{solution}');", el)
This inserts the solved CAPTCHA into the hidden input fields.
Step 10: Submit the Form and Close the Browser
After solving and inserting the CAPTCHA solution, we submit the form to complete the process. Once submitted, the browser can be closed.
driver.execute_script("document.getElementById('captcha-challenge').submit();")
driver.sleep(10) # Wait for the form to submit
driver.close()
driver.quit()
Troubleshooting
If you encounter any issues during the process, here are a few troubleshooting tips:
- Check Your API Key Ensure your 2Captcha API key is valid and correctly set up.
- Use a Proxy If you are facing issues related to rate limits or geographic restrictions, consider using a proxy for your requests.
- Captcha Configuration Changes Websites can change their CAPTCHA configurations. If the solution stops working, check for any updates on the page and adjust the code accordingly.
Conclusion
By following these steps, you can solve FunCaptcha challenges automatically using Python and the 2Captcha API. This approach can be applied to a wide range of websites that use FunCaptcha to protect their resources.
You can look at alternative solutions.
For further documentation and troubleshooting, visit the 2Captcha API documentation.