import os
import time
import random
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

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

if __name__ == '__main__':
    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 True:
        try:
            score_box = browser.find_element(By.CLASS_NAME, 'score-box')
            break
        except:
            time.sleep(1)
            continue

    while True:
        score_box.clear()
        score_box.send_keys(str(random.randint(95, 99)))
        time.sleep(1)
        try:
            nav_wrapper = browser.find_element(By.CLASS_NAME, 'nav-wrapper')
            ActionChains(browser).move_to_element(nav_wrapper).click(nav_wrapper).perform()
            time.sleep(1)
        except:
            continue
Python Selenium 自动填写评分:解放双手,提高效率

原文地址: https://www.cveoy.top/t/topic/fYt9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录