2121import re
2222from time import sleep
2323import time
24+ from sys import argv
2425
2526
2627class color :
@@ -138,8 +139,7 @@ def choose_course(driver):
138139 iframe = driver .find_element_by_xpath ('//iframe' )
139140 driver .switch_to .frame (iframe )
140141 wait .until (EC .presence_of_element_located ((By .CSS_SELECTOR , '.ulDiv' )))
141- courses_num = len (
142- driver .find_elements_by_xpath ('//div[@class="ulDiv"]/ul/li' ))
142+ courses_num = len (driver .find_elements_by_xpath ('//div[@class="ulDiv"]/ul/li' ))
143143 courses_lt = []
144144 valid_i = 1
145145 for i in range (1 , courses_num ):
@@ -288,30 +288,26 @@ def play_video(driver, menu_url):
288288 try :
289289 wait .until (EC .presence_of_element_located (
290290 (By .XPATH , '//div[@class="left"]/div/div[@class="main"]/div[@class="tabtags"]' )))
291- except :
292- print (COLOR .NOTE , ' no videos,continue~' , COLOR .END )
293- log_fp .write (' no videos,continue~\n ' )
294- return
295-
296- try :
297- bt = driver .find_element_by_xpath (
298- '//div[@class="left"]/div/div[@class="main"]/div[@class="tabtags"]/span[@title="视频"]' )
299- except :
300291 try :
301292 bt = driver .find_element_by_xpath (
302- '//div[@class="left"]/div/div[@class="main"]/div[@class="tabtags"]/span[@title="视频 "]' )
293+ '//div[@class="left"]/div/div[@class="main"]/div[@class="tabtags"]/span[@title="视频"]' )
303294 except :
304- try : # 无标题视频
295+ try :
305296 bt = driver .find_element_by_xpath (
306- '//div[@class="left"]/div/div[@class="main"]/div[@class="tabtags"]/span[last()-1 ]' )
297+ '//div[@class="left"]/div/div[@class="main"]/div[@class="tabtags"]/span[@title="视频 " ]' )
307298 except :
308- pass
309- sleep (7 )
310- try :
299+ try : # 无标题视频
300+ bt = driver .find_element_by_xpath (
301+ '//div[@class="left"]/div/div[@class="main"]/div[@class="tabtags"]/span[@title="微课"]' )
302+ except :
303+ bt = driver .find_element_by_xpath (
304+ '//div[@class="left"]/div/div[@class="main"]/div[@class="tabtags"]/span[last()-1]' )
305+ sleep (7 )
311306 action_chains .move_to_element (bt )
312307 bt .click ()
313- except :
308+ except :#可能会有没有标签栏的情况
314309 pass
310+
315311 # action_chains.click_and_hold(bt)
316312 # action_chains.release(bt)
317313
@@ -359,7 +355,7 @@ def play_video(driver, menu_url):
359355 log_fp .write (' go ' + str (v_num ) + ':\n ' )
360356 driver .execute_script ("window.scrollTo(0,arguments[0])" , 400 + 700 * (v_num - 1 ))
361357 sleep (2 )
362- try :
358+ try :#查看是否有任务点标识并查看是或否已经完成该任务点
363359 #flag = driver.find_element_by_xpath('//div[@class="ans-cc"]/p['+str(p_index[v_num-1])+']/div')
364360 # print(first_road+video_road[v_num-1])
365361 flag = driver .find_element_by_xpath (first_road + video_road [v_num - 1 ])
@@ -384,16 +380,22 @@ def play_video(driver, menu_url):
384380 iframe = driver .find_element_by_xpath (first_road + video_road [v_num - 1 ] + '/iframe' )
385381 driver .switch_to .frame (iframe )
386382
387- wait .until (EC .presence_of_element_located (
388- (By .XPATH , '//div[@id="reader"]/div/button[@class="vjs-big-play-button"]' )))
389- bt = driver .find_element_by_xpath ('//div[@id="reader"]/div/button[@class="vjs-big-play-button"]' )
390- action_chains .move_to_element_with_offset (bt , 0 , 0 )
391- # action_chains.click(bt)
392- sleep (5 )
393- bt .click () # 点击播放
394- print (COLOR .OK + ' start play ' + COLOR .END )
395- log_fp .write (' start play \n ' )
396- flags .flag2 = 0
383+ try :
384+ wait .until (EC .presence_of_element_located (
385+ (By .XPATH , '//div[@id="reader"]/div/button[@class="vjs-big-play-button"]' )))
386+ bt = driver .find_element_by_xpath ('//div[@id="reader"]/div/button[@class="vjs-big-play-button"]' )
387+ action_chains .move_to_element_with_offset (bt , 0 , 0 )
388+ # action_chains.click(bt)
389+ sleep (5 )
390+ bt .click () # 点击播放
391+ print (COLOR .OK + ' start play ' + COLOR .END )
392+ log_fp .write (' start play \n ' )
393+ flags .flag2 = 0
394+ except :
395+ driver .switch_to .parent_frame ()
396+ print (COLOR .DISPLAY + ' this is not a video, go ahead!' + COLOR .END )
397+ log_fp .write (" this is not a video, go ahead!\n " )
398+ continue
397399 except :
398400 # 找的到视频但无法播放
399401 log_fp .write (' failed to start,Retry \n ' )
@@ -697,10 +699,15 @@ def ans_question(driver, course_name):
697699 log_fp .write (" 答题失败!" + '\n ' )
698700 return str (flags .chapter ) + '-' + str (flags .section )
699701
700- for i in range (2 ):
701- wait .until (EC .presence_of_element_located ((By .XPATH , '//iframe[1]' )))
702- iframe = driver .find_element_by_xpath ('//iframe[1]' )
703- driver .switch_to .frame (iframe )
702+ try :
703+ for i in range (2 ):
704+ wait .until (EC .presence_of_element_located ((By .XPATH , '//iframe[1]' )))
705+ iframe = driver .find_element_by_xpath ('//iframe[1]' )
706+ driver .switch_to .frame (iframe )
707+ except :
708+ print (COLOR .NOTE , ' no questions,continue~' , COLOR .END ) # 未找到章节测验
709+ log_fp .write (' no questions,continue~\n ' )
710+ return 0
704711 # print(driver.page_source)
705712 sleep (3 )
706713
@@ -782,74 +789,83 @@ def ans_question(driver, course_name):
782789
783790 return 0
784791
785-
786- def perform (driver , menu_url , course_name ):
787- while 1 :
788- pattern = eval (input (COLOR .NOTE + "please select the pattern(0 is auto,1 is manual):" + COLOR .END ))
789- if pattern in [0 , 1 ]:
790- break
792+ def perform_model0 (driver ,menu_url ,course_name ):
791793 ch_se_lt = get_chapter_section (driver , menu_url )
792794 print (COLOR .WARN , '未完成任务章节:' , COLOR .END , str (ch_se_lt ))
793795 log_fp .write ('未完成任务章节:' + str (ch_se_lt ) + '\n ' )
794796 error_lt = []
795797 last_time = time .time ()- 300
796- if pattern == 1 :
797- chapter = eval ( input ( COLOR . NOTE + "please select which chapter:" + COLOR . END ))
798- section = eval ( input ( COLOR . NOTE + "please select which section:" + COLOR . END ))
799- subsection = eval ( input ( COLOR . NOTE + "please select which subsection(if not input 0):" + COLOR . END ))
800- flags .getvalue ( chapter , section , subsection , 0 , 0 , pattern )
798+ flags . getvalue ( 0 , 0 , 0 , 0 , 0 , pattern )
799+ for ch_se in ch_se_lt :
800+ flags . chapter = ch_se [ 0 ]
801+ flags . section = ch_se [ 1 ]
802+ flags .subsection = ch_se [ 2 ] if len ( ch_se ) == 3 else 0
801803 while 1 :
802804 play_video (driver , menu_url )
803805 if flags .flag > 0 or flags .flag2 > 0 :
804806 print (COLOR .ERR , 'unfinished! Retry…' , COLOR .END )
805- continue
806- elif flags .end > 0 :
807- flags .section += 1
808- continue
809- now_time = time .time ()
810- #print(now_time-last_time)
811- if now_time - last_time < 300 :
812- sleep (300 - (now_time - last_time ))
813- last_time = time .time ()
814- if flags .end == 2 :
815- print (COLOR .OK , 'finish the lesson! quit! ' , COLOR .END )
816- err_section = ans_question (driver , course_name )
817- if err_section != 0 :
818- print (COLOR .ERR , 'unfinished!' , COLOR .END )
819- error_lt .append (err_section ) # 记录答题提交失败的章节
820807 else :
821- print (COLOR .OK , 'finished!' , COLOR .END )
822- flags .section += 1
823- else :
824- flags .getvalue (0 , 0 , 0 , 0 , 0 , pattern )
825- for ch_se in ch_se_lt :
826- flags .chapter = ch_se [0 ]
827- flags .section = ch_se [1 ]
828- flags .subsection = ch_se [2 ] if len (ch_se ) == 3 else 0
829- while 1 :
830- play_video (driver , menu_url )
831- if flags .flag > 0 or flags .flag2 > 0 :
832- print (COLOR .ERR , 'unfinished! Retry…' , COLOR .END )
833- else :
834- break
835-
836- now_time = time .time ()
837- #print(now_time-last_time)
838- if now_time - last_time < 300 :
839- sleep (300 - (now_time - last_time ))
840- last_time = time .time ()
841-
842- err_section = ans_question (driver , course_name )
843- if err_section != 0 :
844- print (COLOR .ERR , 'unfinished!' , COLOR .END )
845- error_lt .append (err_section ) # 记录答题提交失败的章节
846- else :
847- print (COLOR .OK , 'finished!' , COLOR .END )
848- print (COLOR .OK , 'finish the lesson! quit! ' , COLOR .END )
808+ break
809+
810+ now_time = time .time ()
811+ #print(now_time-last_time)
812+ if now_time - last_time < 300 :
813+ sleep (300 - (now_time - last_time ))
814+ last_time = time .time ()
815+
816+ err_section = ans_question (driver , course_name )
817+ if err_section != 0 :
818+ print (COLOR .ERR , 'unfinished!' , COLOR .END )
819+ error_lt .append (err_section ) # 记录答题提交失败的章节
820+ else :
821+ print (COLOR .OK , 'finished!' , COLOR .END )
822+ print (COLOR .OK , 'finish the lesson! quit! ' , COLOR .END )
823+ log_fp .write ("err_lt:" + str (error_lt ) + '\n ' )
849824
825+
826+ def perform_model1 (driver ,menu_url ,course_name ):
827+ ch_se_lt = get_chapter_section (driver , menu_url )
828+ print (COLOR .WARN , '未完成任务章节:' , COLOR .END , str (ch_se_lt ))
829+ log_fp .write ('未完成任务章节:' + str (ch_se_lt ) + '\n ' )
830+ error_lt = []
831+ last_time = time .time ()- 300
832+ chapter = eval (input (COLOR .NOTE + "please select which chapter:" + COLOR .END ))
833+ section = eval (input (COLOR .NOTE + "please select which section:" + COLOR .END ))
834+ subsection = eval (input (COLOR .NOTE + "please select which subsection(if not input 0):" + COLOR .END ))
835+ flags .getvalue (chapter , section , subsection , 0 , 0 , pattern )
836+ while 1 :
837+ play_video (driver , menu_url )
838+ if flags .flag > 0 or flags .flag2 > 0 :
839+ print (COLOR .ERR , 'unfinished! Retry…' , COLOR .END )
840+ continue
841+ elif flags .end > 0 :
842+ flags .section += 1
843+ continue
844+ now_time = time .time ()
845+ #print(now_time-last_time)
846+ if now_time - last_time < 300 :
847+ sleep (300 - (now_time - last_time ))
848+ last_time = time .time ()
849+ if flags .end == 2 :
850+ print (COLOR .OK , 'finish the lesson! quit! ' , COLOR .END )
851+ err_section = ans_question (driver , course_name )
852+ if err_section != 0 :
853+ print (COLOR .ERR , 'unfinished!' , COLOR .END )
854+ error_lt .append (err_section ) # 记录答题提交失败的章节
855+ else :
856+ print (COLOR .OK , 'finished!' , COLOR .END )
857+ flags .section += 1
850858 log_fp .write ("err_lt:" + str (error_lt ) + '\n ' )
851- driver .quit ()
852859
860+ def perform (driver , menu_url , course_name ):
861+ while 1 :
862+ pattern = eval (input (COLOR .NOTE + "please select the pattern(0 is auto,1 is manual):" + COLOR .END ))
863+ if pattern in [0 , 1 ]:
864+ break
865+ if pattern == 1 :
866+ perform_model1 (driver ,menu_url ,course_name )#debug模式
867+ else :
868+ perform_model0 (driver ,menu_url ,course_name )#auto模式
853869
854870def main ():
855871 driver = init ()
@@ -858,11 +874,14 @@ def main():
858874 # print(driver.current_url)
859875 ret = choose_course (driver )
860876 perform (driver , ret [0 ], ret [1 ])
877+ driver .quit ()
861878
862879
880+ COLOR = color ()
881+ flags = FLAG ()
863882if __name__ == "__main__" :
864- COLOR = color ()
865- flags = FLAG ()
866883 log_fp = open (r'./chaoxing.txt' , 'a+' , encoding = 'utf-8' )
867884 main ()
868885 log_fp .close ()
886+ else :
887+ log_fp = open (r'./chaoxing.txt' , 'a+' , encoding = 'utf-8' )
0 commit comments