selenium處理驗證碼模塊的安裝和使用

PIL模塊的安裝html

pytesser 模塊的安裝:python

下載後獲得 「pytesser_v0.0.1.zip」,是一個壓縮文件,使用方法: web

一、在 「D:\For_Software\others\Python_PyDev\Python\Lib\site-packages」 路徑下新建一個文件夾,命名 「pytesser」 。把 「pytesser_v0.0.1.zip」 裏的文件解壓到該目錄:函數

二、將 「pytesser.py」 更名爲 「__init__.py」。ui

三、打開 「__init__.py」 文件,將 「tesseract_exe_name」 變量的值改成 「‘D:/For_Software/others/Python_PyDev/python/Lib/site-packages/pytesser/tesseract’」(原值爲 「‘tesseract’」)。url

四、pytesser 模塊依賴於 PIL 模塊,若是是按照上面的方法安裝 PIL 的話,須要把 「init.py」 文件裏的 「import Image」 改爲 「from PIL import Image」 。 spa

 

 

http://blog.csdn.net/mrlevo520/article/details/51901579.net

http://www.cnblogs.com/wolaiye320/p/5649462.htmlcode

 

from selenium import webdriverhtm

from selenium.webdriver.support.ui import Select

from selenium.common.exceptions import NoSuchElementException

from selenium.common.exceptions import NoAlertPresentException

from selenium.webdriver.common.action_chains import ActionChains

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.alert import Alert

import unittest, time, re

from PIL import ImageGrab

from PIL import Image,ImageEnhance

from pytesser import *

import pytesseract

import os

 

class Cjlr(unittest.TestCase):

def setUp(self):

self.driver = webdriver.Firefox()

self.driver.implicitly_wait(10)

self.base_url = "http://182.92.131.35:8088/"

 

def test_cjlr(self):

driver = self.driver

driver.get(self.base_url + "/")

driver.find_element_by_xpath(".//*[@id='mini-3']/span").click()

time.sleep(5)

#獲取驗證碼

driver.save_screenshot('E:\\OCR\\aa.png') # 截取當前網頁,該網頁有咱們須要的驗證碼

imgelement = driver.find_element_by_id('img_code') # 定位驗證碼

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("E:\\OCR\\aa.png") # 打開截圖

frame4 = i.crop(rangle) # 使用Image的crop函數,從截圖中再次截取咱們須要的區域

frame4.save('E:\\OCR\\ac.png')

qq = Image.open('E:\\OCR\\ac.png')

imgry = qq.convert('L') # 圖像增強,二值化

sharpness = ImageEnhance.Contrast(imgry) # 對比度加強

sharp_img = sharpness.enhance(2.0)

sharp_img.save("E:\\OCR\\ac.png")

code = image_file_to_string("E:\\OCR\\ac.png") # code即爲識別出的圖片數字str類型

print code

 

#登陸

driver.find_element_by_id('tel_num').send_keys('18336091139')

driver.find_element_by_id('pwd').send_keys('123456')

time.sleep(10)

driver.find_element_by_id('randCode').send_keys(code)

driver.find_element_by_id("teacher_login").click()

相關文章
相關標籤/搜索