connie_tong

导航

 
from selenium import webdriver
import time
from PIL import Image
import pytesseract
import re


dr = webdriver.Chrome()
dr.get('http://172.16.1.9/IPINwebDEV/login.html')

dr.maximize_window()

n = dr.find_elements_by_class_name('login_type_li')[2]
n.click()

name = dr.find_element_by_xpath('/html/body/div[3]/div/div/div[2]/input[1]')
name.send_keys('wiwi')

password = dr.find_element_by_xpath('/html/body/div[3]/div/div/div[2]/input[2]')
password.send_keys('Aa123456')

# 整個頁面截圖
dr.save_screenshot('.\\printscreen.png')

# 獲取驗證碼的位置坐標
imgelement = dr.find_element_by_xpath('/html/body/div[3]/div/div/div[2]/img')
location = imgelement.location
size = imgelement.size
rangle = (
int(location['x']), int(location['y']), int(location['x'] + size['width']), int(location['y'] + size['height']))
# 打開截圖
i = Image.open(".\\printscreen.png")
# 根據坐標裁剪圖片
frame4 = i.crop(rangle)
# 保存為圖片
frame4.save('.\\save.tif')
# 转化为灰度
imgry = frame4.convert('L')
# 保存下來
imgry.save(".\\imgry1.tif")
# 獲取圖片驗證碼的內容
text = pytesseract.image_to_string(Image.open(r".\\imgry1.tif"))
# 打印出來查看,發現內容有特殊字符空格
print(text)

# 去除特殊字符,剩下數字英文中文,導入re包
sub_str = re.sub(u"([^\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a])", "", text)

# 輸入驗證碼
c = dr.find_element_by_xpath('/html/body/div[3]/div/div/div[2]/input[3]')
c.send_keys(sub_str)

time.sleep(2)

# 點擊登錄按鈕
login = dr.find_element_by_id('login_btn')
login.click()

 

posted on 2021-03-02 16:42  connie_tong  阅读(457)  评论(0编辑  收藏  举报