自己实践成功的从csv文件中读取用户名密码并实际登录系统后判断是否登录成功,并将已经运行的用户名密码及运行结果输出到一个新的csv文件中~
# coding=utf-8
from selenium import webdriver
from time import sleep
import keyword
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import Select
import csv
import random
import io #文件路径
inputFilePath = "E:\script\python-script\csvfile_input.csv"
outputFilePath = "E:\script\python-script\csvfile_output.csv" #判断成功登录后是否存在对应元素
def IsElementExist(driver,classname):
try:
driver.find_element_by_class_name(classname)
return True
except:
return False
#读取csv文件
def Readcsvfile(filePath):
csvFile = open(filePath, "r")
reader = csv.reader(csvFile) # 返回的是迭代类型
return reader #将读取的内容写入一个新的csv文档
def Savecsvfile(filePath):
csvFile2 = open(filePath, 'w', newline='') # 设置newline,否则两行之间会空一行
writer = csv.writer(csvFile2)
writer.writerow(['UserName', 'Pwd','Result'])
writer.writerows(outputString)
#csvfile.close() #主函数
if __name__=="__main__":
outputString = []
lists=Readcsvfile(inputFilePath)
for list in lists:
userName=list[0]
pwd=list[1]
print(userName)
print(pwd)
driver = webdriver.Chrome()
driver.get("http://10.10.1.9/super/login/index.php")
driver.find_element_by_id("username").send_keys(list[0])
sleep(1)
driver.find_element_by_id("password").send_keys(list[1])
driver.find_element_by_id("loginbtn").click()
result = "Unsuccessful"
if IsElementExist(driver, "new_index_bottom_left_news"):
result = "Successful"
print(result)
outputString.append([userName, pwd,result])
driver.quit()
Savecsvfile(outputFilePath)