Python Selenium 自动评分程序
import os
import time
import random
import tkinter as tk
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
def check_browser():
# 检查是否已经打开了浏览器
try:
options = Options()
options.add_experimental_option('debuggerAddress', '127.0.0.1:9527')
browser = webdriver.Chrome(options=options)
browser.execute_script('return true')
return True
except:
return False
def run_program():
global running
running = True
if not check_browser():
os.system(r'start chrome --remote-debugging-port=9527 --user-data-dir="D:\评阅用"')
options = Options()
options.add_experimental_option('debuggerAddress', '127.0.0.1:9527')
browser = webdriver.Chrome(options=options)
while True:
for handle in browser.window_handles:
browser.switch_to.window(handle)
if '内蒙古开放大学' in browser.title:
print('用户登录成功!')
break
else:
time.sleep(1)
continue
break
while running:
try:
score_box = browser.find_element(By.CLASS_NAME, 'score-box')
score_input = score_box.find_element(By.TAG_NAME, 'input')
score_input.clear()
score_input.send_keys(str(random.randint(94, 99)))
time.sleep(1)
submit_button = browser.find_element(By.CLASS_NAME, 'nav-text')
submit_button.click()
time.sleep(2)
except:
continue
else:
if '已到最后' in browser.page_source:
print('已到最后,暂停程序')
break
time.sleep(1)
progress_label.config(text='运行进度:' + str(score_input.get_attribute('value')) + '/100')
def stop_program():
global running
running = False
if __name__ == '__main__':
running = False
root = tk.Tk()
root.title('自动评分程序')
root.geometry('300x150')
start_button = tk.Button(root, text='开始运行', command=run_program)
start_button.pack(pady=10)
stop_button = tk.Button(root, text='停止运行', command=stop_program)
stop_button.pack(pady=10)
progress_label = tk.Label(root, text='运行进度:0/100')
progress_label.pack(pady=10)
root.mainloop()
原文地址: https://www.cveoy.top/t/topic/fYyI 著作权归作者所有。请勿转载和采集!