慕课网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(“注册”))
#传入得元素是否可见
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”)
验证码思路
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%
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] 系统找不到指定的文件。
修改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
重启下pycharm,或者重启下电脑,我试了下 还是可以的
如何调用验证码 地址:https://www.showapi.com/api/lookPoint/184/4 账号姓名:xuzhongtao 密码:88552829ABCd