老师要求每挂满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_hand