from selenium import webdriver
from selenium.webdriver.common.by import By
from PIL import Image
import os
import pytesseract
import time
from applitools.selenium import Eyes, Target
import base64
class HelloWorld:
global driver, eyes
try:
# 这里填写你保存的秘钥
eyes = Eyes()
eyes.api_key = 'XXX'
# 初始化Chrome浏览器驱动
driver = webdriver.Chrome()
driver.maximize_window()
eyes.open(driver, "智募通测试", "智募通测试", {'width': 1920, 'height': 1080})
time.sleep(3)
# Navigate the browser to the "百度" web-site.
driver.get('https://xxxxx.xxxxp.com/')
time.sleep(3)
# 检查点 #1: 首页
eyes.check("测试首页", Target.window())
time.sleep(3)
# 点击"切换"按钮
driver.find_element(By.CLASS_NAME, 'switch-icon').click()
time.sleep(3)
# 定位用户名输入框并输入用户名
driver.find_element(By.CLASS_NAME, 'el-input__wrapper').click()
time.sleep(3)
username = driver.find_element(By.XPATH,
'/html/body/div/div/div[3]/div[1]/div/div/div[2]/div/form/div[1]/div/div[1]/div/input')
username.send_keys("jttest003")
time.sleep(3)
# 定位密码输入框并输入密码
driver.find_element(By.CLASS_NAME, 'el-input__inner').click()
password = driver.find_element(By.XPATH,
'/html/body/div/div/div[3]/div[1]/div/div/div[2]/div/form/div[2]/div/div[1]/div/input')
password.send_keys("123456")
eyes.check("用户名输入框", Target.region(username))
eyes.check("密码输入框", Target.region(password))
time.sleep(3)
# 获取验证码图片的src属性值
img_element = driver.find_element(By.XPATH,
'/html/body/div/div/div[3]/div[1]/div/div/div[2]/div/form/div[3]/div/div[1]/div/div/span[2]/span/div/img')
captcha_src = img_element.get_attribute('src')
# 将base64编码的图片数据解码
image_data = captcha_src.split(',')[1]
image_binary = base64.b64decode(image_data)
print(image_binary)
# 获取当前目录
dir = os.getcwd()
print(dir)
# 将二进制数据保存为图像文件
image_path = os.path.join(dir, 'captcha1.png')
with open(image_path, 'wb') as img_file:
img_file.write(image_binary)
# 如果你还没有设置Tesseract的路径,需要在这里设置
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
# 打开图像文件
image = Image.open(image_path)
# 使用pytesseract提取图像中的文字
text = pytesseract.image_to_string("captcha1.png", lang='chi_sim')
# 去除多余的空格和换行符
text = text.replace(" ", "").replace("\n", "")
time.sleep(3)
# 输入验证码
driver.find_element(By.XPATH,
'/html/body/div/div/div[3]/div[1]/div/div/div[2]/div/form/div[3]/div/div[1]/div/div/input').click()
captcha = driver.find_element(By.XPATH,
'/html/body/div/div/div[3]/div[1]/div/div/div[2]/div/form/div[3]/div/div[1]/div/div/input')
captcha.send_keys(text)
eyes.check("验证码输入框", Target.region(captcha))
# 点击登录按钮
driver.find_element(By.XPATH,
'/html/body/div/div/div[3]/div[1]/div/div/div[2]/div/form/div[5]/div/button/span').click()
time.sleep(10)
# End the test.
results = eyes.close(False)
print(results)
except Exception as e:
print(f"Applitools Error: {str(e)}")
# 如果需要,可以在这里添加其他错误处理逻辑
finally:
# Close the browser.
driver.quit()
# If the test was aborted before eyes.close was called, ends the test as aborted.
eyes.abort()