提交 27893344 authored 作者: 彭甘宇's avatar 彭甘宇

新增-运维登录测试

上级 a16c4b75
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
import logging
import time
file_path = r'C:\Users\EDY\Desktop\git\运维自动化脚本\运维集控\项目测试\运维标准版\登录用户数据.xlsx'
df = pd.read_excel(file_path)
#driver = webdriver.Chrome()
edge_options = Options()
edge_options.add_argument('--ignore-certificate-errors')
edge_options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Edge(options=edge_options)
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def login(account, password, captcha_solution):
try:
# 定位并填写用户名
username_input = WebDriverWait(driver, 3).until(
EC.presence_of_element_located((By.XPATH, "//input[@placeholder='请输入登录账号']"))
)
username_input.clear()
username_input.send_keys(account)
# 定位并填写密码
password_input = WebDriverWait(driver, 3).until(
EC.presence_of_element_located((By.XPATH, "//input[@placeholder='请输入登录密码']"))
)
password_input.clear()
password_input.send_keys(password)
# 定位并填写验证码
captcha_input = WebDriverWait(driver, 3).until(
EC.presence_of_element_located((By.XPATH, "//input[@placeholder='请输入验证码(区分大小写)']"))
)
captcha_input.clear()
captcha_input.send_keys(captcha_solution)
# 登录
login_button = WebDriverWait(driver, 3).until(
EC.element_to_be_clickable((By.XPATH, "//div[@class='loginButton']"))
)
login_button.click()
time.sleep(5)
try:
success_popup = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.XPATH, "//*[contains(text(), '操作成功!')]"))
)
logging.info(f"登录成功: {account}")
return True
except:
try:
error_popup = WebDriverWait(driver, 3).until(
EC.presence_of_element_located(
(By.XPATH, "//*[contains(text(), '输入的用户或密码不一致') or contains(text(), '验证码无效,请重新输入')]"))
)
logging.error(f"登录失败: {account}")
return False
except:
logging.error(f"登录失败: {account}")
return False
except Exception as e:
logging.error(f"发生错误: {e}")
return False
# 打开登录页面
driver.get("https://192.168.1.118:8443/#/login")
for index, row in df.iterrows():
account = row['account']
password = row['password']
captcha_solution = row['certcode']
login_result = login(account, password, captcha_solution)
driver.refresh()
# 关闭浏览器
driver.quit()
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论