【Python】sasa版:文件中csv讀取在寫入csv讀取的數據和執行是否成功。

 sasa寫的文件(包含解析文字)php

 

 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 = "Documents\LoginInputData.csv"
14 outputFilePath = "Documents\LoginOutputData.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)
相關文章
相關標籤/搜索