python selenium 案例


from selenium import webdriver
import random
from selenium.webdriver import ActionChains
import time

# chrome_options = webdriver.ChromeOptions()

# 忽略无用的日志
# chrome_options.add_experimental_option("excludeSwitches", ['enable-automation', 'enable-logging'])
# chrome_options.add_argument('--ignore-certificate-errors')


# browser = webdriver.Chrome(options=chrome_options)

# 作业名
HOMEWORK_NAME = "20220623作业"

LOGIN_URL = "https://online.zretc.net/login"
USERNAME = "17004304333"
PASSWORD = "123456"

def login():
    global browser
    browser.get(LOGIN_URL)
    time.sleep(3)
    mimadenglu = browser.find_element_by_xpath("//div[contains(text(),'密码登录')]")  
    # print("密码登录:",mimadenglu) 

    ActionChains(browser).move_to_element(mimadenglu).click(mimadenglu).perform()

    time.sleep(1)
    browser.find_elements_by_class_name("el-input__inner")[0].send_keys(USERNAME) 
    browser.find_elements_by_class_name("el-input__inner")[1].send_keys(PASSWORD)

    # 找到登录按钮
    denglu = browser.find_element_by_xpath("//span[contains(text(),'登 录')]/..")  
    # print("登录按钮:",denglu)
    denglu.click()

# 没有下一页异常
class NoNextPageException(Exception):
    pass


def nextPage():
    global browser
    # print("本页全部批阅,3秒后自动翻页")
    nextBtn = browser.find_element_by_class_name("btn-next")
    display = nextBtn.is_enabled()
    # print("下一页按钮:",display)
    if display :
        # 模拟鼠标点击
        ActionChains(browser).move_to_element(nextBtn).click(nextBtn).perform()
    else:
        raise NoNextPageException("没有下一页") 

def findHomework():
    '''
    搜索作业
    '''
    global browser
    # 将作业名填到输入框
    inp = browser.find_element_by_class_name("el-input__inner")
    inp.send_keys(HOMEWORK_NAME)

    # 找到搜索按钮,并点击
    # search = browser.find_element_by_class_name("el-button el-button--primary el-button--small")
    search = browser.find_element_by_xpath("//button[@class='el-button el-button--primary el-button--small']")
    # print("search:",search)
    search.click()
    time.sleep(3)

    # 找到作业并点击
    homework = browser.find_element_by_link_text(HOMEWORK_NAME)

    # print("作业:",homework)

    # 模拟鼠标点击
    ActionChains(browser).move_to_element(homework).click(homework).perform()

def findWeiPiGai():
    '''
    查找未批改的作业,找不到自动翻页,一直找到最后一页
    '''
    run = True
    while run:

        table = browser.find_element_by_xpath("//table[@class='el-table__body']")
        # print("table:",table)
        # 获取table所有行数据,
        rows = table.find_elements_by_xpath(".//tr[@class='el-table__row']")

        print("行数:",len(rows))

        for item in rows:

            # 查找未批阅
            weipiyue = item.find_element_by_xpath(".//span[contains(text(),'未批阅')]")
            # 查找已提交
            yitijiao = item.find_element_by_xpath(".//span[text()='已提交']")

            # print("display:",weipiyue.is_displayed(),'--内容:',weipiyue.text)

            dsp_yitijiao = yitijiao.is_displayed()
            dsp_weipiyue = weipiyue.is_displayed()

            # print("未提交::",dsp_weipiyue)
            # 如果有人没提交,就显示
            if not dsp_yitijiao:
                name = item.find_element_by_xpath(".//td[3]/div").text
                print("==警告:",name," 没有提交!")


            # 显示 已提交、未批阅,就开始批阅
            if dsp_yitijiao and dsp_weipiyue:

                # 点击批阅按钮
                piyueLnk = item.find_element_by_xpath(".//span[text()='批阅']")
                # print("批阅超链接",piyueLnk)
                # 点击批阅超链接
                ActionChains(browser).move_to_element(piyueLnk).click(piyueLnk).perform()

                time.sleep(3)
                piyue()
                return


        try:
            time.sleep(3)
            nextPage()
            time.sleep(3)
        except NoNextPageException as e:
            print(e)
            print("退出程序!")
            break

def piyue():
    '''
    填分数,添加成绩,一直循环,直到报错
    '''
    while True:
        inps = browser.find_elements_by_class_name("el-input__inner")
        for item in inps:
            score = random.randint(90 , 95)
            item.send_keys(str(score))
            delay = random.randint(3 , 6)
            time.sleep(delay)

        # 提交分数
        submit = browser.find_element_by_xpath("//div[@class='function-keys']/div[3]")
        print("submit:",submit.text)
        ActionChains(browser).move_to_element(submit).click(submit).perform()

        delay = random.randint(3 , 6)
        time.sleep(delay)


browser = webdriver.Chrome()

login()

time.sleep(3)

browser.get('https://online.zretc.net/teacher/instances/1536660768173326337/jobs/overview')

findHomework()

time.sleep(3)

findWeiPiGai()

原文出处:https://malaoshi.top/show_1IX3ZHKxaS9l.html