码迷,mamicode.com
首页 > 编程语言 > 详细

python 获取excel数据 自动登陆

时间:2019-02-21 15:52:53      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:tab   drive   row   []   logs   .sh   file   path   click   

import xlrd
import time
import unittest
from selenium import webdriver

class u8819(unittest.TestCase):
global loginurl
global driverpath
loginurl = ‘http://www.effevo.com‘
driverpath = ‘D:\\chromedriver.exe‘
userpath = ‘D:\\data.xlsx‘

#获取data.xlsx中的用户数据(用户名密码)
def open_excel(self, file = userpath, rownameindex = 0, table = ‘用户表‘):
try:
self.data = xlrd.open_workbook(file)
self.table = self.data.sheet_by_name(table)
self.colnames = self.table.row_values(rownameindex) #找到列名 默认第一行为列名
self.nrows = self.table.nrows
for rownum in range(1, self.nrows):
rowvalue = self.table.row_values(rownum)
list = []

if rowvalue:
user = {}
for i in range(len(rowvalue)):
user[self.colnames[i]] = rowvalue[i]
list.append(user)
print(list)
return list
except Exception:
print(‘文件未发现:‘ + file)

   #获取excel的用户名密码登陆  
def login(self):
listdata = self.open_excel()
#if(len(listdata) < 0 ):
# assert 0, u"Excel数据异常:无数据"

for i in range(len(listdata)):
self.driver = webdriver.Chrome(driverpath)
self.driver.get(loginurl)
assert "工作云" in self.driver.title

self.driver.find_element_by_xpath(".//*[@id=‘home‘]/div/div[2]/header/nav/div[3]/ul/li[1]/a").click()
time.sleep(5)

self.driver.find_element_by_xpath(".//*[@id=‘passname‘]").clear()
self.driver.find_element_by_xpath(".//*[@id=‘passname‘]").send_keys(str(listdata[i][‘username‘]))
self.driver.find_element_by_xpath(".//*[@id=‘password‘]").clear()
self.driver.find_element_by_xpath(".//*[@id=‘password‘]").send_keys(listdata[i][‘password‘])
self.driver.find_element_by_xpath(".//*[@id=‘content‘]/div/div[6]/input").click()


a = u8819()
a.login()

if __name__ == ‘__main__‘:
unittest.main()

来源:https://www.cnblogs.com/insane-Mr-Li/p/9093212.html

python 获取excel数据 自动登陆

标签:tab   drive   row   []   logs   .sh   file   path   click   

原文地址:https://www.cnblogs.com/orientation-jpyayi/p/10412740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!