DEV Community

shadowb
shadowb

Posted on

How To Bypass The Capthca Using Selenium Python.

Hi guys we are bypassing the captcha by selenium web driver to log in particular webpage.
(Please note that this article does not condone or support any form of security bypass or unethical use of technology).

source imgflip

Prerequisites:

  1. OpenCV (cv2)
  2. Tesseract OCR
  3. Pillow (PIL)
  4. Selenium

Now lets code our requirment.

1. Getting the log in page

 Import the required modules as I shown below.

from selenium import webdriver
import cv2
from PIL import Image
from pytesseract import pytesseract


driver = webdriver()
driver.maximize_window()

# Now we will hit the website just pass your website link below.

driver.get(" Your Website Link Here")

Enter fullscreen mode Exit fullscreen mode

2 . Capturing the screenshot of log in page and cropping it.

# Taking screenshot of login page and saving it as image.png
driver.save_screenshot("image.png")
Enter fullscreen mode Exit fullscreen mode

3 . To focus on the area of interest menase crop the exact image which holds only captcha block.

This is sample cropped image.

Image description

img = cv2.imread('image.png')
# you guys have  define coordinates as per your page layout.
cropped_image = img[200:250, 60:200]  
cv2.imwrite("Cropped_Image.jpeg", cropped_image)

Enter fullscreen mode Exit fullscreen mode

3. Performing OCR - Extracting text from cropped image.

To extract text from the cropped image, we use Tesseract OCR. Make sure you have Tesseract installed and properly configured. We set the path to the Tesseract executable and the path to our cropped image.

# Define path to tesseract.exe
path_to_tesseract = "Your path to tesseract.exe"

# Define path to the cropped image
path_to_image = "Your path to cropped image"

# Point pytesseract to the Tesseract executable
pytesseract.tesseract_cmd = path_to_tesseract
img = Image.open(path_to_image)

# Extract text from image
text = pytesseract.image_to_string(img)

Enter fullscreen mode Exit fullscreen mode

Now take that data in text variable and using send keys send it to captcha labelbox.

enetr_captcha = driver.find_element("element path to location").send_keys(text)
Enter fullscreen mode Exit fullscreen mode

Please remember to use this knowledge responsibly and ethically. Accessibility is about making the digital world more inclusive, not compromising security.

Conclusion :

In this blog post, we've explored how to use Python, OpenCV, and Tesseract OCR to extract text from images. While this technique has legitimate uses, it's essential to use such knowledge for the right purposes, such as improving web accessibility.

I hope this would be helpful if yes like and comments Yeehhhh!

Top comments (0)