打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python selenium中Excel数据维护(二)

接着python里面的xlrd模块详解(一)中我们我们来举一个实例:

我们来举一个从Excel中读取账号和密码的例子并调用:

  ♦1.制作Excel我们要对以上输入的用户名和密码进行参数化,使得这些数据读取自Excel文件。我们将Excel文件命名为data.xlsx,其中有两列数据,第一列为username,第二列为password。

  ♦2.读取Excel代码如下

#-*- coding:utf-8 -*-import xlrd,time,sys,unittest    #导入xlrd等相关模块class Data_Excel(unittest.TestCase):# 封装在Data_Excel类里面方便后面使用    file_addrec = r'C:\Users\liqiang22230\Desktop\date.xlsx' #定义date.xlsx数据维护Excel的路径文件    def open_excel(self,file = file_addrec):#file = file_addrec #注意在class中def中一定要带self        try:#检验文件有没有被获取到            self.data =xlrd.open_workbook(file)            return self.data        except Exception :            print(file)            print('eero')    def excel_table_byindex(self,file = file_addrec,colnameindex=0,by_index='用户表'):        #把这个读取Excel中封装在excel_table_byindex函数中,这时需要三个参数1.文件2.sheet名称,列所在的行数          self.data = xlrd.open_workbook(file)#获取Excel数据          self.table = self.data.sheet_by_name(by_index)#使用sheet_by_name获取sheet页名叫用户表的sheet对象数据          self.colnames  = self.table.row_values(colnameindex)#获取行数下标为0也就是第一行Excel中第一行的所有的数据值          self.nrows = self.table.nrows #获得所有的有效行数          list = []#总体思路是把Excel中数据以字典的形式存在字符串中一个字典当成一个列表元素          for rownum in range(1,self.nrows):            row = self.table.row_values(rownum)#获取所有行数每一行的数据值                if row:                app = {}#主要以{'name': 'zhangsan', 'password': 12324.0},至于字典中有多少元素主要看有多少列                     for i in range(len(self.colnames)):             #在这个Excel中,列所在的行有两个数据,所以没循环一行就以这两个数据为键,行数的值为键的值,保存在一个字典里                          app[self.colnames[i]] = row[i]                    list.append(app)        print(list)        return lista = Data_Excel()a.excel_table_byindex()if __name__=="__main__":    unittest.main()

执行结果如下:

Testing started at 15:47 ...[{'name': 'zhangsan', 'password': 12324.0}, {'name': 'zhangsan', 'password': 12324.0}, {'name': 'lisi', 'password': 923848.0}, {'name': 'lisi', 'password': 923848.0}, {'name': 'wangmazi', 'password': 213123.0}, {'name': 'wangmazi', 'password': 213123.0}]Process finished with exit code 0Empty test suite.

  ♦3.调用Excel代码如下:

def Login(self):        listdata = excel_table_byindex("E:\\data.xlsx",0)#传入两个参数1.文件路径2.第一行所在下标        if (len(listdata) <= 0 ):#判断list列表中是否有数据                assert 0 , u"Excel数据异常"        for i in range(0 , len(listdata) ):#循环出list中所有的字典                self.driver = webdriver.Chrome()                self.driver.get("http://www.effevo.com")                assert "effevo" in self.driver.title                #点击登录按钮                self.driver.find_element_by_xpath(".//*[@id='home']/div/div[2]/header/nav/div[3]/ul/li[2]/a").click()                time.sleep(1)                self.driver.find_element_by_id('passname').send_keys(listdata[i]['username'])#切出list下标下标为i的字典键为username的值                self.driver.find_element_by_id('password').send_keys(listdata[i]['password'])#切出list下标下标为i的字典键为password的值                self.driver.find_element_by_xpath(".//*[@id='content']/div/div[6]/input").click()                time.sleep(2)
          self.driver.close()

有什么需要纠正的地方请一定要指出来!谢谢啦。

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
前端实战:Vue实现数据导出导入案例
【auth】Laravel解决多张表身份验证的问题
excel上标下标2上下标
代码实现 UITableView与UITableViewCell
Selenium2+python自动化58
pyqt5 tablewidget导出excel
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服