老师要求每挂满150分钟的课程,然后就想着写一个脚本来挂学时,使用了selenium来模拟浏览器行为来写的一个挂网课脚本。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
uname = ''
paswd = ''
def open_brower():
b = webdriver.Chrome()
#打开经世优学网站
b.get('http://study.huatec.com/')
#设置等待时间直到找到元素
user = WebDriverWait(b,10).until(lambda b:b.find_element_by_id('username'))
user.clear()
user.send_keys(uname)
pwd = WebDriverWait(b,10).until(lambda b:b.find_element_by_id('password'))
pwd.clear()
pwd.send_keys(paswd)
#点击登录
b.find_element_by_class_name('btn-success').click()
time.sleep(10)
#用time模块来睡眠10S,让网站渲染出来
std_data = b.find_element_by_link_text('学习数据')
std_data.click()
#打开第一门课程
c1 = WebDriverWait(b,10).until(lambda b:b.find_element_by_xpath('//tbody/tr[1]/td[7]/div/a'))
c1.click()
#这个网站加载有点慢所以都加了睡眠时间等待网站渲染
time.sleep(8)
#把句柄切换到课程的句柄
try:
b.switch_to_window(b.window_handles[1])
except DeprecationWarning:
print('')
finally:
b.find_element_by_xpath('//*[@id="&courseId=00DCE5A9AE5343DAA5F4ACFA725A9BD0§ionId=6342E71EFA864996BD07AB5C99909948"]').click()
time.sleep(8)
#切回到学习数据页面然后打开第二门课程
b.switch_to_window(b.window_handles[0])
c2 = b.find_element_by_xpath('//tbody/tr[2]/td[7]/div/a').click()
time.sleep(8)
try:
b.switch_to_window(b.window_handles[3])
except DeprecationWarning:
print('')
finally:
b.find_element_by_xpath('//*[@id="&courseId=13F7C9A2967D487B9CE01B6B1BC019F7§ionId=FB9B94C89483419D8AE3FB94F5C57C03"]').click()
time.sleep(8)
b.switch_to_window(b.window_handles[0])
c3 = b.find_element_by_xpath('//tbody/tr[3]/td[7]/div/a').click()
time.sleep(8)
try:
b.switch_to_window(b.window_handles[5])
except DeprecationWarning:
print('')
finally:
b.find_element_by_xpath('//*[@id="https://80.cdn-vod.huaweicloud.com/asset/d7507eff0aff147b94325fca5e808086/play_video/index.m3u8&courseId=7AA7B6E81E504F8298E90E63B7FB3B20&coursewareId=D14267D5B196454E89300A8181972EDD&chapterId=3B9EEF77852E470F9F90739EAADCC862§ionId=71990A2C1EAF40A5BED2B7600DB05AAA&cwId=D14267D5B196454E89300A8181972EDD&videoType=2"]').click()
time.sleep(8)
b.switch_to_window(b.window_handles[0])
c4 = b.find_element_by_xpath('//tbody/tr[4]/td[7]/div/a').click()
time.sleep(8)
try:
b.switch_to_window(b.window_handles[7])
except DeprecationWarning:
print('')
finally:
b.find_element_by_xpath('//*[@id="&courseId=919F8863B29949F0A1EA99291D008A84§ionId=2184C73C6E554CD788B5306C213E3388"]').click()
if __name__ == '__main__':
open_brower()