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()
相关推荐
巴里巴气7 小时前
selenium基础知识 和 模拟登录selenium版本
爬虫·python·selenium·爬虫模拟登录
2501_924064119 小时前
2025年跨端云真机测试平台深度测评:XR与折叠屏时代的兼容性之战
测试工具·移动端自动化测试·自动化测试脚本
Small black human18 小时前
HTTP-Postman的安装及其使用
测试工具·postman
AIZHINAN1 天前
Appium 简介
自动化测试·测试工具·appium
吴free1 天前
mac电脑wireshark快速实现http接口抓包
网络·测试工具·http·wireshark
DeamoTech1 天前
ESCADA
物联网·测试工具
旷世奇才李先生2 天前
Selenium 安装使用教程
selenium·测试工具
巴里巴气2 天前
对selenium进行浏览器和驱动进行配置Windows | Linux
selenium·测试工具
q567315233 天前
Java Selenium反爬虫技术方案
java·爬虫·selenium
有趣的我3 天前
wireshark介绍和使用
网络·测试工具·wireshark