网课很多看不完呀

所以动手做了一个基础的自动答题和下一节的程序

用到了python 3

selenium

Chrome

如何自动化Chrome?https://www.cnblogs.com/eternal1025/p/8880245.html

配置好之后上代码

源码下载地址  https://github.com/zlaiyyf/ChaoXIing_seleniumm

时间紧张所以嘛bug就。。。。

 coding:utf-8
from selenium import webdriver
# 显示等待
from selenium.webdriver.support.wait import WebDriverWait
# 启动参数
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import UnexpectedAlertPresentException,TimeoutException
from scrip import click
import time class Chaoxing(): def __init__(self,password,username):
"""
暂时不考虑其他学校的 :param password:
:param username:
""" chrome_opt = Options() # 创建参数设置对象.
# chrome_opt.add_argument('–start-maximized') # 设置浏览器窗口大小. chrome_opt.add_argument('--disable-infobars')
# chrome_opt.add_argument("-–start-maximized") chrome_opt.add_experimental_option('excludeSwitches', ['enable-automation'])
self.browser = webdriver.Chrome(chrome_options=chrome_opt)
self.username = username
self.password = password def land(self):
"""
登陆
:return:
""" self.browser.get('http://sxu.fanya.chaoxing.com/portal')
# self.browser.find_element_by_xpath('//input[@value= "登录"]').click()
WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath('//input[@value= "登录"]')).click()
# ActionChains(driver).click(click_btn)
WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath('//input[@id="unameId"]')).send_keys(self.username)
WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath('//input[@id="passwordId"]')).send_keys(self.password)
print('输入账号完成{}'.format(self.username))
time.sleep(10) WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath('//input[@value= "登录"]')).click() def find_course(self):
"""
发现课程
:return:
"""
self.browser.switch_to.frame('frame_content')
self.browser.find_elements_by_xpath('//li[@style="position:relative"]')
course_name = self.browser.find_elements_by_xpath('//h3[@class="clearfix"]')
click.click_couse(course_name,'创业创新领导力') windows = self.browser.window_handles
self.browser.switch_to.window(windows[-1])
self.couse()
def couse(self):
"""
进入课程
:return:
"""
class_num = -1
while True: time.sleep(2)
class_num = class_num + 1
class_name_list = self.browser.find_elements_by_xpath('//div[@class="leveltwo"]')
if class_num == len(class_name_list):
break
else:
print(class_num)
class_name_num = self.browser.find_elements_by_xpath('//div[@class="leveltwo"]')[class_num].text
print(class_name_num.split('\n'))
if '' == class_name_num.split('\n')[1]:
continue
class_name_tag = self.browser.find_elements_by_xpath('//span[@class="articlename"]')[class_num]
class_name = class_name_tag.text
class_name_tag.click() print('正在点击{}'.format(class_name)) time.sleep(2)
self.view(class_name=class_name)
continue
def view(self,class_name):
"""
看视频
:param class_name:
:return:
"""
# try:
self.browser.find_element_by_xpath('//span[@title="视频"]').click()
self.browser.switch_to.frame("iframe")
time.sleep(5)
self.browser.switch_to.frame(self.browser.find_element_by_xpath('//iframe[@class="ans-attach-online ans-insertvideo-online"]'))
WebDriverWait(self.browser, 30, 0.2).until(lambda x: x.find_element_by_xpath('//div[@id="video"]')).click() view_tag = self.browser.find_element_by_xpath('//div[@id="video"]')
ActionChains(self.browser).move_to_element(view_tag).perform()
while True:
time.sleep(2) if self.view_percentage() == '' :
self.browser.switch_to.default_content()
self.browser.find_element_by_xpath('//a[contains(text(), "回到课程")]').click()
break def view_percentage(self):
""""
检查是否看完
"""
# total_duration = self.browser.find_element_by_xpath('//span[@class="vjs-duration-display"]').text
# current_duration = self.browser.find_element_by_xpath('//span[@class="vjs-current-time-display"]').text
view_percentage_tag = self.browser.find_element_by_xpath('//div[@class="vjs-play-progress vjs-slider-bar"]')
view_percentage = view_percentage_tag.get_attribute('style')
print('当前进度'+view_percentage)
self.is_exist_problem()
""""
检查是否看完
"""
if '100%' in view_percentage :
return '' def is_exist_problem(self):
try:
problem_tag_style = WebDriverWait(self.browser, 30, 0.2).until(
lambda x: x.find_element_by_xpath('//div[@id="ext-comp-1035"]')).get_attribute('style') if problem_tag_style == 'overflow: auto;':
print('有题目')
input_tag_list = self.browser.find_elements_by_xpath('//input')
for input_tag in input_tag_list:
input_tag.click()
self.browser.find_element_by_xpath('//div[@class="ans-videoquiz-submit"]').click()
time.sleep(2)
if EC.alert_is_present()(self.browser):
self.browser.switch_to.alert.accept()
else:
break
else:
pass
except UnexpectedAlertPresentException:
print('alert出错')
self.browser.switch_to.alert.accept()
except TimeoutException:
print('TimeoutException')
pass

