微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

利用 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 @R_618_4045@ion
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}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