def query_ans_normal(ev=None): global res infodic = { 'question': str(queText.get('0.0', 'end')), 'type': '其他', 'course': '', 'courseID': '' } QA = QueryAns(**infodic) res.set(str(QA.work())) queText.delete('1.0', 'end')
self.rate, self._sc_out_fp) singlecourse.work() if __name__ == "__main__": if len(argv) == 1: #logindata = getlogindata() logindata = getlogindata_phone()[0:2] mode = 0 rate = 1 noans_num = 5 else: logindata = (argv[1]).split(',') mode = int(argv[2]) rate = eval(argv[3]) noans_num = eval(argv[4]) QA = QueryAns() QA.noans_num = noans_num try: process = Login_courses_by_request(logindata, mode, rate) # process = Login_courses_by_chrome(logindata,mode,rate) #备用登录选项 process.work() except SystemExit: print(COLOR.NOTE, "QUIT!", COLOR.END) except: try: print(format_exc()) send_err(format_exc()) except: print(COLOR.ERR, "ERROR! QUIT!", COLOR.END)
def _ans_question(self): # 点击章节测验 #action_chains = ActionChains(self.driver) self.driver.switch_to.default_content() sleep(2) try: bt = self.driver.find_element_by_xpath( '//div[@class="left"]/div/div[@class="main"]/div[@class="tabtags"]/span[@title="章节测验"]' ) except KeyboardInterrupt: raise KeyboardInterrupt except: try: bt = self.driver.find_element_by_xpath( '//div[@class="left"]/div/div[@class="main"]/div[@class="tabtags"]/span[last()]' ) except KeyboardInterrupt: raise KeyboardInterrupt except: # 还可能没有标签页 pass try: sleep(3) self.driver.execute_script("arguments[0].click();", bt) # action_chains.move_to_element(bt) # bt.click() except KeyboardInterrupt: raise KeyboardInterrupt except: pass #print(6, end=" ") wait = WebDriverWait(self.driver, 30) try: # 进入答题界面 wait.until( EC.presence_of_element_located((By.XPATH, '//iframe[1]'))) iframe = self.driver.find_element_by_xpath('//iframe[1]') self.driver.switch_to.frame(iframe) print(COLOR.NOTE + ' now go to question ' + COLOR.END, file=self._out_fp) #log_fp.write(' now go to question \n') #print(7, end=" ") except KeyboardInterrupt: raise KeyboardInterrupt except: print(COLOR.NOTE, ' no questions,continue~', COLOR.END, file=self._out_fp) # 未找到章节测验 #log_fp.write(' no questions,continue~\n') return 0 # 多任务点处理 for i in range(3): try: task_num = self.driver.execute_script( "window.scrollTo(0,document.body.scrollHeight);return document.getElementsByClassName('ans-job-icon').length" ) except KeyboardInterrupt: raise KeyboardInterrupt except: sleep(1) # task_num = self.driver.execute_script( # "return document.getElementsByClassName('ans-job-icon').length") try: self.driver.execute_script("window.scrollTo(0,0)") except KeyboardInterrupt: raise KeyboardInterrupt except: pass wait.until( EC.presence_of_all_elements_located( (By.XPATH, '//div[@class="ans-cc"]'))) for i in range(3): try: ans_cc = self.driver.find_element_by_xpath( '//div[@class="ans-cc"]') h5_text = ans_cc.get_attribute('innerHTML') except KeyboardInterrupt: raise KeyboardInterrupt except: sleep(1) task_road = PlayMedia.get_road(h5_text, task_num) # bs4处理得到各个任务点路径 print(COLOR.DISPLAY, ' there are ' + str(task_num) + ' task in this section:', COLOR.END, file=self._out_fp) #log_fp.write(' there are ' + str(task_num) + ' task in this section:\n') first_road = '//div[@class="ans-cc"]' for v_num in range(1, task_num + 1): print(COLOR.DISPLAY, ' go ' + str(v_num) + ':', COLOR.END, file=self._out_fp) #log_fp.write(' go ' + str(v_num) + ':\n') sleep(2) try: # 查看是否有任务点标识并查看是或否已经完成该任务点 #flag = self.driver.find_element_by_xpath('//div[@class="ans-cc"]/p['+str(p_index[v_num-1])+']/div') # print(first_road+video_road[v_num-1]) flag = self.driver.find_element_by_xpath(first_road + task_road[v_num - 1]) self.driver.execute_script("arguments[0].scrollIntoView();", flag) #icon_flag = 1 nowflag = flag.get_attribute('class') #print(nowflag, end=" ") if 'finished' in nowflag: print(COLOR.OK + ' Well! the task is already finished! continue~' + COLOR.END, file=self._out_fp) #log_fp.write(' Well! the task is already finished! continue~' + '\n') continue except KeyboardInterrupt: raise KeyboardInterrupt except: pass # icon_flag = 0 # 有的无任务点标识 try: wait.until( EC.presence_of_element_located( (By.XPATH, first_road + task_road[v_num - 1] + '/iframe[1]'))) iframe = self.driver.find_element_by_xpath(first_road + task_road[v_num - 1] + '/iframe[1]') self.driver.switch_to.frame(iframe) wait.until( EC.presence_of_element_located((By.XPATH, '//iframe[1]'))) iframe = self.driver.find_element_by_xpath('//iframe[1]') self.driver.switch_to.frame(iframe) except KeyboardInterrupt: raise KeyboardInterrupt except: print(COLOR.NOTE, ' no questions,continue~', COLOR.END, file=self._out_fp) # 未找到章节测验 #log_fp.write(' no questions,continue~\n') self.driver.switch_to.default_content() wait.until( EC.presence_of_element_located((By.XPATH, '//iframe[1]'))) iframe = self.driver.find_element_by_xpath('//iframe[1]') self.driver.switch_to.frame(iframe) continue # print(self.driver.page_source) sleep(3) # 查询并获取答案 data = { 'courseId': '', 'classId': '', # 'oldWorkId': '', # 'workRelationId': '' } try: for key in data.keys(): data[key] = self.driver.execute_script( 'return document.getElementById(arguments[0]).value', key) sleep(0.1) self.courseID = data['courseId'] + ' ' + data['classId'] except KeyboardInterrupt: raise KeyboardInterrupt except: self.courseID = "" QA = QueryAns(self.driver.page_source, course=self.course_name, courseID=self.courseID) ans_flag, ans_lt = QA.work() # print(ans_flag) # print(ans_lt) # sleep(20) # print(ans_lt) # 开始答题 # //*[@id="ZyBottom"]/div/div[1]/div # //*[@id="ZyBottom"]/div/div[2]/ul/li[1]/label/input # //*[@id="ZyBottom"]/div/div[2]/ul/li[2]/label/input # //*[@id="ZyBottom"]/div/div[4]/div[2]/ul/li[1]/label/input # //*[@id="ZyBottom"]/div/div[4]/div[4]/div[2]/ul/li[1]/label/input # //*[@id="ZyBottom"]/div/div[4]/div[4]/div[4]/div[2]/div/ul/li[1]/label/input try: for i in range(0, len(ans_lt)): for j in range(0, len(ans_lt[i])): if ans_lt[i][j] == 0: continue try: # print(i,j,ans_lt[i][j]) radio = self.driver.find_element_by_xpath( '//*[@id="ZyBottom"]/div' + '/div[4]' * i + '/div[2]/ul/li[' + str(ans_lt[i][j]) + ']/label/input') except KeyboardInterrupt: raise KeyboardInterrupt except: radio = self.driver.find_element_by_xpath( '//*[@id="ZyBottom"]/div' + '/div[4]' * i + '/div[2]/div/ul/li[' + str(ans_lt[i][j]) + ']/label/input') self.driver.execute_script( "arguments[0].scrollIntoView();arguments[0].click();", radio) # action_chains.move_to_element(radio) # radio.click() sleep(1) except KeyboardInterrupt: raise KeyboardInterrupt except: #print('==========', file=self._out_fp) #print(traceback.format_exc(), file=self._out_fp) print(COLOR.ERR, " 答题失败!", COLOR.END, file=self._out_fp) #log_fp.write(" 答题失败!" + '\n') sleep(5) return 1 #str(self._chapter) + '-' + str(self._section) # 点击提交并确定,检测验证码 # //*[@id="tempsave"] # //*[@id="ZyBottom"]/div/div[4]/div[4]/div[4]/div[5]/a[1] # //*[@id="ZyBottom"]/div/div[4]/div[4]/div[4]/div[5]/a[2] # //*[@id="ZyBottom"]/div/div[4]/div[4]/div[4]/div[4]/div[5]/a[2] # //*[@id="ZyBottom"]/div[2]/a[2]/span try: bn = self.driver.find_element_by_xpath( '//*[@id="ZyBottom"]/div' + '/div[4]' * (len(ans_lt) - 2) + '/div[5]/a[' + str(ans_flag) + ']') # 多个题目 except KeyboardInterrupt: raise KeyboardInterrupt except: bn = self.driver.find_element_by_xpath( '//*[@id="ZyBottom"]/div[2]/a[' + str(ans_flag) + ']') # 只有一个题 # action_chains.move_to_element(bn) # bn.click() self.driver.execute_script( "arguments[0].scrollIntoView();arguments[0].click();", bn) sleep(1) try: # 提交验证码 self.driver.switch_to.default_content() while 1: img = self.driver.find_element_by_id('imgVerCode') img.screenshot('ans_vercode.png') if SYSTEM == 0: img = Image.open('ans_vercode.png') img.show() else: p = Popen(['./src/viu', 'ans_vercode.png']) p.communicate() sleep(1.5) numVerCode = input(COLOR.NOTE + " please input the ans_vercode:" + COLOR.END, file=self._out_fp) #log_fp.write(' input the ans_vercode\n') # self.driver.find_element_by_id('code').send_keys(numVerCode) self.driver.find_element_by_xpath( '//input[@id="code"]').send_keys(numVerCode) self.driver.find_element_by_xpath('//a[@id="sub"]').click() sleep(1) except KeyboardInterrupt: raise KeyboardInterrupt except: wait.until( EC.presence_of_element_located((By.XPATH, '//iframe[1]'))) iframe = self.driver.find_element_by_xpath('//iframe[1]') self.driver.switch_to.frame(iframe) wait.until( EC.presence_of_element_located( (By.XPATH, first_road + task_road[v_num - 1] + '/iframe[1]'))) iframe = self.driver.find_element_by_xpath(first_road + task_road[v_num - 1] + '/iframe[1]') self.driver.switch_to.frame(iframe) wait.until( EC.presence_of_element_located((By.XPATH, '//iframe[1]'))) iframe = self.driver.find_element_by_xpath('//iframe[1]') self.driver.switch_to.frame(iframe) # //*[@id="confirmSubWin"]/div/div/a[1] wait.until( EC.presence_of_element_located( (By.XPATH, '//*[@id="confirmSubWin"]/div/div/a[1]'))) bn = self.driver.find_element_by_xpath( '//*[@id="confirmSubWin"]/div/div/a[1]') # action_chains.move_to_element(bn) try: # bn.click() self.driver.execute_script("arguments[0].click();", bn) print(COLOR.OK, 'questions of the section is finished! continue~', COLOR.END, file=self._out_fp) #log_fp.write(' finish the questions ' + '\n') except KeyboardInterrupt: raise KeyboardInterrupt except: #print('=======', file=self._out_fp) #print(traceback.format_exc(), file=self._out_fp) send_err(traceback.format_exc()) print(COLOR.ERR, " 提交失败!", COLOR.END, file=self._out_fp) return 1 self.driver.switch_to.parent_frame() self.driver.switch_to.parent_frame() sleep(5) # self.driver.switch_to_alert().accept() # if(len(err_lt)==0): return 0