当前位置: 首页 > article >正文

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

 代码实现:

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()


http://www.kler.cn/a/417577.html

相关文章:

  • 何为运行时(Runtime)
  • React 第二十二节 useSyncExternalStore Hook 常见问题及用法详解
  • PHP 中 `foreach` 循环结合引用使用时可能出现的问题
  • 6 [新一代Github投毒针对网络安全人员钓鱼]
  • VMware下Linux和macOS遇到的一些问题总结
  • 如何优化垃圾回收机制?
  • 【K230 CanMV】图像识别-摄像头获取图像 Sensor 函数全解析
  • 开源法律、政策和实践
  • ArcGIS栅格影像裁剪工具
  • R安装rgdal报错 解决办法
  • Android 引入 proto 项目及使用方法
  • 网络安全相关证书资料
  • linux环境下,导出conda和pip的安装包和对应版本
  • solana java 转账交易示例
  • 前端用原生js下载File对象文件,多用于上传附件时,提交之前进行点击预览,或打开本地已经选择待上传的附件列表
  • DDR3保姆级使用教程:ZYNQ 7010
  • 【嵌入式——QT】QT制作安装包
  • 什么是换电系统?驱动新能源汽车发展的“能源驿站”
  • 构造函数与析构函数错题汇总
  • pip 安装指定镜像源
  • ssm_mysql_考研指导平台
  • Linux进程间通信(上)
  • android-studio 下载并安装
  • 如何正确书写sh文件/sh任务?bash任务
  • 数据结构-最小生成树
  • vue3+ant design vue实现日期选择器默认显示当前年,并限制用户只能选择当前年及之前~