当前位置:   article > 正文

python利用selenium库(WEB自动化)完成智慧树自动刷题_智慧树脚本自动答题

智慧树脚本自动答题

 这里用的是Chrome浏览器,使用selenium库是需要先安装Chrome的驱动

分析:

selenium库能实现网页自动操作,智慧树每次学习完一小节后都需要点击下一节视频。这里我检测了视频的总播放长度和当前播放的时间长度,每5秒钟检测一次它们是否相同,如果相同就点击下一节视频完成自动化。看视频时也会遇到不定时弹窗,就每隔5秒钟检测是否有弹窗出现,如果有弹窗就点击选项然后关闭。大概的思路就是这样。

如果有什么地方侵权了请联系我删除

我只是为了能和大家一起学习,没有其他意思

  1. #导入库
  2. from selenium import webdriver
  3. from selenium.webdriver.common.by import By
  4. from selenium.webdriver.common.action_chains import ActionChains
  5. import os
  6. import time
  7. # 点击开始
  8. def play():
  9. start_status = driver.find_element(By.XPATH, '//div[@id="playButton"]').get_attribute('class')
  10. start_button = driver.find_element(By.ID, 'playButton')
  11. xuanting = driver.find_element(By.XPATH, '//*[@id="vjs_container"]/div[8]') # 点击视频
  12. jianchetanchuang() # 检测弹窗
  13. time.sleep(2)
  14. if start_status.find('playButton') != -1:
  15. print('当前静止')
  16. xuanting.click()
  17. print('点击播放成功')
  18. else:
  19. print('当前正在播放')
  20. # 悬停找到控制台
  21. def openControlsBar():
  22. controlsBar = driver.find_element(By.XPATH, ' //div[@id="container"]')
  23. ActionChains(driver).move_to_element(controlsBar).perform() ##
  24. # 下一个视频
  25. def nextVideo():
  26. openControlsBar()
  27. nextBtn = driver.find_element(By.XPATH, '//div[@id="nextBtn"]')
  28. nextBtn.click()
  29. print('切换下一个视频')
  30. # 获取当前播放时间
  31. def getCurrentTime():
  32. openControlsBar()
  33. time = driver.find_element(By.XPATH, '//div[@class="nPlayTime"]/span[1]').text
  34. print('当前播放时间为:' + time)
  35. return time
  36. # 关闭弹窗继续播放
  37. def pop_up():
  38. panduan = driver.find_element(By.XPATH,
  39. '//*[@id="playTopic-dialog"]/div/div[2]/div/div[1]/div/div/div[2]/p/span[2]').text
  40. if panduan in '判断题':
  41. driver.find_element(By.XPATH,
  42. '//*[@id="playTopic-dialog"]/div/div[2]/div/div[1]/div/div/div[2]/ul/li[1]/div[2]').click() # 点击第一个选项
  43. driver.find_element(By.XPATH, '//*[@id="playTopic-dialog"]/div/div[3]/span/div').click() # 点击关闭
  44. else:
  45. driver.find_element(By.XPATH,
  46. '//*[@id="playTopic-dialog"]/div/div[2]/div/div[1]/div/div/div[2]/ul/li[1]').click() # 点击第一个
  47. driver.find_element(By.XPATH, '//*[@id="playTopic-dialog"]/div/div[3]/span/div').click() # 点击关闭
  48. # chuowu = driver.find_element(By.XPATH,'//*[@id="playTopic-dialog"]/div/div[2]/div/div[1]/div/div/div[2]/p/span[1]/span').text#获取是否正确
  49. # driver.find_element(By.XPATH,'//*[@id="playTopic-dialog"]/div/div[2]/div/div[1]/div/div/div[2]/ul/li[2]/div[2]')#点击错
  50. # if chuowu == '正确':
  51. # driver.find_element(By.XPATH,'//*[@id="playTopic-dialog"]/div/div[2]/div/div[1]/div/div/div[2]/ul/li[1]/div[2]')#点击对
  52. # driver.find_element(By.XPATH,'//*[@id="playTopic-dialog"]/div/div[3]/span/div').click()#点击关闭
  53. # 关闭学前必读
  54. def xueqian():
  55. try:
  56. # 检测是否有学前必读
  57. # 点击×
  58. close = driver.find_element(By.XPATH, '// *[ @ id = "app"] / div / div[6] / div[2] / div[1] / i')
  59. close.click()
  60. print("关闭学前必读成功!")
  61. except:
  62. print("未发现学前必读")
  63. # 获取当前视频总时间
  64. def getTotalTime():
  65. openControlsBar()
  66. time = driver.find_element(By.XPATH, '//div[@class="nPlayTime"]/span[2]').text
  67. print('本集视频时间为:' + time)
  68. return time
  69. #控制下面的循环间隔
  70. def sleeptime(hour, min, sec):
  71. return hour * 3600 + min * 60 + sec
  72. #检测弹窗
  73. def jianchetanchuang(): # 检测弹窗
  74. try:
  75. pop_up() # 关闭弹窗
  76. print('关闭弹窗完成')
  77. except:
  78. print('未检测到弹窗')
  79. # 不自动关闭浏览器
  80. option = webdriver.ChromeOptions()
  81. option.add_experimental_option("detach", True)
  82. driver = webdriver.Chrome(options=option)
  83. #主程序
  84. driver.get('智慧树视频链接地址')
  85. userName = "填写账号"
  86. passWord = "填写密码"
  87. driver.find_element(By.ID, 'lUsername').send_keys(userName)
  88. driver.find_element(By.ID, 'lPassword').send_keys(passWord)
  89. driver.find_element(By.XPATH, "//span[@class='wall-sub-btn']").click()
  90. time.sleep(2)
  91. yanzhi = driver.find_element(By.XPATH, '/html/body/div[29]/div[2]/div/div/div[1]/span[2]').text
  92. second = sleeptime(0, 0, 5)
  93. if yanzhi == '请完成安全验证':
  94. print('完成验证按任意键继续')
  95. os.system('pause')
  96. # 检测是否看完了
  97. while (True):
  98. time.sleep(second) # 5秒执行一次
  99. #关闭学前必读
  100. xueqian()
  101. # 检测弹窗
  102. jianchetanchuang()
  103. if getCurrentTime() == getTotalTime():
  104. nextVideo()
  105. time.sleep(5)
  106. play()

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/392722
推荐阅读
相关标签
  

闽ICP备14008679号