利用 selenium 自动在 canvas 上提交作业

参考:
你用 Python 写过哪些有趣的脚本? - 景略集智的回答 - 知乎
How to upload file with selenium (Python)?

import time
from selenium import webdriver
from configparser import ConfigParser, ExtendedInterpolation

# 读配置文件
parser = ConfigParser(interpolation=ExtendedInterpolation())
parser.read('提交作业.ini', encoding="utf-8-sig")

# Using Chrome to access web
driver = webdriver.Chrome()
# Open the website
driver.get('https://oc.sjtu.edu.cn/')
time.sleep(3)
# Select the id box
id_box = driver.find_element_by_id('user')
# Send id information
id_box.send_keys(parser['User']['id'])
# Find password box
pass_box = driver.find_element_by_name('pass')
# Send password
pass_box.send_keys(parser['User']['password'])
# Find login button
time.sleep(10)
login_button = driver.find_element_by_id('submit-button')
# Click login
login_button.click()

# Find and click on list of user
user_button = driver.find_element_by_css_selector('#global_nav_profile_link')
user_button.click()
# element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#nav-tray-portal > span > span > span > div > div > ul > li:nth-child(4) > span > a")))
time.sleep(2)
file_button = driver.find_element_by_css_selector('#nav-tray-portal > span > span > span > div > div > ul > li:nth-child(4) > span > a')
file_button.click()

# Choose File button
# 要找 type=file,重点注意
upload_file = driver.find_element_by_css_selector('#content > div > header.ef-header > div > div.ef-actions > span > form > input[type=file]')
filepath = parser['Paths']['filepath']
# Send the file location to the button
upload_file.send_keys(filepath)

提交作业.ini

[User]
id=
password=

[Paths]
home_dir=E:\SJTU
course=入学
filename=毕业证书.jpg
filepath=${home_dir}\${course}\${filename}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值