51Testing软件测试论坛
标题:
python+selenium 测试百度登陆时,验证码无法破解
[打印本页]
作者:
cq520131517
时间:
2017-4-5 14:14
标题:
python+selenium 测试百度登陆时,验证码无法破解
# coding:utf-8
from PIL import Image
import pytesseract
from PIL import ImageEnhance
from selenium import webdriver
from time import sleep
url='http://www.baidu.com'
driver = webdriver.Chrome()
driver.maximize_window() #将浏览器最大化
driver.get(url)
driver.find_element_by_xpath('//*[@id="u1"]/a[7]').click()
sleep(2)
driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_8__userName"]').click()
driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_8__userName"]').send_keys('xxxxxxx')
sleep(2)
#输入登录密码
driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_8__password"]').click()
driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_8__password"]').send_keys('xxxxxxx')
sleep(2)
driver.save_screenshot('E://123.png') #截取当前网页,该网页有我们需要的验证码
imgelement = driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_8__verifyCodeImg"]') #定位验证码
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://123.png") #打开截图
frame4=i.crop(rangle) #使用Image的crop函数,从截图中再次截取我们需要的区域
frame4.save('E://frame4.jpg')
qq=Image.open('E://frame4.jpg')
#enhancer = ImageEnhance.Color(qq)
#enhancer = enhancer.enhance(0)
#enhancer = ImageEnhance.Brightness(enhancer)
#enhancer = enhancer.enhance(2)
#enhancer = ImageEnhance.Contrast(enhancer)
#enhancer = enhancer.enhance(8)
#enhancer = ImageEnhance.Sharpness(enhancer)
#qq = enhancer.enhance(20)
text= pytesseract.image_to_string(qq) #.strip() #使用image_to_string识别验证码
print text
Traceback (most recent call last):
File "D:/dataDrawing/sysTestTry/baiDuCheck1.py", line 37, in <module>
text= pytesseract.image_to_string(qq) #.strip() #使用image_to_string识别验证码
File "C:\Python27\lib\site-packages\pytesseract\pytesseract.py", line 161, in image_to_string
config=config)
File "C:\Python27\lib\site-packages\pytesseract\pytesseract.py", line 94, in run_tesseract
stderr=subprocess.PIPE)
File "C:\Python27\lib\subprocess.py", line 710, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
startupinfo)
WindowsError: [Error 2]
作者:
cq520131517
时间:
2017-4-5 14:17
如果能打印出text,那就验证码文本框就可以解决。。。。求大神们帮忙解决。。
作者:
cq520131517
时间:
2017-4-5 18:43
找了一下午,终于解决了。就是把pytesseract.exe的文件目录配置在环境变量path下面就ok,感谢网友,感谢度娘,感谢楼主借地。。。
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2