from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import NoSuchElementException,StaleElementReferenceException,ElementNotVisibleException,TimeoutException,WebDriverException
import time
import os
home=''
dr=webdriver.Chrome()
dr.maximize_window()
dr.get(home)
user=""
pswd=""
def waitForElementVisibility(xpath,duration,msg):
wait=WebDriverWait(dr,duration)
locator=(By.XPATH,xpath) # //*[@id="app-container"]/div[4]/div/img[2]
try:
element= wait.until(EC.visibility_of_element_located(locator))
return element
except TimeoutException:
print(str(msg))
return None
def LoginStudyCenter():
xpath='//div[@class="poster"]/img[@class="close"]'
duration=10
msg='关闭广告超时'
element=waitForElementVisibility(xpath,duration,msg)
if not element is None:
element.click()
xpath='//button[@class="m-btn-login"]'
duration=10
msg='单击登录注册窗口超时'
element=waitForElementVisibility(xpath,duration,msg)
if not element is None:
element.click()
xpath='//input[@placeholder="请输入用户名/手机/身份证"]'
duration=10
msg='输入用户名超时'
element=waitForElementVisibility(xpath,duration,msg)
if not element is None:
element.click()
element.clear()
element.send_keys(user)
xpath='//input[@placeholder="请输入密码"]'
duration=10
msg='输入密码超时'
element=waitForElementVisibility(xpath,duration,msg)
if not element is None:
element.click()
element.clear()
element.send_keys(pswd)
xpath='//button/span[contains(text(),"登录")]'
duration=10
msg='提交登录超时'
element=waitForElementVisibility(xpath,duration,msg)
if not element is None:
element.click()
xpath='//*[@id="app-container"]/div[1]/div/div/div[2]/div[1]'
duration=20
msg='提交登录超时'
element=waitForElementVisibility(xpath,duration,msg)
if not element is None:
print('进入学习中心成功!')
time.sleep(5)
return True
else:
print('进入学习中心失败')
return False
def BackToSubjects():
backToSubject=dr.find_element_by_xpath('//span[text()="返回课程章节"]')
try:
dr.execute_script("arguments[0].click();",backToSubject)
time.sleep(10)
return True
print('返回课程章节成功')
except:
print('返回课程章节失败')
return False
def LoopSubjects():
print('正在进入学习页面')
subjects=dr.find_elements_by_xpath('//div[@class="el-progress__text" and not(text()="100%")]/ancestor::td[@class="el-table_1_column_2 "]/following-sibling::td[@class="el-table_1_column_5 "]/div/button/span[contains(text(),"学习")]')
subjectCount=len(subjects)
print('当前课程未完成的专题还有:'+str(subjectCount)+'个')
while subjectCount>0:
beginStudy=dr.find_element_by_xpath('//div[@class="el-progress__text" and not(text()="100%")]/ancestor::td[@class="el-table_1_column_2 "]/following-sibling::td[@class="el-table_1_column_5 "]/div/button/span[contains(text(),"学习")]')
try:
dr.execute_script("arguments[0].click();",beginStudy)
#等待页面加载
xpath='//i[@class="iconfont close-btn"]'
wait=WebDriverWait(dr,30)
locator=(By.XPATH,xpath) # //*[@id="app-container"]/div[4]/div/img[2]
try:
element= wait.until(EC.presence_of_element_located(locator))
except TimeoutException:
print('等待导航超时!')
dr.refresh()
time.sleep(30)
if LoopLessons()==False:
break
except TimeoutException:
print('----单击继续学习超时')
subjects=dr.find_elements_by_xpath('//div[@class="el-progress__text" and not(text()="100%")]/ancestor::td[@class="el-table_1_column_2 "]/following-sibling::td[@class="el-table_1_column_5 "]/div/button/span[contains(text(),"学习")]')
subjectCount=len(subjects)
print('本课程所有专题学习完毕,返回课程列表')
return BackToStudyCenter()
def BackToStudyCenter():
xpath='//div[contains(text(),"学习中心")]'
duration=10
msg='返回学习中心失败'
element=waitForElementVisibility(xpath,duration,msg)
if not element is None:
dr.execute_script("arguments[0].click();",element)
print('返回学习中心成功,20秒等待跳转')
time.sleep(20)
return True
else:
print('返回学习中心失败')
return False
def LoopCourses():
xpath='//div[contains(text(),"已学")]/parent::div/parent::div/following-sibling::div[@class="enter-btn"]'
Courses=dr.find_elements_by_xpath(xpath)
CourseCount=len(Courses)
print('当前用户未完成学习的课程有:'+str(CourseCount)+'个')
while CourseCount>0:
wait=WebDriverWait(dr,60)
locator=(By.XPATH,xpath)
try:
keepStudy= wait.until(EC.visibility_of_element_located(locator))
dr.execute_script("arguments[0].click();",keepStudy)
time.sleep(10)
print('准备进入专题页面,等待10秒跳转!')
if LoopSubjects()==False:
break
except TimeoutException:
print('----单击继续学习超时')
Courses=dr.find_elements_by_xpath(xpath)
CourseCount=len(Courses)
print('课程学习完毕或者意外退出啦')
def LoopLessons():#主要控制视频的播放
#忽略所有提示
IgnoreTips()
unfinishLessons=dr.find_elements_by_xpath('//i[@class="iconfont"]/parent::div/following-sibling::span[not(text()="章测验") and not(text()="练习题")]')
print("当前专题未观看视频还有:"+str(len(unfinishLessons))+'个')
unfinishCount=len(unfinishLessons)
#for i in range(unfinishCount):
if 1:
try:
#nextlesson=dr.find_elements_by_xpath('//i[@class="iconfont"]/parent::div/following-sibling::span[not(text()="章测验") and not(text()="练习题")]')[unfinishCount-1]
nextlesson=dr.find_element_by_xpath('//i[@class="iconfont"]/parent::div/following-sibling::span[not(text()="章测验") and not(text()="练习题")]')
print('开始观看第一个未完成视频')
while not nextlesson is None:
try:
dr.execute_script("return arguments[0].scrollIntoView();",nextlesson)
time.sleep(1)
try:
nextlesson.click()
try:
closetip=waitForElementVisibility('//*[contains(text(),"留在本页")]',10,"等待 留在本页失败 1")
dr.execute_script("arguments[0].click();",closetip)
break#退出本章节了
except:
print('尝试单击 留在本页 1')
wait=WebDriverWait(dr,10)
locator=(By.XPATH,'//button[@title="Play"]')
try:
playBtn= wait.until(EC.visibility_of_element_located(locator))
#播放并观看视频
try:
WatchVideo()
except:
print('观看未完成视频失败!')
except:
print('当前页面不是视频页面,准备进行下一步判断处理-------->')
try:
#test=dr.find_element_by_xpath('//div[@class="course-title small" and contains(text(),"章测验")]')
test=dr.find_element_by_xpath('//div[@class="course-title small"]')
print('定位标题元素成功,准备判断本页面为章测验或者练习题')
print(test.text)
if test.text=='章测验' or test.text=='练习题':
print('当前页面是章测验页面,准备进行测验处理')
ChapterTest()
try:
closetip=waitForElementVisibility('//*[contains(text(),"留在本页")]',10,"等待 留在本页失败 2")
dr.execute_script("arguments[0].click();",closetip)
if test.text=='章测验':
break#退出本章节了
except:
print('尝试单击 留在本页 2')
if test.text=='章测验':
break#退出本章节了
else:
print('当前页面是文章页面,没有视频,准备跳过!')
except:
print("当期页面不是章测验")
except:
print("切换至未完成Lesson页面失败")
break
except:
print("滚动至到指定章节失败!")
break
#这里查找下一页
nextlesson=dr.find_element_by_xpath('//div[@class="btn-tip"]/span[text()="下一页"]')
except:
print("所有视频都已观看!")
#练习和章测验 此处添加处理
print('本专题的所有章节已经看完了,返回专题列表!')
return BackToSubjects()
def IgnoreTips():
counter=0
while 1:
counter=counter+1
if counter==3:
dr.refresh()
if counter==6:
print('网页加载很失败')
break
wait=WebDriverWait(dr,3)
locator=(By.XPATH,'//div[@class="modal-operation"]/button[text()="继续学习"]')
try:
multi= wait.until(EC.visibility_of_element_located(locator))
dr.execute_script("arguments[0].click();",multi)
except:
print('----单击多开提示超时')
wait=WebDriverWait(dr,3)
locator=(By.XPATH,'//div[@class="modal-operation"]/button[text()="知道了"]')
try:
watchtime= wait.until(EC.visibility_of_element_located(locator))
dr.execute_script("arguments[0].click();",watchtime)
except:
print('----单击时长提示超时')
wait=WebDriverWait(dr,3)
locator=(By.XPATH,'//div[@class="close-btn" and text()="跳过所有提示"]')
try:
watchtime= wait.until(EC.visibility_of_element_located(locator))
dr.execute_script("arguments[0].click();",watchtime)
break
except:
print('----单击跳过提示超时')
def WatchVideo():
if 1:
playBtn= dr.find_element_by_xpath('//button[@title="Play"]')
playBtn.click()
try:
video=dr.find_element_by_xpath('//mediaelementwrapper/video[@class="custom-video"]')
x=dr.execute_script("return arguments[0].playbackRate=15;", video)
print('视频加速成功!')
#此处设置退出条件撒
wait=WebDriverWait(dr,int(10000))
locator=(By.XPATH,'//span[text()="已看完"]')
try:
finish= wait.until(EC.visibility_of_element_located(locator))
print("当前视频看完了!")
except:
print('检查视频是否看完超时咯!')
except:
print('视频加速失败')
def ChapterTest():
anlist=[]
if 1:
xpath='//button[@class="btn-submit" and text()="提交"]'
submits=dr.find_elements_by_xpath(xpath)
submitCount=len(submits)
print('提交按钮的数量有:'+str(submitCount))
try:
submit= dr.find_elements_by_xpath(xpath)[submitCount-1]
try:
dr.execute_script("return arguments[0].scrollIntoView();",submit)
print('滚屏至提交按钮,等待3秒---->')
time.sleep(3)
try:
dr.execute_script("arguments[0].click();",submit)
print('空白提交成功,等待3秒---->')
time.sleep(3)
except:
print('空白提交失败!')
except:
print('滚屏至提交按钮失败')
except:
print('等待提交按钮失败')
xpath='//div[@class="correct-answer-area"]/span[contains(text(),"正确答案")]'
wait=WebDriverWait(dr,10)
locator=(By.XPATH,xpath)
#try:
if 1:
redo=wait.until(EC.presence_of_element_located(locator))
print('页面发现正确答案信息')
print('------------------------------------------------------------------------')
try:
rights=dr.find_elements_by_xpath('//div[@class="correct-answer-area"]/span[contains(text(),"正确答案")]/following-sibling::span')
print("正确答案题目数量:"+str(len(rights)))
try:
for k in range(len(rights)):
try:
print('准备获取第'+str(k+1)+'道题目答案---->')
an=dr.find_elements_by_xpath('//div[@class="correct-answer-area"]/span[contains(text(),"正确答案")]/following-sibling::span')[k]
print('定位到元素')
anText=an.get_attribute("innerText")
print('读取元素文本内容:')
print(anText)
#antext2=dr.execute_script('return arguments[0].innerText;',an)
anlist.append(anText)
print('正确答案存入列表')
except:
print('获取第'+str(k+1)+'道题目答案失败')
except:
print('记录正确答案失败')
except:
print('定位正确答案失败')
print('-----------------------------------准备作答-------------------------------------')
try:
#滚动到重做开始做题
xpath='//button[@class="btn-hollow btn-redo"]'
redos=dr.find_elements_by_xpath(xpath)
redoCount=len(redos)
print('重做按钮的个数有:'+str(redoCount))
#检查是否及格多余吗?
redo=dr.find_elements_by_xpath(xpath)[redoCount-1]
try:
dr.execute_script("return arguments[0].scrollIntoView();",redo)
print('滚屏到重做按钮,等待3秒---->')
time.sleep(3)
try:
dr.execute_script("arguments[0].click();",redo)
#redo.click()
print('单击重做按钮成功,等待3秒---->')
time.sleep(3)
#开始做题
for i,an in enumerate(anlist):
print('准备回答第'+str(i+1)+'道题目')
ans=an.split(',')
for a in ans:
if len(a)>0:
print('第'+str(i+1)+'道题目的答案是:'+a)
#此处开始作答
aj=a+"."
xpath='//div[@class="option" and contains(text(),"'+aj+'")]'
print('查找路径:'+xpath)
chk=dr.find_elements_by_xpath(xpath)[i]
try:
dr.execute_script("return arguments[0].scrollIntoView();",chk)
print('滚屏至选项-----'+a)
time.sleep(0.5)
dr.execute_script("arguments[0].click();",chk)
print('第'+str(i+1)+'单击选项 '+a+' 成功')
time.sleep(0.5)
except:
print('第'+str(i+1)+'单击选项 '+a+' 失败')
print('全部题目作答完毕')
except:
print('单击重做按钮失败')
except:
print('滚屏到重做按钮失败')
except:
print('定位重做按钮失败')
if 1:
xpath='//button[@class="btn-submit" and text()="提交"]'
submits=dr.find_elements_by_xpath(xpath)
submitCount=len(submits)
print('提交按钮的数量有:'+str(submitCount))
try:
submit= dr.find_elements_by_xpath(xpath)[submitCount-1]
try:
dr.execute_script("return arguments[0].scrollIntoView();",submit)
print('重做-滚屏至提交按钮,等待3秒---->')
time.sleep(3)
try:
dr.execute_script("arguments[0].click();",submit)
print('重做-提交成功,等待3秒---->')
time.sleep(3)
except:
print('重做-提交失败!')
except:
print('重做-滚屏至提交按钮失败')
except:
print('重做-等待提交按钮失败')
if __name__ == '__main__':
if LoginStudyCenter():
LoopCourses()