参考:
你用 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}