python + selenium 之登录流程的图形验证码处理

# coding=utf-8

from selenium import webdriver

from PIL import Image

import time

#创建浏览器对象

driver = webdriver.Chrome()

driver.maximize_window()

#打开登录页

url ='https://test.cn***un.com/f***ou/Seller'

driver.get(url)

time.sleep(3)

#输入用户名、密码

driver.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[1]/input").send_keys("15271080***")

driver.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[2]/input").send_keys("qq1******")

"""

识别验证码

"""

driver.save_screenshot('login.png')

#获取验证码元素的位置

image_code_site = driver.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[4]/img")

#获取验证码的坐标

loc = image_code_site.location

#获取验证码的宽高

size = image_code_site.size

print("坐标:" ,loc)

print("宽高: ",size)

#获取验证码的位置

left = loc['x']

top = loc['y']

bottom = top + size['height']

right = left +size['width']

page_pic = Image.open('login.png')

image_code_site = page_pic.crop((left,top,right, bottom))

image_code_site.save('yzm.png')

#调用超级鹰第三方接口识别验证码

from chaojiying.Chaojiyingimport *

#读取文件内容

with open ('yzm.png','rb')as f :

pic1 = f.read()

#调用第三方验证码识别平台接口识别验证码

yz = Chaojiying_Client(username ='jack***2',password ='w****ia',soft_id ='1902')

res = yz.PostPic(pic1,codetype ='1902').get('pic_str')

print('识别结果 : ',res)

#输入验证码

driver.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[3]/input").send_keys(res)

#driver.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[3]/input").send_keys('1111')

time.sleep(2)

#点击登录

driver.find_element_by_xpath("/html/body/div[1]/div[2]/form/div[5]/input").click()

time.sleep(2)

driver.quit()

推荐阅读更多精彩内容