使用python的selenium库刷超星网课的更多相关文章

  1. 使用Python的selenium库制作脚本,支持后台运行

    本文介绍如何使用Python的selenium库制作脚本.概念:       Selenium是一个用于测试网站的自动化测试工具,支持各种浏览器包括Chrome.Firefox.Safari等主流界面 ...

  2. python爬虫---selenium库的用法

    python爬虫---selenium库的用法 selenium是一个自动化测试工具,支持Firefox,Chrome等众多浏览器 在爬虫中的应用主要是用来解决JS渲染的问题. 1.使用前需要安装这个 ...

  3. 安装python的selenium库和驱动

    对于使用selenium来进行python爬虫操作可以简化好多操作,它实际上的运行就是通过打开一个浏览器来一步一步的按照你的代码来执行 如果安装过python编译器后应该pip工具也是有的,验证pyt ...

  4. python利用selenium库识别点触验证码

    利用selenium库和超级鹰识别点触验证码(学习于静谧大大的书,想自己整理一下思路) 一.超级鹰注册:超级鹰入口 1.首先注册一个超级鹰账号,然后在超级鹰免费测试地方可以关注公众号,领取1000积分 ...

  5. Python爬虫-- selenium库

    selenium库 selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)和测试的并行处理(S ...

  6. 解决python 导入selenium 库后自动化运行成功但是报错问题

    本章节开始进入自动化的基础教学了,首先我们要对我们的工具有一定的熟练使用程度,做自动化常用的工具一个是搭建 RobotFramework自动化框架,另外一个便是我们最常用的python 工作原理是比较 ...

  7. python beautiful soup库的超详细用法

    原文地址https://blog.csdn.net/love666666shen/article/details/77512353 参考文章https://cuiqingcai.com/1319.ht ...

  8. Python:利用 selenium 库抓取动态网页示例

    前言 在抓取常规的静态网页时,我们直接请求对应的 url 就可以获取到完整的 HTML 页面,但是对于动态页面,网页显示的内容往往是通过 ajax 动态去生成的,所以如果是用 urllib.reque ...

  9. 浅谈python中selenium库调动webdriver驱动浏览器的实现原理

    最近学web自动化时用到selenium库,感觉很神奇,遂琢磨了一下,写了点心得. 当我们输入以下三行代码并执行时,会发现新打开了一个浏览器窗口并访问了百度首页,然而这是怎么做到的呢? from se ...

随机推荐

  1. Maven私服搭建及使用-Windows版

    了解有限,目前只针对基础功能介绍,持续更新 一.下载对应的版本(本例版本:nexus-3.7.1-02) https://www.sonatype.com/download-oss-sonatype ...

  2. mongodb aggregate 聚合 操作(扁平化flatten)

    mongodb自带的函数非常多,最近用mongo做持久化数据库,遇到一个需求:子文档是个数组,把数组里的各个字段扁平化合到根文档中,查过资料后(主要是mongodb的文档和stackoverflow) ...

  3. STL -- heap结构及算法

    STL -- heap结构及算法 heap(隐式表述,implicit representation) 1. heap概述 : vector + heap算法 heap并不归属于STL容器组件,它是个 ...

  4. SUSE系列---修改IP和hosts文件

    有些时候我们需要修改linux系统的IP,此时我们要注意,修改IP之后,如果我们如果不修改hosts文件,那么可能我们一些服务会无法使用:下面分别记录修改IP和hosts文件的方法: 修改IP: 打开 ...

  5. C#类型转换类(通用类)

    //     /// 类型转换类     /// 处理数据库获取字段为空的情况     ///     public static class DBConvert     {         #reg ...

  6. LC 677. Map Sum Pairs

    Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...

  7. LC 833. Find And Replace in String

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  8. shell中变量计算

    year=44 1.let,不需要$引用变量 let m=year+3 echo $m 2.(()) m=$((year+3)) 3.[  ],注意两边一定要有空格 m=$[ year+3 ] 4. ...

  9. c# 动态编译CodeDomProvider.CompileAssemblyFromSource(CompilerParameters cp,string code)

    1.使用c#动态编译,可参考如下: //生成c#代码 string code = GenerateCode(); CodeDomProvider cdp = CodeDomProvider.Creat ...

  10. mariadb面试

    [mariadb主从架构的工作原理] 主节点写入数据以后,保存到二进制文件中,从节点生成IO线程和sql线程,IO线程请求读取二进制文件:主节点生成的dump线程,将数据发送到中继日志中,sql线程读 ...