import time
from selenium.webdriver import Chrome
web = Chrome()
web.get('https://mooc2-ans.chaoxing.co...')
1. 登录
phone = web.find_element_by_class_name('ipt-tel')
pwd = web.find_element_by_class_name('ipt-pwd')
login = web.find_element_by_class_name('btn-big-blue')
phone.send_keys('手机号码')
pwd.send_keys('明码')
login.click()
time.sleep(2)
实现一个页面的所有未实现的知识点
def view_one_page_points():
while (1): iframe = web.find_element_by_id('iframe') # 每次刷新后,都要进入外部iframe web.switch_to.frame(iframe) # 筛选,去除已实现的知识点 k_points = web.find_elements_by_css_selector('div.ans-attach-ct:not(.ans-job-finished)') num = len(k_points) flag = False for i in range(0, num): if i == (num - 1): # 是最初一个,示意该页刷完 flag = True k_point = k_points[i] # 查找工作图标个数,1为知识点,0为不是知识点 icon_num = len(k_point.find_elements_by_xpath('./div[contains(@class,"ans-job-icon")]')) if icon_num == 0: # 再次筛选,去除不是知识点的div continue # 进行视频的播放 video_iframe = k_point.find_element_by_xpath('./iframe') # 视频iframe print(video_iframe) time.sleep(2) web.switch_to.frame(video_iframe) # 进入视频iframe time.sleep(2) web.find_element_by_class_name('vjs-big-play-button').click() # 点击播放按钮 time.sleep(1) web.find_element_by_xpath('//*[@id="video"]/div[5]/div[6]/button').click() # 静音 # 播放和暂停按钮 pause_btn = web.find_element_by_xpath('//button[contains(@class,"vjs-play-control")and ' 'contains(@class,"vjs-control")and contains(@class,"vjs-button")]') while (1): # 播放期待 time.sleep(1) # 每1秒,查看视频是否播放结束 if (pause_btn.get_attribute('title') == "重播"): # 点击后播放,即播放结束状态 break print('视频播放结束') # 视频播放结束,退出播放iframe,而后退出循环,再次查找该页面的所有知识点(k_points) web.switch_to.default_content() print('退出知识点iframe') time.sleep(2) web.refresh() # 刷新页面,之后需从新进入iframe time.sleep(2) print('刷新页面') break if flag: # 该页面知识点播放结束 breakpass
while (1):
# 进入iframeframe_content = web.find_element_by_xpath('//*[@id="frame_content-zj"]')web.switch_to.frame(frame_content)time.sleep(2)# 查找所有未实现的知识点页面all_no_list = web.find_elements_by_xpath('//span[@class="catalog_points_yi"]')list_num = len(all_no_list) #知识点页面个数if list_num == 0: # 没有知识点页面,即全副刷完 break# 跳转到第一个知识点页面all_no_list[0].click()web.switch_to.window(web.window_handles[-1]) # 跳转到该知识点界面time.sleep(5)#####view_one_page_points() # 播放该知识点页面的所有未实现的知识点视频#####print('实现一个知识点页面...')web.close() # [Skrill下载](https://www.gendan5.com/wallet/Skrill.html)敞开以后窗口# 该页面知识点结束,敞开以后窗口,抉择下一个知识点窗口web.switch_to.window(web.window_handles[0]) # 变更视角到该课程主界面time.sleep(1)# 刷新页面web.refresh()time.sleep(2)print('刷新主页面')pass