網(wǎng)課很多看不完呀
所以動(dòng)手做了一個(gè)基礎(chǔ)的自動(dòng)答題和下一節(jié)的程序
用到了python 3
selenium
Chrome
如何自動(dòng)化Chrome?https://www.cnblogs.com/eternal1025/p/8880245.html
配置好之后上代碼
源碼下載地址
https://github.com/zlaiyyf/ChaoXIing_seleniumm時(shí)間緊張所以嘛bug就。。。。
coding:utf-8from selenium import webdriver# 顯示等待from selenium.webdriver.support.wait import WebDriverWait# 啟動(dòng)參數(shù)from selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.common.exceptions import UnexpectedAlertPresentException,TimeoutExceptionfrom scrip import clickimport timeclass Chaoxing(): def __init__(self,password,username): """ 暫時(shí)不考慮其他學(xué)校的 :param password: :param username: """ chrome_opt = Options() # 創(chuàng)建參數(shù)設(shè)置對(duì)象. # chrome_opt.add_argument('–start-maximized') # 設(shè)置瀏覽器窗口大小. chrome_opt.add_argument('--disable-infobars') # chrome_opt.add_argument("-–start-maximized") chrome_opt.add_experimental_option('excludeSwitches', ['enable-automation']) self.browser = webdriver.Chrome(chrome_options=chrome_opt) self.username = username self.password = password def land(self): """ 登陸 :return: """ self.browser.get('http://sxu.fanya.chaoxing.com/portal') # self.browser.find_element_by_xpath('//input[@value= "登錄"]').click() WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath('//input[@value= "登錄"]')).click() # ActionChains(driver).click(click_btn) WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath('//input[@id="unameId"]')).send_keys(self.username) WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath('//input[@id="passwordId"]')).send_keys(self.password) print('輸入賬號(hào)完成{}'.format(self.username)) time.sleep(10) WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath('//input[@value= "登錄"]')).click() def find_course(self): """ 發(fā)現(xiàn)課程 :return: """ self.browser.switch_to.frame('frame_content') self.browser.find_elements_by_xpath('//li[@style="position:relative"]') course_name = self.browser.find_elements_by_xpath('//h3[@class="clearfix"]') click.click_couse(course_name,'創(chuàng)業(yè)創(chuàng)新領(lǐng)導(dǎo)力') windows = self.browser.window_handles self.browser.switch_to.window(windows[-1]) self.couse() def couse(self): """ 進(jìn)入課程 :return: """ class_num = -1 while True: time.sleep(2) class_num = class_num + 1 class_name_list = self.browser.find_elements_by_xpath('//div[@class="leveltwo"]') if class_num == len(class_name_list): break else: print(class_num) class_name_num = self.browser.find_elements_by_xpath('//div[@class="leveltwo"]')[class_num].text print(class_name_num.split('\n')) if '1' == class_name_num.split('\n')[1]: continue class_name_tag = self.browser.find_elements_by_xpath('//span[@class="articlename"]')[class_num] class_name = class_name_tag.text class_name_tag.click() print('正在點(diǎn)擊{}'.format(class_name)) time.sleep(2) self.view(class_name=class_name) continue def view(self,class_name): """ 看視頻 :param class_name: :return: """ # try: self.browser.find_element_by_xpath('//span[@title="視頻"]').click() self.browser.switch_to.frame("iframe") time.sleep(5) self.browser.switch_to.frame(self.browser.find_element_by_xpath('//iframe[@class="ans-attach-online ans-insertvideo-online"]')) WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath('//div[@id="video"]')).click() view_tag = self.browser.find_element_by_xpath('//div[@id="video"]') ActionChains(self.browser).move_to_element(view_tag).perform() while True: time.sleep(2) if self.view_percentage() == '200' : self.browser.switch_to.default_content() self.browser.find_element_by_xpath('//a[contains(text(), "回到課程")]').click() break def view_percentage(self): """" 檢查是否看完 """ # total_duration = self.browser.find_element_by_xpath('//span[@class="vjs-duration-display"]').text # current_duration = self.browser.find_element_by_xpath('//span[@class="vjs-current-time-display"]').text view_percentage_tag = self.browser.find_element_by_xpath('//div[@class="vjs-play-progress vjs-slider-bar"]') view_percentage = view_percentage_tag.get_attribute('style') print('當(dāng)前進(jìn)度'+view_percentage) self.is_exist_problem() """" 檢查是否看完 """ if '100%' in view_percentage : return '200' def is_exist_problem(self): try: problem_tag_style = WebDriverWait(self.browser, 30, 0.2).until( lambda x: x.find_element_by_xpath('//div[@id="ext-comp-1035"]')).get_attribute('style') if problem_tag_style == 'overflow: auto;': print('有題目') input_tag_list = self.browser.find_elements_by_xpath('//input') for input_tag in input_tag_list: input_tag.click() self.browser.find_element_by_xpath('//div[@class="ans-videoquiz-submit"]').click() time.sleep(2) if EC.alert_is_present()(self.browser): self.browser.switch_to.alert.accept() else: break else: pass except UnexpectedAlertPresentException: print('alert出錯(cuò)') self.browser.switch_to.alert.accept() except TimeoutException: print('TimeoutException') pass
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。