Python CAPTCHA Automation Tutorial with Selenium

Learn how to build a complete CAPTCHA automation workflow using Python, Selenium, and CaptchaKings API. This tutorial covers reCAPTCHA v2, v3, and hCaptcha.

Prerequisites

Install the required packages:

pip install selenium requests webdriver-manager

Complete Python Script

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager import requests import time class CaptchaSolver: def __init__(self, api_key): self.api_key = api_key self.api_url = "https://api.captchakings.com" def solve_recaptcha_v2(self, site_key, page_url): # Create task task_data = { "clientKey": self.api_key, "task": { "type": "RecaptchaV2TaskProxyless", "websiteURL": page_url, "websiteKey": site_key } } response = requests.post(f"{self.api_url}/createTask", json=task_data) task_id = response.json().get("taskId") # Wait for result for _ in range(60): result = requests.post(f"{self.api_url}/getTaskResult", json={ "clientKey": self.api_key, "taskId": task_id }) data = result.json() if data.get("status") == "ready": return data["solution"]["gRecaptchaResponse"] time.sleep(2) return None # Usage with Selenium solver = CaptchaSolver("YOUR_API_KEY") driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) driver.get("https://example.com/login") # Get sitekey from page sitekey = driver.find_element(By.CLASS_NAME, "g-recaptcha").get_attribute("data-sitekey") # Solve captcha token = solver.solve_recaptcha_v2(sitekey, driver.current_url) # Inject token driver.execute_script(f'document.getElementById("g-recaptcha-response").innerHTML = "{token}"') # Submit form driver.find_element(By.ID, "submit").click()

Key Features

  • Automatic sitekey extraction
  • Polling mechanism for async solving
  • Token injection into hidden textarea
  • Works with any reCAPTCHA v2 protected site

🐍 Get Your Python API Key

$0.50 free balance to test your automation

Start Free