Friends said that the company to test the environment to do interface testing, login need to pass the correct image of the verification Code, the principle of lazy and easy, recommend him to test the environment of the picture verification code to write dead , our company is doing so ^_^. Persuade no fruit/(ㄒoㄒ)/~~, only through the OCR technology to identify the picture verification code, looked at their verification code, long so, fortunately very easy to identify (background color is transparent, there is a pit to deal with).
Python implementation of the image verification Code login demo, the use of the third-party module has requests, PIL, Pytesseract.
1 #Coding:utf-82 ImportRequests3 fromPILImportImage4 fromPytesseractImportimage_to_string, Pytesseract5 6Pytesseract.tesseract_cmd ='d:\\env\\tesseract-ocr\\tesseract'7 #URL8Base_url ='https://hostxxx'9Code_url = Base_url +'/common-platform/code'TenDo_login_url = Base_url +'/common-platform/dologin' One A - #Blank background color pytesseract not recognized, changed the background color - defChange_background (IMG_FP): the Try: -IMG =Image.open (IMG_FP) -X, y =img.size -New_img = Image.new ('RGBA', Img.size, (255, 255, 255)) + New_img.paste (IMG, (0, 0, x, y), IMG) - returnnew_img + except: A PrintU'failed to change picture background' at - - #Identify the image verification code - defocr2str (IMG): - returnstr (image_to_string (IMG)) - in - #Create session toSession =requests. Session () + #request Picture Verification Code Interface -Code_resp = Session.request (method='GET', Url=code_url, verify=False) the #Save picture Verification Code *With open ('Code.png','WB') as F: $ f.write (code_resp.content)Panax Notoginseng #Verification Code -Code = OCR2STR (Change_background ('Code.png')) the #Login Data +Do_login_data = { A 'UserName':'User', the 'Password':'pwd', + 'Verificationcode': Code - } $ #Login Interface $Do_login_resp = Session.request (method='POST', Url=do_login_url, Data=do_login_data, verify=False) - PrintDo_login_resp.text#Verify that the login is successful
Python implementation simple image Verification code login