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()
相关推荐
程序员小远9 小时前
自动化测试与功能测试详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
第三方软件测评19 小时前
第三方软件测评中心分享:软件功能测试类型和测试工具
功能测试·测试工具
水银嘻嘻20 小时前
web 自动化之 selenium 下拉&鼠标键盘&文件上传
selenium·自动化
攻城狮7号1 天前
Python爬虫第20节-使用 Selenium 爬取小米商城空调商品
开发语言·数据库·爬虫·python·selenium
慢一点会很快2 天前
【网络分析工具】网络工具wireshark、TCPdump、iperf使用详解
测试工具·wireshark·tcpdump
代码的乐趣2 天前
支持selenium的chrome driver更新到136.0.7103.92
chrome·python·selenium
海尔辛2 天前
学习黑客抓包wireshark
学习·测试工具·wireshark
xixixiLucky2 天前
配置Java Selenium Web自动化测试环境
java·前端·selenium
黑客笔记3 天前
sql注入漏洞的对抗
数据库·sql·测试工具
悟能不能悟3 天前
如何在postman使用时间戳
测试工具·postman