python selenium UI Four ways to automate captcha

  • 2020-06-23 00:54:09
  • OfStack

This paper introduces four methods of python selenium UI to solve captCHA automatically, and shares them with you, as follows:

The test environment

windows7+ firefox50+ geckodriver # firefox browser driver python3 selenium3

There are four methods for selenium UI to solve captCHA automatically: remove captcha, set universal code, add CAPtCHA recognition technology-ES17en, and add cookie login. This paper mainly explains captCHA recognition technology-ES19en and add cookie login.

1. Remove captcha

Remove the captcha and log into the site directly with your username and password.

2. Set universal codes

Set universal code, is no matter what the situation, input universal code, you can successfully log into the website.

3. Captcha recognition technology -tesseract

Preparation conditions

tesseract, download address: https: / / github com/parrot - office tesseract/releases/tag / 3.5.1 track of Python3 x, download address: https: / / www python. org/downloads / pillow (Python3 Image Processing Library)

Install Python and install the pillow library via pip install pillow. Then put tesseract. exe and testdata folders in tesseract into the directory where the test script is located. testdata defaults to ES65en. traineddata and osd. traineddata.

Following are two main files, TesseractPy3.ES72en calls tesseract via the python code to identify the captcha. code.py obtains the captcha image through selenium, and then USES the function in TesseractPy3 to get the captcha, realizing the automatic login of the website.

TesseractPy3.py


#coding=utf-8

import os
import subprocess
import traceback
import logging

from PIL import Image #  Is derived from the Pillow library 

TESSERACT = 'tesseract' #  The name of the local command invoked 
TEMP_IMAGE_NAME = "temp.bmp" #  Converted temporary file 
TEMP_RESULT_NAME = "temp" #  Save a temporary file that identifies characters 
CLEANUP_TEMP_FLAG = True #  Clean temporary file identification 
INCOMPATIBLE = True #  Compatibility identification 

def image_to_scratch(image, TEMP_IMAGE_NAME):
  #  Process images into compatible formats 
  image.save(TEMP_IMAGE_NAME, dpi=(200,200))

def retrieve_text(TEMP_RESULT_NAME):
  #  Read the recognized content 
  inf = open(TEMP_RESULT_NAME + '.txt','r')
  text = inf.read()
  inf.close()
  return text

def perform_cleanup(TEMP_IMAGE_NAME, TEMP_RESULT_NAME):
  #  Clean up temporary files 
  for name in (TEMP_IMAGE_NAME, TEMP_RESULT_NAME + '.txt', "tesseract.log"):
    try:
      os.remove(name)
    except OSError:
      pass

def call_tesseract(image, result, lang):
  #  call tesseract.exe , write the result of reading output_filename In the 
  args = [TESSERACT, image, result, '-l', lang]
  proc = subprocess.Popen(args)
  retcode = proc.communicate()

def image_to_string(image, lang, cleanup = CLEANUP_TEMP_FLAG, incompatible = INCOMPATIBLE):
  #  Suppose the image is in an incompatible format and incompatible = True , first convert the picture to compatible format (this procedure will convert the picture to .bmp Format), and then get the reading result ; if cleanup=True, Delete the temporary file after the operation. 
  logging.basicConfig(filename='tesseract.log')
  try:
    try:
      call_tesseract(image, TEMP_RESULT_NAME, lang)
      text = retrieve_text(TEMP_RESULT_NAME)
    except Exception:
      if incompatible:
        image = Image.open(image)
        image_to_scratch(image, TEMP_IMAGE_NAME)
        call_tesseract(TEMP_IMAGE_NAME, TEMP_RESULT_NAME, lang)
        text = retrieve_text(TEMP_RESULT_NAME)
      else:
        raise
    return text
  except: 
    s=traceback.format_exc()
    logging.error(s)
  finally:
    if cleanup:
      perform_cleanup(TEMP_IMAGE_NAME, TEMP_RESULT_NAME)

code.py


#coding=utf-8

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from PIL import Image
import unittest, time, re
from TesseractPy3 import *

class lgoin(unittest.TestCase):
  def setUp(self):
    self.driver = webdriver.Ie()
    self.driver.implicitly_wait(30)
    self.base_url = 'http://127.0.0.1:8080/test' #  Links to test 
    self.title = ' A management platform ' #  Test website Title
    self.verificationErrors = []
    self.accept_next_alert = True

  def test_lgoin(self):
    driver = self.driver
    driver.get(self.base_url)
    driver.maximize_window()
    driver.save_screenshot('All.png') #  Intercept the current web page that has the captcha we need 
    imgelement = driver.find_element_by_class_name('kaptchaImage')
    location = imgelement.location #  Get verification code x,y Axis coordinates 
    size = imgelement.size #  Gets the length and width of the captcha 
    rangle = (int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height'])) #  Write the coordinates we need to intercept 
    i = Image.open("All.png") #  Open the screenshot 
    result = i.crop(rangle) #  use Image the crop Function to capture the area we want from the screenshot again 
    result.save('result.jpg')
    text = image_to_string('result.jpg', 'eng').strip()

    assert self.title in driver.title

    driver.find_element_by_id(u'userCode').clear()
    driver.find_element_by_id(u'userCode').send_keys('XXXXXX') #  The user name 
    driver.find_element_by_id(u'password').clear()
    driver.find_element_by_id(u'password').send_keys('XXXXXX') #  password 
    #driver.find_element_by_name('verifyCode').clear()
    driver.find_element_by_name('verifyCode').send_keys(text)
    driver.find_element_by_name('submit').submit()


  def is_element_present(self, how, what):
    try: self.driver.find_element(by=how, value=what)
    except NoSuchElementException as e: return False
    return True

  def is_alert_present(self):
    try: self.driver.switch_to_alert()
    except NoAlertPresentException as e: return False
    return True

  def close_alert_and_get_its_text(self):
    try:
      alert = self.driver.switch_to_alert()
      alert_text = alert.text
      if self.accept_next_alert:
         alert.accept()
      else:
        alert.dismiss()
      return alert_text
    finally: self.accept_next_alert = True

  def tearDown(self):
    #self.driver.quit()
    self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
  unittest.main()

Finally, execute the command python code.py to successfully log into the website automatically.

Note:

Due to the quality and clarity of capTCHA images, not every time can be successfully logged in.

4. Add cookie login

First, get the cookie after the site login, and then add cookie to realize the purpose of the site login. We use cook to represent cookie after the login of xxxxxx.


# coding=utf-8

from selenium import webdriver
import time 

driver = webdriver.Firefox()
driver.get("http://www.xxxxxx.com/") #  The website to visit 

driver.add_cookie(cook) #  Here to add cookie Sometimes, cookie There may be multiple entries that need to be added multiple times 
time.sleep(3) 

#  Refresh the page and you will see that you have logged in successfully 
driver.refresh()

Note:

When logging in, please check the box for the next automatic login. When the browser prompts whether to save the user password, please select "OK". In this way, the cookie obtained has a higher probability of successful login


Related articles: