selenium之自动选课脚本

来源:互联网 发布:阿里云 自建机房 编辑:程序博客网 时间:2024/05/18 22:51

概述

  此脚本是针对四川大学本科教务系统开发的自动化选课脚本, 方便学生选课。 对其它网站无效, 但可以作为参考。


在脚本中写入想要选择的课程号与课序号, 以及用户名和密码, 即可不断自动刷课直到选课成功为止。


代码实现

#coding:utf-8from selenium import webdriverimport timebrowser=webdriver.Chrome() #浏览器kch = "311084030"   #想要选择的课程号kxh = "01"  #想要选择的课序号def isElementExist(_browser, _string, _type):flag=Truebrowser=_browsertry:if _type == 'name':browser.find_element_by_name(_string)elif _type == 'xpath':browser.find_element_by_xpath(_string)elif _type == 'text':browser.find_element_by_link_text(_string)return flagexcept:flag=Falsereturn flag            def phase1():#打开网址,并登录browser.get("http://202.115.47.141/login.jsp")s1 = 'username' #此处为用户名s2 = 'password' #此处为密码while False == isElementExist(browser, "zjh", "name"):            #若打开失败则重新打开browser.get("http://202.115.47.141/login.jsp")browser.find_element_by_name("zjh").send_keys(s1)browser.find_element_by_name("mm").send_keys(s2)browser.find_element_by_id("btnSure").click()phase2()def phase2():#打开选课管理界面path = "//*[@id='divCoHome']/table/tbody/tr/td[2]/table/tbody/tr[1]//*[@class='hometopBg1']/tbody/tr/td/a"browser.switch_to_default_content()browser.switch_to_frame("bottomFrame")browser.switch_to_frame("mainFrame")while False == isElementExist(browser, path, "xpath"):#失败刷新页面, 回退到phase1browser.refresh()phase1()browser.find_element_by_xpath(path).click()phase3()def phase3():#打开选课方案界面browser.switch_to_default_content()browser.switch_to_frame("bottomFrame")browser.switch_to_frame("menuFrame")path = "//*[@id='Xkgl']//*[@class='content2']/tbody/tr[2]/td/a"while False == isElementExist(browser, path, "xpath"):     #失败刷新页面, 回退到phase1browser.refresh()phase1()browser.find_element_by_xpath(path).click()phase4()def phase4():#搜索课程browser.switch_to_default_content()browser.switch_to_frame("bottomFrame")browser.switch_to_frame("mainFrame")text = '自由选择'while False == isElementExist(browser, text, "text"):     #失败刷新页面, 回退到phase2browser.refresh()phase2()browser.find_element_by_link_text(text).click()browser.switch_to_frame("inforUpContent")while False == isElementExist(browser, "kch", "name"):     #失败刷新页面, 回退到phase2browser.refresh()phase2()browser.find_element_by_name("kch").send_keys(kch)browser.switch_to.parent_frame()browser.find_element_by_xpath("//*[@title='确定']").click()phase5()def phase5():#选择课程browser.switch_to_frame("inforUpContent")value = kch+'_'+kxhpath = "//*[@value='%s']"%valuewhile False == isElementExist(browser, path, "xpath"):     #失败刷新页面, 回退到phase2browser.refresh()phase2()browser.find_element_by_xpath(path).click()browser.switch_to.parent_frame()browser.find_element_by_xpath("//*[@title='确定']").click()def main(): phase1()#等待用户输入退出waitkey=raw_input()browser.quit()if __name__ == '__main__':    main()


0 0
原创粉丝点击