Verification code recognition for python selenium automated login

1. Identify by locating elements and intercepting pictures

#ocr identification principle: first locate the verification code picture according to the class dl_yzm of the verification code, then save the screenshot of the verification code, then use ocr to identify, read out the verification code, and fill it into yzm_text (not available with IE browser)

driver.find_element(By.CLASS_NAME, "dl_yzm").click()

yzmImage=driver.find_element(By.XPATH,"/html/body/div[@class='login_bg']/div[@class='login_main']/div[@class='login_box']/p[4] /span[@class='dl_yzm']/img/@src") #Verification code image

yzmImage.screenshot("yzm.png") #Verification code screenshot, save as yzm.png

ocr=ddddocr.DdddOcr()(show_ad=False)

with open("yzm.png","rb") as fp:

image=fp.read()

catch=ocr.classification(image) #Verification code returned to catch

print(catch)

driver.find_element(By.ID,"yzm_text").send_keys(catch) #Fill the verification code recognized by ocr into yzm_text

2.#Another way to capture and identify the verification code image by means of coordinates

#Intercept current page

driver.save_screenshot('web_screen.png')

page_snap_obj=Image.open('web_screen.png')

#Position verification code

yzm=driver.find_element(By.CLASS_NAME,"dl_yzm")

location=yzm.location

size=yzm.size

left=location['x']

top=location['y']

right=left+size['width']

bottom=top+size['height']

yzm_obj=page_snap_obj.crop((left,top,right,bottom))

#Save the captured verification code picture and identify the content of the verification code

yzm_obj.save('yzm.png')

ocr=ddddocr.DdddOcr(show_ad=False)

with open('yzm.png','rb') as fp:

img=fp.read()

catch=ocr.classification(img)

driver.find_element(By.ID, "yzm_text").send_keys(catch) # Fill in the verification code recognized by ocr to yzm_text

Guess you like

Origin blog.csdn.net/u012388338/article/details/129621286