1 # coding=utf-8
2 """
3 Created on 2017年7月31日
4
5 @author: candy
6 """
7 from selenium import webdriver
8 from time import ctime,sleep
9 from selenium.common.exceptions import NoSuchElementException
10 import xlrd
11
12 # 用户登录
13 """
14 def login():
15 driver.get("http://10.10.1.9")
16 driver.find_element_by_id("username").send_keys("candy31")
17 driver.find_element_by_id("password").send_keys("1")
18 driver.find_element_by_id("loginbtn").click()
19 """
20 #引入excel文档进行用户名的导入
21 def open_excel(file='file.xls'):
22 try:
23 data = xlrd.open_workbook(file)
24 return data
25 except( Exception, e):
26 print( str(e))
27
28 def excel_table_byindex(file='file.xls', colnameindex=0, by_index=0):
29 data = open_excel(file)
30 table = data.sheets()[by_index]
31 nrows = table.nrows # 行数
32 colnames = table.row_values(colnameindex) # 某一行数据
33 list = []
34 for rownum in range(1, nrows):
35 row = table.row_values(rownum)
36 if row:
37 app = {}
38 for i in range(len(colnames)):
39 app[colnames[i]] = row[i]
40 list.append(app)
41 return list
42
43 def login():
44 driver.get("http://10.10.1.9/super/login/index.php")
45 driver.find_element_by_id('username').send_keys(listdata[i]['username'])
46 driver.find_element_by_id('password').send_keys("1")
47 driver.find_element_by_id("loginbtn").click()
48
49 # 考试答题
50 def exam():
51 # 进入考试
52 driver.get("http://10.10.1.9/super/pingnan/front/view.php?id=1669")
53 # 开始答题
54 driver.find_element_by_css_selector("#content_right > div:nth-child(2) > div > section > section > div.box.quizattempt > div.singlebutton.quizstartbuttondiv > form > div > input[type="submit"]:nth-child(1)").click()
55
56 # 第一题-判断题
57 driver.find_element_by_css_selector("#q1 > div.content > div > div.ablock > div.answer > div.r1 > label").click()
58 driver.find_element_by_name("next").click()
59 # 提交答案
60 #def submit():
61 # “提交所有答案并结束”
62 driver.find_element_by_css_selector("#content_right > div:nth-child(2) > div:nth-child(6) > div > div > form > div> input:nth-child(1)").click()
63 # 定位"提交答案"按钮
64 driver.find_element_by_css_selector("body>div>div>div>div>div>div>input:nth-child(1)").click()
65
66
67 # 主函数
68 if __name__ == '__main__':
69 driver = webdriver.Chrome()
70 listdata = excel_table_byindex("E:\data.xlsx",0)
71 if (len(listdata) <= 0 ):
72 assert 0, u"Excel数据异常"
73
74 for i in range(0 , len(listdata) ):
75 login()
76 exam()
77 sleep(2)
78 #submit()
79 driver.find_element_by_class_name("userpicture").click()
80 sleep(2)
81 driver.find_element_by_class_name("user_show").find_element_by_link_text(u'退出').click()
#Excel存入E盘根目录,data.xlsx内容如下截图: