1# coding=utf-82 fromSelenium Import Webdriver3 fromTime Import Sleep4 Import Keyword5 fromSelenium.webdriver.common.keys Import Keys6 fromselenium.webdriver.support.wait Import webdriverwait7 fromselenium.webdriver.support.ui Import Select8 Import CSV9 Import RandomTen Import IO One A #文件路径 -Inputfilepath ="Documents\logininputdata.csv" -Outputfilepath ="Documents\loginoutputdata.csv" the - def readcsvfiletolist (filePath): - -With Io.open (FilePath,"R", encoding='Utf-8') asCSVFile: +List =Csvfile.readlines () - returnList + A def savelisttofiletolist (filePath): at -CSVFile = File (FilePath,'WB') -writer =Csv.writer (csvfile) - #表头 -Writer.writerow (['UserName','PWD',"']) - writer.writerows (outputstring) in csvfile.close () - to #通过Id判断该元素是否存在 + def iselementexist (driver,id): - Try: the driver.find_element_by_id (ID) * returnTrue $ except:Panax Notoginseng returnFalse - the if__name__=="__main__": + A #读取文件到list数组里面 theLists =readcsvfiletolist (Inputfilepath) +#lists的组成: ["grace,1","grace2,2"] - #声明一个输出数组 to save the result set $outputstring=[] $ - #循环数组 - forListinchlists: the - #获取每一行数据 and use a comma to unpack the user name and passwordWuyi#list ="grace,1" the#list. Split (",") =[grace1,1] #csv是用逗号分隔, the following list.split (",")[0] is the 1th element in the Grace1,list.split (",")[1the 2nd element in the] is 1 -Username=list.split (",")[0].strip () WuPwd= List.split (",")[1].strip () - About print (userName) $ print (PWD) - - #开始浏览登陆 -Driver =Webdriver. Firefox () ADriver.Get("http://demo.pingnanlearning.com/test/login/index.php") + theDRIVER.FIND_ELEMENT_BY_ID ("username"). Send_keys (UserName) -Sleep1) $DRIVER.FIND_ELEMENT_BY_ID ("Password"). Send_keys (PWD) theSleep1) theDRIVER.FIND_ELEMENT_BY_ID ("loginbtn"). Click () the theSleep2) - inresult="unsuccessful" the #il判断是否登陆成功 the ifIselementexist (Driver,"maincontent"): About #成功 theresult ="successful" the #将结果追加到保存结果集 the outputstring.append ([Username,pwd,result]) + driver.quit () - the #Bayi # End of Cycle the print (outputstring) the - #将结果集写入文件 -Savelisttofiletolist (Outputfilepath)
The CSV read in the "Python" file reads the data in the CSV read and the execution is successful.