AI4CAP.COM
Back to Blog
TutorialAutomation

13 min read

Setting Up Selenium with Automatic CAPTCHA Solving

Learn how to seamlessly integrate AI-powered CAPTCHA solving into your Selenium automation workflows.

Prerequisites

  • Selenium WebDriver installed for your preferred language
  • ChromeDriver or GeckoDriver (for Firefox)
  • AI4CAP.COM API key (get one here)
  • Basic understanding of web automation

Installation

# Install required packages pip install selenium requests pillow # For async support (recommended) pip install aiohttp asyncio

Basic Integration

Here's how to create a Selenium automation that automatically solves CAPTCHAs:

import time import base64 import requests from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC class SeleniumCaptchaSolver: def __init__(self, api_key): self.api_key = api_key self.api_url = "https://api.ai4cap.com/v1" self.driver = webdriver.Chrome() def solve_captcha(self, captcha_element): """Solve a CAPTCHA element automatically""" # Take screenshot of CAPTCHA captcha_image = captcha_element.screenshot_as_base64 # Send to AI4CAP API response = requests.post( f"{self.api_url}/tasks", headers={"API-Key": self.api_key}, json={ "type": "ImageToTextTask", "body": captcha_image } ) task_id = response.json()["taskId"] # Poll for result while True: result = requests.get( f"{self.api_url}/tasks/{task_id}", headers={"API-Key": self.api_key} ).json() if result["status"] == "ready": return result["solution"]["text"] elif result["status"] == "failed": raise Exception(f"CAPTCHA solving failed: {result.get('error')}") time.sleep(2) def automate_with_captcha(self, url): """Example automation with CAPTCHA handling""" self.driver.get(url) try: # Wait for CAPTCHA to appear captcha_img = WebDriverWait(self.driver, 10).until( EC.presence_of_element_located((By.CLASS_NAME, "captcha-image")) ) # Solve the CAPTCHA solution = self.solve_captcha(captcha_img) # Enter solution captcha_input = self.driver.find_element(By.ID, "captcha-input") captcha_input.send_keys(solution) # Submit form submit_btn = self.driver.find_element(By.ID, "submit") submit_btn.click() print("CAPTCHA solved successfully!") except Exception as e: print(f"Error: {e}") finally: self.driver.quit() # Usage solver = SeleniumCaptchaSolver("YOUR_API_KEY") solver.automate_with_captcha("https://example.com/form")

Advanced Techniques

1. Handling reCAPTCHA v2

For Google reCAPTCHA v2, you need to handle the iframe and token injection:

def solve_recaptcha_v2(driver, site_key, page_url): """Solve reCAPTCHA v2 and inject token""" # Get token from API response = requests.post( f"{API_URL}/tasks", headers={"API-Key": API_KEY}, json={ "type": "RecaptchaV2TaskProxyless", "websiteURL": page_url, "websiteKey": site_key } ) task_id = response.json()["taskId"] token = wait_for_solution(task_id) # Inject token into page driver.execute_script(f''' document.getElementById('g-recaptcha-response').innerHTML = '{token}'; document.getElementById('g-recaptcha-response').style.display = 'none'; if (typeof ___grecaptcha_cfg !== 'undefined') {{ Object.entries(___grecaptcha_cfg.clients).forEach(([key, client]) => {{ if (client.callback) {{ client.callback('{token}'); }} }}); }} ''') return token

2. Handling Dynamic CAPTCHAs

For CAPTCHAs that load dynamically or change:

class DynamicCaptchaHandler: def __init__(self, driver, api_key): self.driver = driver self.api_key = api_key def wait_and_solve(self, captcha_selector, max_retries=3): """Handle dynamic CAPTCHAs with retries""" for attempt in range(max_retries): try: # Wait for CAPTCHA to be fully loaded WebDriverWait(self.driver, 20).until( lambda d: d.find_element(By.CSS_SELECTOR, captcha_selector) .get_attribute('complete') == 'true' ) captcha_element = self.driver.find_element( By.CSS_SELECTOR, captcha_selector ) # Check if CAPTCHA has changed current_src = captcha_element.get_attribute('src') if hasattr(self, 'last_src') and self.last_src == current_src: time.sleep(1) continue self.last_src = current_src # Solve CAPTCHA solution = self.solve_captcha(captcha_element) # Verify solution was accepted if self.verify_solution(solution): return solution except Exception as e: print(f"Attempt {attempt + 1} failed: {e}") raise Exception("Failed to solve dynamic CAPTCHA")

3. Parallel Processing

Speed up automation by solving multiple CAPTCHAs concurrently:

import asyncio from concurrent.futures import ThreadPoolExecutor class ParallelCaptchaSolver: def __init__(self, api_key, max_workers=5): self.api_key = api_key self.executor = ThreadPoolExecutor(max_workers=max_workers) async def solve_multiple_captchas(self, captcha_elements): """Solve multiple CAPTCHAs in parallel""" tasks = [] for element in captcha_elements: task = asyncio.create_task( self.solve_captcha_async(element) ) tasks.append(task) solutions = await asyncio.gather(*tasks) return solutions async def solve_captcha_async(self, element): """Async wrapper for CAPTCHA solving""" loop = asyncio.get_event_loop() return await loop.run_in_executor( self.executor, self.solve_captcha_sync, element )

Best Practices

  1. Implement Retry Logic

    CAPTCHAs can fail due to network issues or changes. Always implement retry mechanisms.

  2. Handle Different CAPTCHA Types

    Detect the CAPTCHA type and use appropriate solving methods.

  3. Use Explicit Waits

    Always wait for CAPTCHAs to fully load before attempting to solve them.

  4. Monitor Success Rates

    Track your solving success rates and adjust strategies accordingly.

  5. Respect Rate Limits

    Don't overwhelm target websites or the API with too many requests.


Common Issues and Solutions

Issue: CAPTCHA image not loading properly

# Solution: Wait for image to be fully loaded wait = WebDriverWait(driver, 20) captcha = wait.until( EC.presence_of_element_located((By.ID, "captcha-image")) ) # Additional check for image loading driver.execute_script( "return arguments[0].complete && " + "typeof arguments[0].naturalWidth != 'undefined' && " + "arguments[0].naturalWidth > 0", captcha )

Issue: Solution not being accepted

# Solution: Clear input and use JavaScript to set value input_element = driver.find_element(By.ID, "captcha-input") input_element.clear() # Use JavaScript for more reliable input driver.execute_script( "arguments[0].value = arguments[1]; " + "arguments[0].dispatchEvent(new Event('input', {bubbles: true}));", input_element, solution )

Ready to Automate?

Start integrating CAPTCHA solving into your Selenium projects today with our reliable API.