İçeriğe geç
DurumPanel

Kılavuzlar

hCaptcha görsel tanıma ile tıklama simülasyonu (Selenium)

Bu sayfa Çince sürümden makine çevirisiyle hazırlanmıştır ve hatalar içerebilir. Geçerli olan Çince sürüm sürümüdür.

Bu eğitim, Selenium 4 ile hCaptcha demo sayfasını açar ve 3×3 ızgara görsel sorusunu gerçek bir tarayıcıda çözer: soruyu ve 9 görseli okur, hangilerinin hedef olduğunu hCaptcha görsel tanıma (HCaptchaClassification) ile belirler, ardından tıklamaları simüle eder.

hCaptcha’da ayrıca “belirtilen konuma tıklama” ve “sürükleme” gibi soru türleri vardır; bunlar kare yerine koordinat döndürür. Çözüm için hCaptcha görsel tanıma sayfasına bakın. Kod yazmak istemiyorsanız tarayıcı eklentisini kullanabilirsiniz; eklenti bu soru türlerinin tümünü destekler.

pip install requests selenium
export YESCAPTCHA_CLIENT_KEY=your_client_key
  1. Onay kutusuna tıklayın. Onay kutusu, adresinde frame=checkbox bulunan hCaptcha iframe’inin içindedir; bu iframe’e geçip #checkbox öğesine tıklayın.
  2. Soru iframe’ine geçin. Soru, adresinde frame=challenge bulunan iframe’in içindedir.
  3. Soruyu okuyun. .prompt-text içindeki metin sorudur; olduğu gibi API’ye gönderin (hCaptcha arayüz dilini Basitleştirilmiş Çince veya İngilizce yapmanız önerilir). Sorunun yanında örnek görsel varsa, onu da anchors alanına ekleyin.
  4. Görselleri alın. Her karedeki .task-image .image öğesinin arka plan görseli bir küçük resimdir; bunları sırayla indirip Base64’e çevirin ve queries listesini oluşturun.
  5. Tanıyın. createTask çağrısını HCaptchaClassification türüyle yapın. Dönen objects değerleri karelerle birebir eşleşir; true olanlar hedeftir.
  6. Tıklayın ve gönderin. Hedef olan kareleri tıklayıp gönder düğmesine basın. Art arda birkaç tur gelebilir; 3. adımdan 6. adıma kadar tekrarlayın.
  7. Tamamlandı. Doğrulama geçildiğinde sayfadaki h-captcha-response alanında token bulunur; bunu form ile birlikte gönderin.
hcaptcha_click_selenium.py
"""Click through an hCaptcha 3x3 image challenge with Selenium 4 and HCaptchaClassification.
Flow: open the page -> click the checkbox -> read the question and the 9 images ->
ask YesCaptcha which images match -> click them -> submit. Repeat while new rounds appear.
Run: pip install requests selenium
export YESCAPTCHA_CLIENT_KEY=your_client_key
python hcaptcha_click_selenium.py
Each recognized round is charged (see the price list; rounds with no target found are free).
hCaptcha changes its page structure often; adjust the selectors below if a step stops working.
Click and drag questions return coordinates instead; see the HCaptchaClassification page.
"""
import base64
import os
import re
import time
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
CLIENT_KEY = os.environ["YESCAPTCHA_CLIENT_KEY"]
PAGE_URL = "https://accounts.hcaptcha.com/demo"
def classify(question, images_b64):
task = {"type": "HCaptchaClassification", "question": question, "queries": images_b64}
data = requests.post(
"https://api.yescaptcha.com/createTask",
json={"clientKey": CLIENT_KEY, "task": task},
timeout=60,
).json()
if data.get("errorId"):
raise RuntimeError(f"{data.get('errorCode')}: {data.get('errorDescription')}")
return data["solution"] # synchronous
def background_image_b64(element):
"""The tiles are <div>s with a CSS background image; download it and Base64-encode it."""
style = element.get_attribute("style") or ""
match = re.search(r'url\("?(.*?)"?\)', style)
if not match:
return None
return base64.b64encode(requests.get(match.group(1), timeout=30).content).decode()
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 15)
try:
driver.get(PAGE_URL)
# 1. Click the checkbox (inside the first hCaptcha iframe)
wait.until(EC.frame_to_be_available_and_switch_to_it(
(By.CSS_SELECTOR, 'iframe[src*="hcaptcha"][src*="frame=checkbox"]')))
wait.until(EC.element_to_be_clickable((By.ID, "checkbox"))).click()
driver.switch_to.default_content()
# 2. Solve rounds in the challenge iframe
wait.until(EC.frame_to_be_available_and_switch_to_it(
(By.CSS_SELECTOR, 'iframe[src*="hcaptcha"][src*="frame=challenge"]')))
for _ in range(5):
question = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".prompt-text"))).text.strip()
tiles = driver.find_elements(By.CSS_SELECTOR, ".task-image")
images = [background_image_b64(t.find_element(By.CSS_SELECTOR, ".image")) for t in tiles]
if not tiles or None in images:
print("not a 3x3 image challenge; see the docs for click/drag questions")
break
solution = classify(question, images)
for tile, hit in zip(tiles, solution.get("objects", [])):
if hit:
tile.click()
time.sleep(0.3)
driver.find_element(By.CSS_SELECTOR, ".button-submit").click()
time.sleep(3)
if not driver.find_elements(By.CSS_SELECTOR, ".task-image"):
break
driver.switch_to.default_content()
token = driver.find_element(By.NAME, "h-captcha-response").get_attribute("value")
print("token received" if token else "not passed")
finally:
driver.quit()
  • hCaptcha’nın sayfa yapısı sık değişir; koddaki seçiciler çalışmayı bırakırsa gerçek sayfaya göre düzenleyin.
  • Her tanıma turunda puan düşülür; hedef bulunamayan turlarda puan düşülmez. Tıklamadan sonra doğrulaması başarısız olan turlar 1 saat içinde bildirilebilir; bildirim sonrası puan iadesi için başvuru yapılır.
  • Yeni bir soruda ERROR_UNKNOWN_QUESTION dönerse sayfayı yenileyerek başka bir soru alabilir veya Destek ile iletişime geçebilirsiniz.