Convert Captcha To String In Python Selenium

Python Selenium

Convert Captcha To String In Python Selenium – In This Post, Today I Will Show How To Convert Captcha in String And Number |

Also Read – WordPress Automation

Hamne Dekha Ki, Login And Form Etc.. Fill Karte Time Captcha Jyada Tar Website Me Hota Hai, Or Ye Captcha Ki Problem Kuch Jyada Hi Dikkat Paida Karti Hai, Isliye Aaj Ki Is Post Me Is Problem Ko Solve Karne Wale Hai |

Convert Captcha To String In Python

Maine Isme Python Selenium library Ka Istemal Kiya Hai, Agar Aapko Python Selenium Library In Hindi Me Learn Karni Hai, To Aap Niche Diye Link Kar Click Karke Python Selenium Tutorial Ko Easily Learn Kar Sate Hai

Also Read – Python Selenium Library

Me Aapko Step By Step Batata Hu And Fir Bad Me Ek Jagah Likhe Ke Bad Code Ko Run Karta Hai|

Step 0:

Aapko Captcha Ko String Me Convert Karne Se Pahle Aapko Python Ko Install Karna Padega, Joki Python Setup Karne Ke Liye Niche Link Par Click Karke Install Kar Sakte Ho.

Click Here – Installing Python, Selenium

Step 1:

Sabse Pahle Aapko Pytesseract.exe And Fir Tesseract Library Ko Install Karna Padega, Joki Niche Install Karne Ke Liye Bataya Gaya Hai |

Click Here – Download Tesseract

Aapko Upper Diye Link Par Click Karke Download Kar Lena Hai And Fir Ise, Install Karna Dena Hai |

pip install pytesseract

pip install pillow

Ab Aapko Terminal Me Upper Di Gayi cmd Ko Run Kar Dena Hai, Jisse Aapke System Me pytesseract And pillow Install Ho Jayega |

Step 2:

Aapko Librabry Ko Import Karna Hai, And Uske Bad Aapko tesseract.exe File Ki Location Bhi Set Karni Hai Wo Bhi Aapne System ke Anushar |

from PIL import Image
from selenium import webdriver
from PIL import Image, ImageFilter
import pytesseract 
pytesseract.pytesseract.tesseract_cmd = r"C:\\Program Files\\Tesseract-OCR\\tesseract.exe"

Step 3:

Yaha Par Chrome Ka Object (driver) Create Karke Website Url Put Karna Hai Or Fir get() Function Ki Help Se Url Ko Get Karke URL Open Ho Jayega And Uske Bad Fir URL Main Me Frame Switch Ho Jayega |

driver = webdriver.Chrome(executable_path="E:\\chromedriver_win32\\chromedriver.exe")
driver.get("http://sistemas.cvm.gov.br/?fundosreg")

# change frame
driver.switch_to.frame("Main")

Step 4:

Ab Hame get_captcha() Function Ko Create Karna Hai, Or Ye Function Is sistemas.cvm.gov.br/?fundosreg Website Ke Hisab Se Ready Kiya Gaya Hai, Matlab Ki Is Website Me Captcha Left + Bottom Me Show Hota Hai, To Ye Functions Captcha Location Me Jayega Or Uska Screen Show Leke And Fir Use String Me Convert Kar Dega |

def get_captcha(driver, element, path):
    # now that we have the preliminary stuff out of the way time to get that image :D
    location = element.location
    size = element.size
    # saves screenshot of entire page
    driver.save_screenshot(path)

    # uses PIL library to open image in memory
    image = Image.open(path)

    left = location['x']
    top = location['y'] + 140
    right = location['x'] + size['width']
    bottom = location['y'] + size['height'] + 140

    image = image.crop((left, top, right, bottom))  # defines crop points
    image.save(path, 'png')  # saves new cropped image

    captcha = pytesseract.image_to_string(image) 
    captcha = captcha.replace(" ", "").strip()
    print(captcha)

Step 5:

Ab Captcha Image Ke Xpath Ko Leke img Variable Me Save Karke Ab get_captcha() Function Ko Call Kar Diya Hai,

# download image/captcha
img = driver.find_element_by_xpath(".//*[@id='trRandom3']/td[2]/img")
get_captcha(driver, img, "captcha.png")

Step Final:

Yaha Par Ab Sara Code Ek Editor me Rakha Gaya Hai, Aap Ise Ab Istemal kar Sakte Hai|

from PIL import Image
from selenium import webdriver
from PIL import Image, ImageFilter
import pytesseract 
pytesseract.pytesseract.tesseract_cmd = r"C:\\Program Files\\Tesseract-OCR\\tesseract.exe"


driver = webdriver.Chrome(executable_path="E:\\chromedriver_win32\\chromedriver.exe")
driver.get("http://sistemas.cvm.gov.br/?fundosreg")

# change frame
driver.switch_to.frame("Main")

def get_captcha(driver, element, path):
    # now that we have the preliminary stuff out of the way time to get that image :D
    location = element.location
    size = element.size
    # saves screenshot of entire page
    driver.save_screenshot(path)

    # uses PIL library to open image in memory
    image = Image.open(path)

    left = location['x']
    top = location['y'] + 140
    right = location['x'] + size['width']
    bottom = location['y'] + size['height'] + 140

    image = image.crop((left, top, right, bottom))  # defines crop points
    image.save(path, 'png')  # saves new cropped image

    captcha = pytesseract.image_to_string(image) 
    captcha = captcha.replace(" ", "").strip()
    print(captcha)




# download image/captcha
img = driver.find_element_by_xpath(".//*[@id='trRandom3']/td[2]/img")
get_captcha(driver, img, "captcha.png")

Output:

Dhiyan Rahe Ki, Ye kabhi Kabhi Output Sahi Bhi Nhi Deta Hai, Kyoki Captcha Kabhi Kabhi Hard Way Me Aa Jata Hai |

Leave a Reply

Your email address will not be published. Required fields are marked *