selemiun 自动化测试登录验证码处理
一.软件及插件的安装
1.火狐浏览器版本(55.0(x64 zh-CN):
https://www.cnblogs.com/sandysun/p/7838113.html
插件安装:
(1)firebug
(2)selenim ide
(3)xpath finder
https://www.cnblogs.com/sandysun/p/7838113.html
插件安装:
(1)firebug
(2)selenim ide
(3)xpath finder
2.Python及编辑器安装
(1)Python 3.7.0
(下载火狐驱动geckodriver.exe2.32,保存在Python\py3安装路径中)
(1)Python 3.7.0
(下载火狐驱动geckodriver.exe2.32,保存在Python\py3安装路径中)
pycharm 2018
(2)下载selenium3.14.1(file-setting-project:code-projectinterpreder)
(2)下载selenium3.14.1(file-setting-project:code-projectinterpreder)
3. 环境以下变量设置的安装路径PATH:
jdk
python3
firebox
tesseract-OCR
jdk
python3
firebox
tesseract-OCR
4.完成以上配置就可以测试简单页面。如需输入验证码,需下载以下插件:
(1).tesseract-OCR(识别图片插件),如果没有这个插件,无法识别图片。
(2).在Pycharm中下载Pillow图片包。
(1).tesseract-OCR(识别图片插件),如果没有这个插件,无法识别图片。
(2).在Pycharm中下载Pillow图片包。
二. Pycharm编写以下代码运行
from selenium import webdriver
import time
import pytesseract
from PIL import Image, ImageEnhance
driver = webdriver.Firefox()
driver.maximize_window()
driver.get("https://cas.gzccc.edu.cn/lyuapServer/login")
driver.save_screenshot("D://Python/picture/1.png")
verification = driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div/form/div/div[3]/div[3]/img")
import time
import pytesseract
from PIL import Image, ImageEnhance
driver = webdriver.Firefox()
driver.maximize_window()
driver.get("https://cas.gzccc.edu.cn/lyuapServer/login")
driver.save_screenshot("D://Python/picture/1.png")
verification = driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div/form/div/div[3]/div[3]/img")
user=driver.find_element_by_xpath("//*[@id=\"username\"]")
passa=driver.find_element_by_xpath("//*[@id=\"password\"]")
shuru = driver.find_element_by_xpath("//*[@id=\"j_captcha_response\"]")
denglu = driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div/form/div/div[4]/input[3]")
location=verification.location
size=verification.size
ran = Image.open("D://Python/picture/1.png")#打开图片
box = (1023,410,1110,450)
ran.crop(box).save("D://Python/picture/2.png")
# 4、获取验证码图片,读取验证码
imageCode = Image.open("D://Python/picture/2.png")
# imageCode.load()
sharp_img = ImageEnhance.Contrast(imageCode).enhance(2.0)#图像增强,二值化
sharp_img.save("D://Python/picture/3.png")
sharp_img.load() # 对比度增强
time.sleep(2)
#print(sharp_img)
code = pytesseract.image_to_string(sharp_img).strip()#识别图片
# 5、收到验证码,进行输入验证
user.send_keys("??")
passa.send_keys("??")
shuru.send_keys(code)
denglu.click()
time.sleep(3)
driver.quit()
passa=driver.find_element_by_xpath("//*[@id=\"password\"]")
shuru = driver.find_element_by_xpath("//*[@id=\"j_captcha_response\"]")
denglu = driver.find_element_by_xpath("/html/body/div[2]/div/div[2]/div/form/div/div[4]/input[3]")
location=verification.location
size=verification.size
ran = Image.open("D://Python/picture/1.png")#打开图片
box = (1023,410,1110,450)
ran.crop(box).save("D://Python/picture/2.png")
# 4、获取验证码图片,读取验证码
imageCode = Image.open("D://Python/picture/2.png")
# imageCode.load()
sharp_img = ImageEnhance.Contrast(imageCode).enhance(2.0)#图像增强,二值化
sharp_img.save("D://Python/picture/3.png")
sharp_img.load() # 对比度增强
time.sleep(2)
#print(sharp_img)
code = pytesseract.image_to_string(sharp_img).strip()#识别图片
# 5、收到验证码,进行输入验证
user.send_keys("??")
passa.send_keys("??")
shuru.send_keys(code)
denglu.click()
time.sleep(3)
driver.quit()