[Introduction]
There is verification code login screen is a very troublesome problem, its solution is:
1, the development of universal set or remove the authentication code
2, identified by tesseract-ocr
3, a third-party interface identification codes
[] Implementation steps
1, taken login screen, the positioning element codes, codes section taken image, obtaining a text theme identification codes; herein theme codes if there is an error, the reason for the scale display of the windows is provided a computer system caused, location acquisition coordinates are obtained by the display coordinate 100%, while the coordinate theme used is determined according to the required scale display corresponding to the scaled image, so there have been variations.
There are three ways to solve this problem:
Modify computer display set to 100%. This is the easiest way;
scaling intercepted page image, coming screenshot size scaled to the size of the width and height divided by scaling;
Image.crop parameter modification of the four values are multiplied by the parameter tuple scale.
2, verification code text acquisition fails, then re-acquired
3, authentication code to succeed, then log
The following describes by tesseract-ocr simple identification codes:
#-*- coding: utf-8 -*-
# python 3.5.0
import re
import requests
import time
from selenium import webdriver
from PIL import Image,ImageEnhance
import pytesseract
fileIndex = 0
get_auth_code DEF (Driver, CodeElement):
'' 'Get codes' ''
Global FileIndex
fileName = 'D: / Login' + STR (FileIndex) + '.png'
# login screen shots
driver.save_screenshot (fileName)
FileIndex. 1 + =
imgSize = codeElement.size # codes acquired image size
imgLocation = codeElement.location # coordinate acquisition element codes
rangle = (int (imgLocation [ ' x']), int (imgLocation [ 'y']), int (imgLocation [ imgSize [ 'width']), int (imgLocation [ 'y'] + imgSize [ 'height'])) # global coordinate calculation codes 'X'] +
Login = Image.open (fileName)
frame4 login.crop = ( rangle) # codes pictures taken
realFileName = 'D: / AUTHCODE' + STR (FileIndex) + '.png'
frame4.save (realFileName) # codes saved screenshot
authcodeImg = Image.open (realFileName)
authCodeText = pytesseract.image_to_string (authcodeImg) .strip ( ) # identification codes
return authCodeText # Returns the text codes
def login(driver,authcode):
'''登录系统'''
driver.find_element_by_xpath('//div/ul/li[@class="user"]/input').send_keys("XXXXXXX")
driver.find_element_by_xpath('//div/ul/li[@class="password"]/input').send_keys("123456")
driver.find_element_by_xpath('//div/ul/li[@class="yzm"]/input').send_keys(authcode)
driver.find_element_by_xpath('//div/ul/li[@class="jump"]/button').click()
time.sleep(3)
def isElementExist(element):
flag=True
try:
element=driver.find_element_by_xpath(element)
return flag
except:
flag=False
return flag
def get_yzm(driver):
driver.get ( 'HTTP: //XXXXXXXXXXXXXXXXX/login.html')
driver.maximize_window ()
the time.sleep (. 5)
imgElement = driver.find_element_by_xpath ( '// div / UL / Li [@ class = "YZM"] / img ') # positioning codes position
yzm_text = get_auth_code (driver, imgElement) # acquired identification codes text
return yzm_text
DEF login_main (Driver):
yzm_text = get_yzm (Driver) # Get codes text
while len (yzm_text) = 4! : Analyzing codes # 4 is equal to, not equal to reacquire codes
yzm_text = get_yzm (Driver)
Login (Driver, yzm_text) # log verification code identification is four characters
time.sleep (2)
exit_flag = isElementExist ( '// div / p [@ class = "password-tip"]') # If the correct PIN will pop div elastic block, block acknowledgment elastic element exists
IF exit_flag == True:
login_flag = True
. driver.find_element_by_xpath ( '// div / the p-/ span [@ class = "Edit-OFF"]') the click () # if there is div bomb box, click on the cancel button to go to Home
Print (login_flag)
the else:
login_flag = False
. driver.switch_to_alert () accept () # If you enter the verification code is incorrect, an error code pop bomb alert box
return login_flag
DEF main (Driver):
login_flag = None
the while login_flag = True:!
login_flag = login_main (Driver)
Print ( "main:" + str (login_flag )
if __name__ == '__main__':
driver = webdriver.Chrome()
main(driver)