前言:
最近这个科目三很火,那么今天跟大家分享下科目一的答题教学,当然不是本人答,撸个程序让他自己动,实际上想让程序进行答题的操作在日常生活中也经常遇到,对于一些正在接触自动化的朋友来讲,希望能给大家带来一定收获!(案例官网如下)https://www.jsyks.com/kmy-mnkshttps://www.jsyks.com/kmy-mnks
补充一点,跟粉丝朋友们分享下谷歌浏览器的驱动网址,大家自取:
https://googlechromelabs.github.io/chrome-for-testing/
操作解析:
①打开浏览器,进入考试官网
# 导入自动化模块
from selenium import webdriver
# 导入时间模块
import time
# 1.实例化浏览器对象
qudong = webdriver.Chrome()
# 2.进入考试官网
qudong.get('https://www.jsyks.com/kmy-mnks')
# 暂停阻塞
input()
文章来源:https://www.toymoban.com/news/detail-791665.html
②正常答题:明确答案/选项》》答案和选项进行对比答题
1.知晓答案并且告知程序 https://tiba.jsyks.com/Post/d16b8.htm https://tiba.jsyks.com/Post/f068b.htm https://tiba.jsyks.com/Post/48e87.htm - 在网页源码当中提取答案完事了 - 每题网页网址 id 不一样 https://tiba.jsyks.com/Post/{xxx}.htm - 得到100道题的id
lis = qudong.find_elements(By.CSS_SELECTOR, '.Exam .Content li')
for li in lis:
id = li.get_attribute('c')
href = f'https://tiba.jsyks.com/Post/{id}.htm'
# print(href)
response = requests.get(href).text
# '答案是:错。"'
answer = re.findall('答案是:(.*?)。"', response)[0]
# print(answer)
2. 告诉程序??--》 正常答题 题目选项 - 在网页渲染前端代码 拿到b标签的文本 3. 程序知道答案和选项是一致的!
if answer == '对':
answer = '正确'
elif answer == '错':
answer = '错误'
bs = li.find_elements(By.TAG_NAME, 'b')
for b in bs:
choose = b.text
# len() 判断字符长度
'''
ABCD 排除
'''
if len(choose) > 2:
choose = choose[0]
else:
choose = choose
if answer == choose:
# 答题点击
b.click()
print('选项内容是:', choose)
print('答案内容是:', answer)
③提交试卷
qudong.find_element(By.CSS_SELECTOR, '.btnJJ').click()
time.sleep(5)
qudong.quit() # 关闭网页
文章来源地址https://www.toymoban.com/news/detail-791665.html
代码演示:
import time
from selenium import webdriver
import requests
import re
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.jsyks.com/kmy-mnks')
lis = driver.find_elements(By.CSS_SELECTOR, '.Exam .Content li')
for li in lis:
id = li.get_attribute('c')
href = f'https://tiba.jsyks.com/Post/{id}.htm'
# print(href)
response = requests.get(href).text
# '答案是:错。"'
answer = re.findall('答案是:(.*?)。"', response)[0]
# print(answer)
if answer == '对':
answer = '正确'
elif answer == '错':
answer = '错误'
bs = li.find_elements(By.TAG_NAME, 'b')
for b in bs:
choose = b.text
if len(choose) > 2:
choose = choose[0]
else:
choose = choose
if answer == choose:
b.click()
print('选项内容是:', choose)
print('答案内容是:', answer)
# break
driver.find_element(By.CSS_SELECTOR, '.btnJJ').click()
time.sleep(5)
driver.quit()
# 暂停阻塞
input()
到了这里,关于selenium答题程序的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!