How to Bypass Captchas with Python Selenium and OCR – A Step‑by‑Step Guide

This article walks through solving Python web‑scraping captcha challenges by using Selenium to capture the image, applying OCR for recognition, and offering alternative request‑based methods, while also addressing common driver version mismatches.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Bypass Captchas with Python Selenium and OCR – A Step‑by‑Step Guide

1. Introduction

Hello, I am PiPi. Recently a fan asked about a Python web‑scraping captcha problem in a group, so I’m sharing the solution here.

Below is the original code:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from PIL import Image
import ddddocr

ocr = ddddocr.DdddOcr()

options = webdriver.ChromeOptions()
options.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36')
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(options=options)

# Open target page
driver.get('https://sol.sinosure.com.cn')
time.sleep(5)
driver.maximize_window()
# Locate captcha element and hover to load image
yanzhengma = driver.find_element(By.CSS_SELECTOR, '.pass-form-item.pass-form-item-code')
captcha_element = yanzhengma.find_element(By.CSS_SELECTOR, '.pass-label-img')
webdriver.ActionChains(driver).move_to_element(captcha_element).perform()
time.sleep(5)

# Get location and size of captcha element
location = captcha_element.location
size = captcha_element.size
print(location)
print(size)
# Capture full page screenshot
driver.save_screenshot('screenshot.png')

# Crop captcha from screenshot
left = int(location['x'])
top = int(location['y'])
right = int(location['x'] + size['width'])
bottom = int(location['y'] + size['height'])
captcha_screenshot = Image.open('screenshot.png').crop((left, top, right, bottom))
print(left)
print(top)
print(location)
print(bottom)
# Save cropped captcha and recognize
captcha_screenshot.save('captcha.png')
with open('captcha.png', 'rb') as f:
    img_bytes = f.read()
res = ocr.classification(img_bytes)
print('识别的验证码是:' + res)

The basic idea works, but the cropped area is slightly off, causing the OCR to miss the captcha.

Another snippet showing how to obtain the element's location and size:

location = captcha_element.location<br/>size = captcha_element.size

Some users found that the returned coordinates were near the password field rather than the captcha, even after trying to locate a larger container first.

2. Implementation

One contributor tried the code but encountered a driver‑version mismatch error, which is common when the local ChromeDriver does not match the installed Chrome browser.

The fix is to download the ChromeDriver version that matches your Chrome browser and ensure its folder is added to the system PATH.

Another approach suggested by a fan is to fetch the captcha image directly via its URL using requests, then pass the binary content to the OCR without saving a file:

This method avoids the cropping inaccuracies and works well for those less familiar with Selenium.

3. Conclusion

This article presented a Python web‑scraping solution for captchas, demonstrating both a Selenium‑based screenshot‑crop method and a direct request‑based approach, helping readers overcome common driver issues and recognition errors.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonOCRCaptchaweb-scraping
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.