使用python的selenium库刷超星网课
网课很多看不完呀
所以动手做了一个基础的自动答题和下一节的程序
用到了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库刷超星网课的更多相关文章
- 使用Python的selenium库制作脚本,支持后台运行
本文介绍如何使用Python的selenium库制作脚本.概念: Selenium是一个用于测试网站的自动化测试工具,支持各种浏览器包括Chrome.Firefox.Safari等主流界面 ...
- python爬虫---selenium库的用法
python爬虫---selenium库的用法 selenium是一个自动化测试工具,支持Firefox,Chrome等众多浏览器 在爬虫中的应用主要是用来解决JS渲染的问题. 1.使用前需要安装这个 ...
- 安装python的selenium库和驱动
对于使用selenium来进行python爬虫操作可以简化好多操作,它实际上的运行就是通过打开一个浏览器来一步一步的按照你的代码来执行 如果安装过python编译器后应该pip工具也是有的,验证pyt ...
- python利用selenium库识别点触验证码
利用selenium库和超级鹰识别点触验证码(学习于静谧大大的书,想自己整理一下思路) 一.超级鹰注册:超级鹰入口 1.首先注册一个超级鹰账号,然后在超级鹰免费测试地方可以关注公众号,领取1000积分 ...
- Python爬虫-- selenium库
selenium库 selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)和测试的并行处理(S ...
- 解决python 导入selenium 库后自动化运行成功但是报错问题
本章节开始进入自动化的基础教学了,首先我们要对我们的工具有一定的熟练使用程度,做自动化常用的工具一个是搭建 RobotFramework自动化框架,另外一个便是我们最常用的python 工作原理是比较 ...
- python beautiful soup库的超详细用法
原文地址https://blog.csdn.net/love666666shen/article/details/77512353 参考文章https://cuiqingcai.com/1319.ht ...
- Python:利用 selenium 库抓取动态网页示例
前言 在抓取常规的静态网页时,我们直接请求对应的 url 就可以获取到完整的 HTML 页面,但是对于动态页面,网页显示的内容往往是通过 ajax 动态去生成的,所以如果是用 urllib.reque ...
- 浅谈python中selenium库调动webdriver驱动浏览器的实现原理
最近学web自动化时用到selenium库,感觉很神奇,遂琢磨了一下,写了点心得. 当我们输入以下三行代码并执行时,会发现新打开了一个浏览器窗口并访问了百度首页,然而这是怎么做到的呢? from se ...
随机推荐
- HearthBuddy的plugin加载
// Hearthbuddy.Windows.MainWindow // Token: 0x060001FF RID: 511 RVA: 0x0008951C File Offset: 0x00087 ...
- Windows上配置Mask R-CNN及运行示例demo.ipynb
最近做项目需要用到Mask R-CNN,于是花了几天时间配置.简单跑通代码,踩了很多坑,写下来分享给大家. 首先贴上官方Mask R-CNN的Github地址:https://github.com/m ...
- Winform使用ML.NET时无法加载 DLL“CpuMathNative”问题的解决方法
同样的代码运行在netcore下可以,运行在winform中就出现错误: 引发的异常:“System.DllNotFoundException”(位于 Microsoft.ML.Data.dll 中) ...
- JDBC插入数据,获取自增长值
package com.loaderman.demo.c_auto; public class Dept { private int id; private String deptName; publ ...
- Collapse 折叠面板
通过折叠面板收纳内容区域 基础用法 可同时展开多个面板,面板之间不影响 <el-collapse v-model="activeNames" @change="ha ...
- git分支名大小写问题导致分支push到远程失败
windows系统不识别文件夹大小写导致 本地分支master, 在master上面新建一个分支Hotfix/aa 由于Hotfix首字母大写,所以windows系统会在 项目.git\refs\he ...
- c++传递函数当作对象传递
c++中函数当作对象来传递,类似c#中的指针操作如: #include <iostream> using namespace std; int tst(int a){ cout<&l ...
- 【HANA系列】SAP HANA SQL REPLACE替换字符串
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL REP ...
- MATLAB实现OTSU
目录 1.OTSU算法原理简述: 2.MATLAB实现代码 @ 1.OTSU算法原理简述: 最大类间方差是由日本学者大津(Nobuyuki Otsu)于1979年提出,是一种自适应的阈值确定方法.算法 ...
- 【Python开发】查看数据类型
import types aaa = 0 print type(aaa) if type(aaa) is types.IntType: print "the type of aaa is i ...