验证码报错:FileNotFoundError: [WinError 2] 系统找不到指定的文件

慕课网python的selenium自动化笔记
#conding=utf-8
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait #判断元素是否存在
from selenium.webdriver.support import expected_conditions as EC#判断标题是否存在
from selenium.webdriver.common.by import By #定位元素模块
import time
driver=webdriver.Chrome()
driver.get(“http://www.5itest.cn/register”)
driver.maximize_window()
time.sleep(5)
#判断是否存在此标题
print(EC.title_contains(“注册”))

element=driver.find_elements_by_class_name(“controls”)

#传入得元素是否可见
locator=(By.CLASS_NAME,‘controls’)
WebDriverWait(driver,1).until(EC.visibility_of_element_located(locator))
email_element=driver.find_element_by_id(“register_email”)
print(email_element.get_attribute(‘placeholder’)) #获取值的隐藏信息 标记
email_element.send_keys("[email protected]")
print(email_element.get_attribute(‘value’))#获取你输入的值
driver.quit()
#随机生成5位的邮箱
for i in range(5):
user_element=’’.join(random.sample(‘123456789abcdefghjk’,5))+"@163.com"
print(user_element)

‘’'driver.find_element_by_id(“register_email”).send_keys("[email protected]")

user_name_element_node=driver.find_elements_by_class_name(“controls”)[1]
user_element=user_name_element_node.find_element_by_class_name(“form-control”)
user_element.send_keys(“111111”)

driver.find_element_by_name(“password”).send_keys(“111111”)
driver.find_element_by_xpath("//*[@id=‘captcha_code’]").send_keys(“111111”)’’’

验证码思路
pip install Pillow
pip install pytesseract
driver.save_screenshot(“E:/123.png”)
code_elemnet=driver.find_element_by_id(“getcode_num”)
print(code_elemnet.location)
left=code_elemnet.location[‘x’]
top=code_elemnet.location[‘y’]
right=code_elemnet.size[‘width’]+left
height=code_elemnet.size[‘height’]+top
im=Image.open(“E:/123.png”)
img = im.crop((left,top,right,height))
img.save(“E:/666.png”)
文本大小要改成100%
验证码报错:FileNotFoundError: [WinError 2] 系统找不到指定的文件_第1张图片
windows安装tesseract-ocr 下载地址:链接:https://pan.baidu.com/s/1E1hBscSfjapEUqwrcEQFSQ 提取码:tlrz

github地址: https://github.com/tesseract-ocr/tesseract

You can either Install Tesseract via pre-built binary package or build it from source.

windows:

The latest installer can be downloaded here: http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-3.05.01.exe and http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-4.00.00dev.exe (experimental).

加入系统的path环境变量

验证码报错:FileNotFoundError: [WinError 2] 系统找不到指定的文件_第2张图片

出现这个报错
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

修改pytesseract.py文件里的tesseract_cmd

tesseract_cmd = ‘C:\Program Files\Tesseract-OCR\tesseract.exe’

下载的默认训练集也没有添加到系统路径,会报错pytesseract.pytesseract.TesseractError: (1, ‘Error opening data file \Program Files (x86)\Tesseract-OCR\tessdata/chi_sim.traineddata’)

解决方法:
设置环境变量 TESSDATA_PREFIX
C:\Program Files (x86)\Tesseract-OCR\tessdata

验证码报错:FileNotFoundError: [WinError 2] 系统找不到指定的文件_第3张图片

重启下pycharm,或者重启下电脑,我试了下 还是可以的


如何调用验证码 地址:https://www.showapi.com/api/lookPoint/184/4 账号姓名:xuzhongtao 密码:88552829ABCd

验证码报错:FileNotFoundError: [WinError 2] 系统找不到指定的文件_第4张图片

验证码报错:FileNotFoundError: [WinError 2] 系统找不到指定的文件_第5张图片

#coding=utf-8
from selenium import webdriver
import time
import random
from PIL import Image
from book.ShowapiRequest import ShowapiRequest
driver = webdriver.Chrome()
def driver_init():
driver.get(“http://www.5itest.cn/register”)
driver.maximize_window()
time.sleep(5)
#获取element信息
def get_element(id):
element=driver.find_element_by_id(id)
return element
#获取随机数
def get_range_user():
user_element=’’.join(random.sample(‘123456789abcdefghjk’,8))
return user_element
#获取图片
def get_code_inage(filename):
driver.save_screenshot(filename)
code_elemnet = driver.find_element_by_id(“getcode_num”)
# print(code_elemnet.location)
left = code_elemnet.location[‘x’]
top = code_elemnet.location[‘y’]
right = code_elemnet.size[‘width’] + left
height = code_elemnet.size[‘height’] + top
im = Image.open(filename)
img = im.crop((left, top, right, height))
img.save(filename)
#解析图片
def code_element(filename):
r = ShowapiRequest(“http://route.showapi.com/184-4”, “78579”, “1778421c4a7c4dd9815875fcd79b5075”)
r.addBodyPara(“typeId”, “35”)
r.addBodyPara(“convert_to_jpg”, “0”)
r.addBodyPara(“needMorePrecise”, “0”)
r.addFilePara(“image”,filename ) # 文件上传时设置
res = r.post()
text = res.json()[‘showapi_res_body’][‘Result’]
return text
#运行主程序
def run_main():
user_name_info=get_range_user()
user_email=user_name_info+"@163.com"
filename=“E:/123.png”
driver_init()
get_element(“register_email”).send_keys(user_email)
get_element(“register_nickname”).send_keys(user_name_info)
get_element(“register_password”).send_keys(“111111”)
get_code_inage(filename)
text=code_element(filename)
get_element(“captcha_code”).send_keys(text)
get_element(“register-btn”).click()
driver.close()
run_main()

你可能感兴趣的:(WEB自动化测试,验证码报错问题)