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()
相关推荐
随便写个昵称5 小时前
selenium中xpath的用法大全
selenium·测试工具
随便写个昵称7 小时前
selenium中find_element()用法进行元素定位
selenium·测试工具
chao_7897 小时前
Selenium 操作表单选择控件【selenium】
运维·python·selenium·测试工具·自动化
测试老哥12 小时前
Python+Selenium实现自动化测试
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
测试老哥19 小时前
软件测试之单元测试
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
测试199821 小时前
软件测试之压力测试总结
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·压力测试
好奇的菜鸟1 天前
在 Postman 中高效生成随机环境变量的完整指南
测试工具·lua·postman
车载测试工程师1 天前
汽车功能安全-嵌入式软件测试(软件合格性测试)【目的、验证输入、集成&验证要求】11
功能测试·网络协议·测试工具·安全·车载系统·汽车·测试覆盖率
chao_7891 天前
CSS表达式——下篇【selenium】
css·python·selenium·算法
随便写个昵称1 天前
selenium跳转到新页面时如何进行定位
selenium·测试工具