selenium答题程序

前言:

        最近这个科目三很火,那么今天跟大家分享下科目一的答题教学,当然不是本人答,撸个程序让他自己动,实际上想让程序进行答题的操作在日常生活中也经常遇到,对于一些正在接触自动化的朋友来讲,希望能给大家带来一定收获!(案例官网如下)https://www.jsyks.com/kmy-mnksicon-default.png?t=N7T8https://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()

②正常答题:明确答案/选项》》答案和选项进行对比答题
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()  # 关闭网页

代码演示:

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()

相关推荐

  1. 记录-ORACLE

    2024-01-12 14:56:02       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-12 14:56:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-12 14:56:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-12 14:56:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-12 14:56:02       18 阅读

热门阅读

  1. 算法习题练习

    2024-01-12 14:56:02       29 阅读
  2. .net core 6 集成和使用 mongodb

    2024-01-12 14:56:02       36 阅读
  3. 【大数据面试】常见数仓建模面试题附答案

    2024-01-12 14:56:02       33 阅读
  4. vue3知识盲点总结

    2024-01-12 14:56:02       28 阅读
  5. js中console.log()的使用方法

    2024-01-12 14:56:02       35 阅读
  6. 箭头函数与普通函数的差异

    2024-01-12 14:56:02       34 阅读
  7. Django身份验证初试

    2024-01-12 14:56:02       43 阅读