Selenium 自动化测试demo

场景描述:

模拟用户登录页面操作,包括输入用户名、密码、验证码。验证码为算数运算,如下:

使用到的工具和依赖:

  1. Selenium:pip install selenium

  2. 需要安装浏览器驱动:这里使用的是Edge

  3. Pillow : 用来处理图像,例如图像二值化等等

  4. 图像识别库pytesseract:

3.1 下载安装Tesseract:https://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-w64-setup-5.3.3.20231005.exe

3.2 配置环境变量

3.3 在pycharm中下载依赖:pip install pytesseract

代码实现:

python 复制代码
import base64
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service
from PIL import Image
import pytesseract
import io
import re

#edge驱动
edge_driver_path = 'E:\SoftWare_work\download\edgedriver_win64\msedgedriver.exe'
#浏览器选型配置
edge_options=Options()
#edge_options.add_argument("--headless")   加上该行,代码运行时不会打开浏览器
#启动浏览器
service=Service(edge_driver_path)
driver=webdriver.Edge(options=edge_options,service=service)

#网页
loginPage="http://your_page_ip/login?redirect=/index"
driver.get(loginPage)

time.sleep(2) #等待加载

'''输入用户名、密码、验证码登录'''
user_name=driver.find_element(By.XPATH,"//input[@class='el-input__inner' and @type='text' and @placeholder='用户名']")
user_name.send_keys("username")
password=driver.find_element(By.XPATH,"//input[@class='el-input__inner' and @type='password' and @placeholder='密码']")
password.send_keys("password")
#处理验证码
#1.定位图片
img_elem=driver.find_element(By.CSS_SELECTOR,"div.login-code img.login-code-img")
#2. 获取src属性 base64编码的图片
img_src=img_elem.get_attribute("src")
'''读取图像'''
#2.2 提取base64编码部分
if img_src.startswith("data:image"):
    img_src=img_src.split(",")[1]
#2.3 解码base64数据
image_data=base64.b64decode(img_src)
#2.4 读取图像
image=Image.open(io.BytesIO(image_data))
image.show()#原图像显示
'''图像处理'''
#转化为灰度图像
image_gray=image.convert("L")
image_gray.show()
#图像二值化处理
threshold_image=image_gray.point(lambda p:p>128 and 255)
#图像显示
threshold_image.show()
'''图像识别'''
text = pytesseract.image_to_string(threshold_image)

#提取字符串中的数字和运算符并和计算验证码的值
pattern = r'\d+[+\-*/×]\d+'
matchs=(re.match(pattern,text)).group()
result=0
if matchs.__contains__("+"):
    num1=matchs.split("+")[0]
    num2=matchs.split("+")[1]
    result=int(num1)+int(num2)
elif matchs.__contains__("-"):
    num1 = matchs.split("-")[0]
    num2 = matchs.split("-")[1]
    result = int(num1) - int(num2)
elif matchs.__contains__("*"):
    num1=matchs.split("*")[0]
    num2=matchs.split("*")[1]
    result=int(num1)*int(num2)
else:
    num1 = matchs.split("/")[0]
    num2 = matchs.split("/")[1]
    result = int(num1) / int(num2)
#定位验证码输入框,输入验证码
login_code=driver.find_element(By.XPATH,"//input[@class='el-input__inner' and @type='text' and @placeholder='验证码']")
login_code.send_keys(result)

#点击登录
login_button=driver.find_element(By.CSS_SELECTOR,"button")
login_button.click()

#关闭网页
driver.quit()
相关推荐
看,未来10 小时前
Apipost 与 Postman 工具实践指南:WebSocket调试与动态参数测试
websocket·测试工具·postman
shimly12345611 小时前
tcpdump 用法示例
网络·测试工具·tcpdump
程序员老舅12 小时前
C++ Qt项目教程:WebServer网络测试工具
c++·qt·测试工具·webserver·qt项目·qt项目实战
绿色果酱15 小时前
利用Postman和Apipost进行WebSocket调试和文档设计
websocket·测试工具·yapi·postman
测试199820 小时前
Jmeter HTTP代理服务器录制压力脚本
自动化测试·软件测试·测试工具·jmeter·程序人生·职场和发展·测试用例
bug管理者1 天前
分享几款比较常用的接口测试工具
测试工具
程序员 小濠1 天前
接口测试基础 --- 什么是接口测试及其测试流程?
自动化测试·python·测试工具·职场和发展·appium·接口测试·压力测试
起个破名想半天了1 天前
Web自动化中Selenium下Chrome与Edge的Webdriver常用Options参数
python·selenium·自动化
赶路人儿2 天前
postman并发测试某个接口
测试工具·postman