实验室安全考试系统自动答题

您所在的位置:网站首页 世界四大百科全书是什么 实验室安全考试系统自动答题

实验室安全考试系统自动答题

2023-11-04 16:34| 来源: 网络整理| 查看: 265

前言

学校要求在实验室安全考试系统上学习实验室安全相关的知识,然后完成一个考试。我就想着借这个机会把python selenium自动化测试模块的知识再学一下以前只稍微用过那么一两下,反正就是乱搞嘛,学习这么枯燥当然要自己找点乐子了。完成了一个自动答题的一个脚本,输入自己的学号和密码,程序就会自动答题,最后结果应该是满分,仅此记录一下学习过程,不是为了答题才写的,实验室安全还是要好好了解一下的😄,具体代码如下

代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131# -*- codeing = utf-8 -*-# @Time : 2020/10/24# @Author : loadding...# @File : lab_safety.py# @Software : jupyter from lxml import etreeimport requestsfrom selenium import webdriver import re def getQuestionBank():# url="http://aqks.neu.edu.cn/redir.php?catalog_id=6&cmd=dajuan_chakan&huihuabh=69338&mode=test" url="http://aqks.neu.edu.cn/" response=requests.get(url) page_source=response.text #print(page_source) tree=etree.HTML(page_source) arr=tree.xpath('//*[@class="shiti"]/strong') #print(arr) def search_answer(answer_source,question): #匹配判断题答案 #s=question+'(分值1.0)你未作答标准答案:(.{2,4}?)' #查了好久,终于知道为什么有的答案搜索不到了,比如某些问题里带有?,而?在正则里是有含义的,需要转义 question=question.replace('?','\\?') question=question.replace('(','\\(') question=question.replace(')','\\)') #.*?非贪婪模式匹配,匹配判断题、选择题 s=question+'.*?你未作答标准答案:(.{1,2}?)' #s='灾初起阶段是扑救火灾()的阶段。.*?你未作答标准答案:(.{1,2}?)' #print(s) pattern=re.compile(s) #获取答案 answer=re.findall(pattern,answer_source) #返回搜索到的答案列表,注意可能为空列表 #print(answer) return answer if __name__=='__main__': while(1): #登录网站 print("****************************欢迎来到实验室安全考试系统,本程序是自动答题脚本,祝您玩的愉快!!!*************************") username=input('请输入学号:') password=input('请输入密码:') #实例化出一个Firefox浏览器 dr=webdriver.Chrome() dr.get("http://aqks.neu.edu.cn/") dr.find_element_by_id('u1').send_keys(username) dr.find_element_by_id('password').send_keys(password) #点击提交登录 dr.find_element_by_xpath('//*[@id="web_login"]/div/form/div[3]/input').click() if dr.current_url=='http://aqks.neu.edu.cn/': print("用户名或密码错误!请重新登录!!!") else: break #点击导航栏安全考试 dr.find_element_by_xpath('/html/body/div[1]/div[2]/ul/li[3]/a').click() while(1): type=input('模拟考试/开始考试(输入1或2):') if type=='1': #点击模拟考试 dr.find_element_by_xpath('//*[@id="article"]/div[4]/div[2]/div/a[1]').click() break elif type=='2': #点击开始考试 dr.find_element_by_xpath('//*[@id="article"]/div[4]/div[2]/div/a[2]').click() break else: print("输入错误,请重输!!!") #提取问题,定位的是第一个元素不是一个列表,改用其他方法 #question=dr.find_element_by_class_name('shiti').text[2:-4] #获取答案 url="http://aqks.neu.edu.cn/redir.php?catalog_id=6&cmd=dajuan_chakan&huihuabh=69338&mode=test" response=requests.get(url) response.encoding='gbk'#网页是gbk编码 answer_source=response.text #去掉所有的空白符,包括换行符和空格、制表符 answer_source=re.sub('\s','',answer_source) #print(answer_source) #共有10页,每页10题 page=0 while(page


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3