时间紧任务重,没办法才出此下策,该学习还是要认真学习
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, "//*[@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)
# 循环最初获取未学列表