時間緊任務重,沒辦法才出此下策,該學習還是要認真學習
from selenium import webdriver # 導入web自動化庫
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
# 指定chromedriver位置
web = webdriver.Chrome("c:\chromedriver.exe")
# 學習網站
url = "https://passport2.chaoxing.com/login?fid=&newversion=true&refer=http%3A%2F%2Fi.chaoxing.com"
web.get(url)
# 反檢查
option = Options()
option.add_argument('--disable-blink-features=AutomationControlled')
# 登錄進入網站
def login_first():
time.sleep(3)
# 發送用戶名密碼
web.find_element(By.XPATH, '//*[@id="phone"]').send_keys("賬號")
time.sleep(1)
web.find_element(By.XPATH, '//*[@id="pwd"]').send_keys("密碼")
# 點擊登錄
time.sleep(1)
web.find_element(By.XPATH, '//*[@id="loginBtn"]').click()
time.sleep(1)
# 進入課程
def into_course():
# iframe框架,需要先跳轉
web.switch_to.frame("frame_content")
time.sleep(3)
# 課程
course = web.find_element(By.XPATH, '/html/body/div/div/div/div/div/ul/li/span')
# 解決元素被遮擋點擊不了的問題
web.execute_script('arguments[0].click()', course)
time.sleep(3)
# 切換至新開窗口
web.switch_to.window(web.window_handles[-1])
# 進入章節
time.sleep(8)
web.find_element(By.XPATH, '//*[@id="vue-content"]/div[2]/div[2]/div[4]/div[2]/table/tbody/tr[1]/td[2]/div/span').click()
print('進入小學數學')
time.sleep(5)
# 切換至新開窗口
web.switch_to.window(web.window_handles[-1])
time.sleep(1)
# web.find_element(By.XPATH, '//*[@id="vue-content"]/div[2]/div[2]/div[2]/ul/li[2]/div[1]').click()
# print('進入二年級數學')
# 點擊年級課程圖片
web.find_element(By.XPATH, '//*[@id="vue-content"]/div[2]/div[2]/div[2]/ul/li[4]/div[1]').click()
print('進入四年級數學')
time.sleep(5)
web.switch_to.window(web.window_handles[-1])
# 播放視頻 點擊靜音 首次播放設置2倍速
def play_video(symbol, i):
print('播放第一個')
if symbol:
try:
print('準備播放')
# 進入最外層父frame
web.switch_to.default_content()
# 進入播放器frame
web.switch_to.frame("iframe")
web.switch_to.frame(0)
# 獲取播放器開始按鈕
paly_course = web.find_element(By.XPATH, "http://*[@id='video']/button")
# 模擬點擊按鈕
web.execute_script("arguments[0].click();", paly_course)
time.sleep(3)
# 首次播放設置2倍速
if i == 1:
# 獲取倍速按鈕
speed = web.find_element(By.XPATH, '//*[@id="video"]/div[5]/div[1]/button')
print('開啟2倍速')
# 2倍速點擊3次
web.execute_script('arguments[0].click();', speed)
time.sleep(3)
web.execute_script('arguments[0].click();', speed)
time.sleep(1)
web.execute_script('arguments[0].click();', speed)
time.sleep(1)
# 靜音
print('靜音播放')
# 獲取靜音按鈕
voice = web.find_element(By.XPATH, '//*[@id="video"]/div[5]/div[6]/button')
web.execute_script('arguments[0].click()', voice)
time.sleep(3)
print(f'開始播放第{i}個視頻')
except:
print(f"沒有第{i}個視頻")
# 循環判斷視頻是否播放完成,自動完成視頻中驗證題
def video_finished():
# 最后一次記錄的當前播放時間
video_stat_time_last=0
try:
while True:
time.sleep(3)
# 獲取當前播放時間和視頻總時長
video_stat_time = web.find_element(By.XPATH, '//*[@id="video"]/div[5]/div[2]/span[2]').get_attribute(
"textContent")
video_end_time = web.find_element(By.XPATH, '//*[@id="video"]/div[5]/div[4]/span[2]').get_attribute(
"textContent")
print("正在播放的時間和結束時間是:", video_stat_time, video_end_time)
# 循環檢測視頻是否完成的延時
time.sleep(4)
# 上一次獲取的當前播放時間和本次獲取的相同,即進入驗證題
if video_stat_time_last == video_stat_time:
print('驗證題')
# 獲取A選項,A選項為正確
radio = web.find_element(By.XPATH, '//*[@id="ext-gen1056"]/div/ul/li[1]/label')
web.execute_script('arguments[0].click()', radio)
time.sleep(3)
# 點擊回答完成
radio2 = web.find_element(By.XPATH, '//*[@id="videoquiz-submit"]')
web.execute_script('arguments[0].click()', radio2)
time.sleep(3)
# 點擊繼續學習
radio3 = web.find_element(By.XPATH, '//*[@id="videoquiz-continue"]')
web.execute_script('arguments[0].click()', radio3)
time.sleep(3)
# 賦值給最后一次播放時間
video_stat_time_last = video_stat_time
# 當前播放進度和視頻總時長相同,返回布爾
if video_end_time == video_stat_time:
print('視頻播放完成')
return 1
except:
print('視頻不可播放')
return 1
# 主函數
if __name__ == '__main__':
# 登錄學習
login_first()
# 進入課程
into_course()
for i in range(1,100):
time.sleep(3)
# 獲取未學列表
li_list = web.find_elements(By.XPATH, f'//*[@class="orange"]/..')
# 進入第二課
# web.find_element(By.XPATH, '/html/body/div[5]/div[1]/div[2]/div[3]/div/div[2]/h3/a').click()
# 點擊列表首個未學項
li_list[0].click()
print('點擊未學課')
time.sleep(5)
# print(len(li_list))
# 切換到最外框架
web.switch_to.default_content()
time.sleep(1)
# 允許播放布爾
symbol = 1;
play_video(symbol, i)
# 進入判斷視頻是否播放完循環
symbol = video_finished()
# 切換至最外框架
web.switch_to.default_content()
# 播放完一個視頻點擊回到課程
web.find_element(By.XPATH, f'/html/body/div[3]/div/div[1]/a').click()
time.sleep(5)
# 循環最初獲取未學列表