江门android培训,基于selenium模块的江门市干部培训网络学院自动选课脚本
[Python] 純文本查看 復(fù)制代碼# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
import time
import re
import random
from time import sleep
driver = webdriver.Chrome(executable_path="chromedriver.exe")
def login(username,passwd):
nickName=driver.find_element_by_id('nickName')
sourcePassword=driver.find_element_by_id('sourcePassword')
login_btn=driver.find_element_by_id('login_btn')
nickName.send_keys(username)
sourcePassword.send_keys(passwd)
time.sleep(10)
login_btn.click()
time.sleep(random.uniform(2,5))
try:
driver.find_element_by_class_name("layui-layer-btn0").click()
except:
print('是否登錄成功,若沒成功登陸,請重新運(yùn)行')
def course_need_Add(course_need_add):
course_need_add.click()
time.sleep(random.uniform(2,3))
course_need_add = driver.find_element_by_xpath('/html/body/div[3]/div[2]/div[1]/div[1]/div[5]/a') # 詳情頁的選課
course_need_add.click()
time.sleep(random.uniform(1,3))
driver.find_element_by_xpath('/html/body/div[6]/div[3]/a[1]').click()
time.sleep(random.uniform(1,3))
driver.find_element_by_xpath('/html/body/div[7]/div[3]/a').click()
driver.back()
return print('選課成功')
base_url = "https://gbpx.jiangmen.cn/"
username=input("請輸入賬號(hào):")
passwd= input("請輸入密碼:")
print('請?jiān)跒g覽器網(wǎng)頁登錄界面賬號(hào)密碼自動(dòng)填充后15秒內(nèi)輸入驗(yàn)證碼')
driver.get(base_url)
driver.implicitly_wait(10)
now_window = driver.current_window_handle
login(username,passwd)
driver.implicitly_wait(15)
driver.find_element_by_id('navLesson').click()#進(jìn)入課程中心
driver.find_element_by_xpath('/html/body/div[3]/div[2]/div[3]/div/div[2]/div[2]/div/div/a[6]').click()#點(diǎn)擊尾頁
time.sleep(3)
pageTotal=driver.find_element_by_xpath('/html/body/div[3]/div[2]/div[3]/div/div[2]/div[2]/div/div/a[6]').text#獲取總頁數(shù)
print ("總共有"+pageTotal+"頁課程")
pageTotal=int(pageTotal)
page_start=input("請輸入開始選課的頁數(shù)(純數(shù)字):")
page_end=input("請輸入開始選課的頁數(shù)(純數(shù)字,不得大于總頁數(shù)):")
for page in range(page_start,page_end):
number = driver.find_element_by_xpath('//*[@class="layui-input"]')
number.clear()
number.send_keys(page)
driver.find_element_by_xpath('//*[@class="layui-laypage-btn"]').click()
driver.implicitly_wait(10)
print ("第"+str(page)+"頁")
for i in range(1,11):#對單個(gè)列表內(nèi)單個(gè)課程信息進(jìn)行判斷是否需要繼續(xù)學(xué)習(xí)。
xpath='/html/body/div[3]/div[2]/div[3]/div/div[2]/table/tbody/tr[{}]/td[5]/a'
couse_name='/html/body/div[3]/div[2]/div[3]/div/div[2]/table/tbody/tr[{}]/td[1]/a'
couse_name=couse_name.format(i)
xpath=xpath.format(i)
print(xpath)
time.sleep(random.uniform(1,3))
course_need_add = driver.find_element_by_xpath(xpath)
couse_name = driver.find_element_by_xpath(couse_name).text
print(course_need_add.text,couse_name)
if course_need_add.text == '已選':
i = i + 1
else:
course_need_Add(course_need_add)
time.sleep(random.uniform(1,3))
number = driver.find_element_by_xpath('//*[@class="layui-input"]')
number.clear()
number.send_keys(page)
driver.find_element_by_xpath('//*[@class="layui-laypage-btn"]').click()
time.sleep(5)
print("第" + str(page) + "頁")
總結(jié)
以上是生活随笔為你收集整理的江门android培训,基于selenium模块的江门市干部培训网络学院自动选课脚本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 模拟百度翻译
- 下一篇: opencv大津算法二值化