0


selenium答题程序

前言:

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

②正常答题:明确答案/选项》》答案和选项进行对比答题
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()
标签: selenium python

本文转载自: https://blog.csdn.net/qq_47459180/article/details/135526676
版权归原作者 qq_wuyan 所有, 如有侵权,请联系我们删除。

“selenium答题程序”的评论:

还没有评论