sasa写的文件(包含解析文字)
1 # coding=utf-8 2 from selenium import webdriver 3 from time import sleep 4 import keyword 5 from selenium.webdriver.common.keys import Keys 6 from selenium.webdriver.support.wait import WebDriverWait 7 from selenium.webdriver.support.ui import Select 8 import csv 9 import random 10 import io 11 12 #文件路径 13 inputFilePath = "DocumentsLoginInputData.csv" 14 outputFilePath = "DocumentsLoginOutputData.csv" 15 16 def ReadCsvFileToList(filePath): 17 18 with io.open(filePath, "r", encoding='utf-8') as csvfile: 19 list = csvfile.readlines() 20 return list 21 22 def SaveListToFileToList(filePath): 23 24 csvfile = file(filePath, 'wb') 25 writer = csv.writer(csvfile) 26 #表头 27 writer.writerow(['UserName', 'Pwd', '']) 28 writer.writerows(outputString) 29 csvfile.close() 30 31 #通过Id判断该元素是否存在 32 def IsElementExist(driver,id): 33 try: 34 driver.find_element_by_id(id) 35 return True 36 except: 37 return False 38 39 if __name__=="__main__": 40 41 #读取文件到list数组里面 42 lists = ReadCsvFileToList(inputFilePath) 43 #lists的组成:["grace,1","grace2,2"] 44 #声明一个输出数组 来保存结果集 45 outputString=[] 46 47 #循环数组 48 for list in lists: 49 50 #获取每一行数据 并以逗号拆封为用户名和密码 51 #list ="grace,1" 52 #list.split(",")=[grace1,1]#csv是用逗号分隔,下面的list.split(",")[0]中的第1个元素即grace1,list.split(",")[1]中的第2个元素即1 53 userName=list.split(",")[0].strip() 54 pwd= list.split(",")[1].strip() 55 56 print(userName) 57 print(pwd) 58 59 #开始浏览登陆 60 driver = webdriver.Firefox() 61 driver.get("http://demo.pingnanlearning.com/test/login/index.php") 62 63 driver.find_element_by_id("username").send_keys(userName) 64 sleep(1) 65 driver.find_element_by_id("password").send_keys(pwd) 66 sleep(1) 67 driver.find_element_by_id("loginbtn").click() 68 69 sleep(2) 70 71 result="Unsuccessful" 72 #il判断是否登陆成功 73 if IsElementExist(driver, "maincontent"): 74 #成功 75 result ="Successful" 76 #将结果追加到保存结果集 77 outputString.append([userName,pwd,result]) 78 driver.quit() 79 80 # 81 # 循环结束 82 print(outputString) 83 84 #将结果集写入文件 85 SaveListToFileToList(outputFilePath)