sasa写的文件(包含解析文字)
# coding=utf-
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 = "Documents\LoginInputData.csv"
outputFilePath = "Documents\LoginOutputData.csv" def ReadCsvFileToList(filePath): with io.open(filePath, "r", encoding='utf-8') as csvfile:
list = csvfile.readlines()
return list def SaveListToFileToList(filePath): csvfile = file(filePath, 'wb')
writer = csv.writer(csvfile)
#表头
writer.writerow(['UserName', 'Pwd', ''])
writer.writerows(outputString)
csvfile.close() #通过Id判断该元素是否存在
def IsElementExist(driver,id):
try:
driver.find_element_by_id(id)
return True
except:
return False if __name__=="__main__": #读取文件到list数组里面
lists = ReadCsvFileToList(inputFilePath)
#lists的组成:["grace,1","grace2,2"]
#声明一个输出数组 来保存结果集
outputString=[] #循环数组
for list in lists: #获取每一行数据 并以逗号拆封为用户名和密码
#list ="grace,1"
#list.split(",")=[grace1,]#csv是用逗号分隔,下面的list.split(",")[]中的第1个元素即grace1,list.split(",")[]中的第2个元素即1
userName=list.split(",")[].strip()
pwd= list.split(",")[].strip() print(userName)
print(pwd) #开始浏览登陆
driver = webdriver.Firefox()
driver.get("http://demo.pingnanlearning.com/test/login/index.php") driver.find_element_by_id("username").send_keys(userName)
sleep()
driver.find_element_by_id("password").send_keys(pwd)
sleep()
driver.find_element_by_id("loginbtn").click() sleep() result="Unsuccessful"
#il判断是否登陆成功
if IsElementExist(driver, "maincontent"):
#成功
result ="Successful"
#将结果追加到保存结果集
outputString.append([userName,pwd,result])
driver.quit() #
# 循环结束
print(outputString) #将结果集写入文件
SaveListToFileToList(outputFilePath)