PakistanDatabase.com

how to bypass text based captcha?

mango233

New member
Joined
Mar 5, 2025
Messages
5
Hellcoins
♆9
I'm trying to make a bot that automates filling a vacancy on a specific website and makes the appointment for me, but I can't get around its text captcha system. And I'm new to this and so maybe i need some help. it's not working properlly. i need to take a screenshot of the captcha and then solve it

this is the code that i wrote
def break_captcha(xpath_captcha):
try:
captcha_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, xpath_captcha))
)
location = captcha_element.location
size = captcha_element.size
screenshot = driver.get_screenshot_as_png()

img = Image.open(BytesIO(screenshot))
left, top = location['x'], location['y']
right, bottom = left + size['width'], top + size['height']

captcha_image = img.crop((left, top, right, bottom))
captcha_image.save("captcha.png")

image = cv2.imread("captcha.png")
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.copyMakeBorder(image, 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=[250])
image = cv2.filter2D(image, -1, np.ones((4, 4), np.float32) / 16)
se = cv2.getStructuringElement(cv2.MORPH_RECT, (8,8))
bg = cv2.morphologyEx(image, cv2.MORPH_DILATE, se)
image = cv2.divide(image, bg, scale=255)
image = cv2.filter2D(image, -1, np.ones((3, 4), np.float32) / 12)
image = cv2.threshold(image, 0, 255, cv2.THRESH_OTSU)[1]
image = cv2.copyMakeBorder(image, 5, 5, 5, 5, cv2.BORDER_CONSTANT, value=[250])

captcha_texto = pytesseract.image_to_string(image, config='--psm 13 -c tessedit_char_whitelist=ABCDEFGHIJKLMNPQRSTUVWYZ0123456789')
captcha_texto = re.sub('[\W_]+', '', captcha_texto).strip()

logging.info(f"captcha extrated: {captcha_texto}")
return captcha_texto
except TimeoutException:
logging.error("CAPTCHA não encontrado ou não carregou a tempo.")
return ""
except Exception as e:
logging.error(f"Erro ao processar CAPTCHA: {e}")
return ""


captcha.png
 
Top