识别验证码 - 18570580798/study GitHub Wiki

安装Tesseract-OCR资源包 安装pytesseract 、 PIL库

#coding=utf-8 from PIL import Image import pytesseract from selenium import webdriver from robot.libraries.BuiltIn import BuiltIn

def Webdriver(): selib=BuiltIn().get_library_instance('Selenium2Library') return selib._current_browser()

def Readcode(): driver=Webdriver() driver.save_screenshot('aa.png') #截取当前网页,该网页有我们需要的验证码 imgelement = driver.find_element_by_class_name("validate-code-img") #定位验证码 location = imgelement.location #获取验证码x,y轴坐标 size=imgelement.size #获取验证码的长宽 rangle=(int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height'])) #写成我们需要截取的位置坐标 i=Image.open("aa.png") #打开截图 frame4=i.crop(rangle) #使用Image的crop函数,从截图中再次截取我们需要的区域 imgry = frame4.convert('L') imgry.save('frame4.png') qq=Image.open('frame4.png') code=pytesseract.image_to_string(qq) #使用image_to_string识别验证码 return code