-
Notifications
You must be signed in to change notification settings - Fork 0
/
zhiniao.py
215 lines (168 loc) · 8.54 KB
/
zhiniao.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# -*- coding:utf-8 -*-
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import redisUtil
# 保存题库
def init_answer():
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--start-maximized')
browser = webdriver.Chrome(executable_path='./driver/chromedriver.exe',
chrome_options=chrome_options)
# 访问登录页面
browser.get(url)
time.sleep(1)
# 题目数
count = get_question_count(browser)
click_start_exam(browser)
time.sleep(1)
# 交卷
commit_answer(browser)
# 确认提交
confirm_commit_answer(browser)
# 继续交卷
confirm_commit_next_answer(browser)
# 添加错题
add_error_question(browser, count)
def get_question_count(browser):
question_count = browser.find_element_by_css_selector("[class='info-value question-counts']")
print question_count.text
count = int(question_count.text)
return count
def click_start_exam(browser):
start_btn = browser.find_element_by_id("exam-btn-wrapper")
print start_btn.text
start_btn.click()
param_know = (By.CLASS_NAME, "modal-alert-footer")
windows = WebDriverWait(browser, 20).until(EC.visibility_of_element_located(param_know))
windows.click()
def confirm_commit_next_answer(browser):
wait_confirm_commit_next = (By.CLASS_NAME, "zn-modal-confirm-footer")
confirm_commit_div_next = WebDriverWait(browser, 10).until(
EC.visibility_of_element_located(wait_confirm_commit_next))
confirm_commit_next = confirm_commit_div_next.find_element_by_css_selector(
"[class='btn confirm-cancel zn-btn-empty']")
confirm_commit_next.click()
def confirm_commit_continue_answer(browser):
wait_confirm_commit_next = (By.CLASS_NAME, "zn-modal-confirm-footer")
confirm_commit_div_next = WebDriverWait(browser, 10).until(
EC.visibility_of_element_located(wait_confirm_commit_next))
confirm_commit_next = confirm_commit_div_next.find_element_by_css_selector(
"[class='btn confirm-ok zn-btn-default']")
confirm_commit_next.click()
def confirm_commit_answer(browser):
wait_confirm_commit = (By.CLASS_NAME, "zn-modal-answer-sheet-footer")
confirm_commit_div = WebDriverWait(browser, 10).until(EC.visibility_of_element_located(wait_confirm_commit))
confirm_commit = confirm_commit_div.find_element_by_css_selector("[class='btn submitExam zn-btn-default']")
confirm_commit.click()
def commit_answer(browser):
commit = browser.find_element_by_css_selector("[class='btn submit-btn zn-btn-default']")
print commit.text
commit.click()
# 添加错误题目答案
def add_error_question(browser, count):
# 查看错题
wait_query_error_question = (By.CLASS_NAME, "error-bank")
query_error_question = WebDriverWait(browser, 10).until(EC.visibility_of_element_located(wait_query_error_question))
query_error_question.click()
for i in range(count):
num, question = get_question(browser)
# 正确答案
wait_correct_answer = (By.CLASS_NAME, "correct-answer")
correct_answer = WebDriverWait(browser, 10).until(EC.visibility_of_element_located(wait_correct_answer))
print "正确答案", correct_answer.text
r_conn = redisUtil.get_connection()
r_conn.setnx(key_pref + question, correct_answer.text)
# 下一题
wait_next_question_div = (By.CLASS_NAME, "body-footer")
next_question_div = WebDriverWait(browser, 10).until(EC.visibility_of_element_located(wait_next_question_div))
next_question = None
next_question_button = next_question_div.find_elements_by_xpath('button')
count_button_len = len(next_question_button)
if count_button_len == 1:
next_question = next_question_button[0]
elif count_button_len == 2:
next_question = next_question_button[1]
# try:
# next_question = next_question_div.find_element_by_css_selector("[class='btn next-btn zn-btn-default']")
# except Exception as e:
# next_question = next_question_div.find_element_by_css_selector("[class='btn next-btn zn-btn-default zn-btn-default-hover zn-btn-default-active']");
if i < count - 1:
next_question.click()
time.sleep(1)
def get_question(browser):
# 问题
wait_question = (By.CLASS_NAME, "question-title")
question = WebDriverWait(browser, 5).until(EC.visibility_of_element_located(wait_question))
question_str = question.text.split('.', 1)
print question_str[0], question_str[1]
return question_str[0], question_str[1]
def start_exam():
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--start-maximized')
browser = webdriver.Chrome(executable_path='./driver/chromedriver.exe',
chrome_options=chrome_options)
# 访问登录页面
browser.get(url)
# 题目数
count = get_question_count(browser)
# 开始考试
click_start_exam(browser)
r_conn = redisUtil.get_connection()
for i in range(count):
# 问题
num, question = get_question(browser)
answer_map = r_conn.get(key_pref + question)
wait_question_type = (By.CLASS_NAME, "question-type")
# 问题类型
question_type = WebDriverWait(browser, 5).until(EC.visibility_of_element_located(wait_question_type))
print question_type.text
answer_ul = browser.find_element_by_class_name("answers")
answer_li = answer_ul.find_elements_by_xpath('li')
match = False
for li in answer_li:
key = li.find_element_by_class_name("answer-key")
value = li.find_element_by_class_name("answer-content")
answer_key = key.text[0]
print answer_key, value.text
if question_type.text == u"多选题":
if answer_map is None:
break
for answer in answer_map:
if answer_key == answer:
match = True
li.click()
break
else:
if answer_key == answer_map:
match = True
li.click()
break
if not match:
answer_li[0].click()
print "没有匹配的答案,盲选A。题号:", num
if question_type.text == u"多选题":
# 下一题
next_question = browser.find_element_by_css_selector(
"[class='btn next-btn zn-btn-default']")
next_question.click()
time.sleep(1)
# 交卷
commit_answer(browser)
# 确认提交
confirm_commit_answer(browser)
# 继续提交
confirm_commit_continue_answer(browser)
# 添加错题
add_error_question(browser, count)
if __name__ == '__main__':
url = "https://www.zhi-niao.com/znWeb/exam/detail.html?examId=1220444&mode=F&data=JTdCJTIyaXNTaW5nbGVMb2dpbiUyMiUzQ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