|
| 1 | +# Copyright (c) 2017, Shenghao Zou from University of Wisconsin, Madison |
| 2 | +# All rights reserved. |
| 3 | + |
| 4 | +import selenium |
| 5 | +import unicodedata |
| 6 | +from selenium import webdriver |
| 7 | +from selenium.webdriver.common.keys import Keys |
| 8 | +import time |
| 9 | +import sys |
| 10 | +from openpyxl import load_workbook |
| 11 | +from selenium.webdriver.common.by import By |
| 12 | +from selenium.webdriver.support import expected_conditions as EC |
| 13 | +from selenium.webdriver.support.ui import WebDriverWait |
| 14 | +from getpass import getpass |
| 15 | + |
| 16 | +# Change variables here! |
| 17 | +YOUR_CANVAS_NAME = 'Frank' |
| 18 | +COURSE_CODE = '91385' |
| 19 | +CANVAS_HOME_PAGE = "https://canvas.wisc.edu" |
| 20 | +DELETE_COMMENT = 1 |
| 21 | + |
| 22 | +def student_submit(drv, assignment, sid, points, comments): |
| 23 | + global wait |
| 24 | + global DELETE_COMMENT |
| 25 | + if isinstance(comments, unicode): |
| 26 | + comments = unicodedata.normalize('NFKD', comments).encode('ascii','ignore') |
| 27 | + elif not isinstance(comments, str): |
| 28 | + comments = '' |
| 29 | + drv.get(CANVAS_HOME_PAGE + '/courses/{2}/gradebook/speed_grader?assignment_id={0}#%7B%22student_id%22%3A%22{1}%22%7D'.format(assignment, sid, COURSE_CODE)) |
| 30 | + #print 'Please Confirm Login and Load.' |
| 31 | + #a = input() |
| 32 | + #time.sleep(1) |
| 33 | + view_rubric = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, VIEW_RUBRIC_SELECTOR))) |
| 34 | + pts = drv.find_elements_by_css_selector(POINTS_SELECTOR) |
| 35 | + # view_rubric = drv.find_element_by_css_selector(VIEW_RUBRIC_SELECTOR) |
| 36 | + comment = drv.find_element_by_id(COMMENT_ID) |
| 37 | + submit = drv.find_element_by_id(SUBMIT_ID) |
| 38 | + view_rubric.click() |
| 39 | + save = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, SAVE_SELECTOR))) |
| 40 | + i = 0 |
| 41 | + for x in points: |
| 42 | + pts[i].clear() |
| 43 | + pts[i].send_keys(str(x)) |
| 44 | + i = i + 1 |
| 45 | + comment_delete = drv.find_elements_by_css_selector(DELETE_ASSIGNMENT_SELECTOR) |
| 46 | + if DELETE_COMMENT: |
| 47 | + for deletes in comment_delete: |
| 48 | + res = deletes.get_attribute('innerHTML').encode('ascii','ignore') |
| 49 | + if YOUR_CANVAS_NAME in res or 'FRANK' in res: |
| 50 | + deletes_x = deletes.find_element_by_css_selector('.delete_comment_link.icon-x') |
| 51 | + deletes_x.click() |
| 52 | + wait.until(EC.alert_is_present()) |
| 53 | + alert = drv.switch_to.alert |
| 54 | + alert.accept() |
| 55 | + |
| 56 | + if comments != None: |
| 57 | + comment.send_keys(comments) |
| 58 | + #print 'comfirm' |
| 59 | + #a = raw_input() |
| 60 | + save.click() |
| 61 | + submit.click() |
| 62 | + # time.sleep(1) |
| 63 | + |
| 64 | +USER_NAME = 'szou28' # Please change it to your own user name. |
| 65 | + |
| 66 | +print 'USER:',USER_NAME |
| 67 | +print 'Please enter the password:' |
| 68 | +USER_PWD = getpass() |
| 69 | +print 'Saved.' |
| 70 | +TESTS_NUM = int(raw_input('How many rubrics do we have (a number for example 8): ')) |
| 71 | +assignment = int(raw_input('Please enter the assignment ID (for example 199326): ')) |
| 72 | + |
| 73 | +if len(sys.argv) <= 1: |
| 74 | + print 'please give me the file name(WITH .xlsx): ' |
| 75 | + fileName = raw_input() |
| 76 | +else: |
| 77 | + fileName = sys.argv[1] |
| 78 | +print 'You are now submitting assignment id = ' + str(assignment) + ' test rubrics number:' + str(TESTS_NUM) + ' filename:' + fileName |
| 79 | +y = raw_input('confirm? \nPlease enter \'y\' to confirm your submission. \nDon\'t do anything when submitting: ') |
| 80 | +if y != 'y': |
| 81 | + exit() |
| 82 | + |
| 83 | +driver = webdriver.Firefox() |
| 84 | +driver.get(CANVAS_HOME_PAGE) |
| 85 | +driver.maximize_window() |
| 86 | +print 'Login.' |
| 87 | +POINTS_SELECTOR = '.criterion_points.span1.no-margin-bottom' |
| 88 | +VIEW_RUBRIC_SELECTOR = '.toggle_full_rubric.edit.btn' |
| 89 | +COMMENT_ID = 'speedgrader_comment_textarea' |
| 90 | +SUBMIT_ID = 'comment_submit_button' |
| 91 | +SAVE_SELECTOR = '.save_rubric_button.Button.Button--primary' |
| 92 | +DELETE_ASSIGNMENT_SELECTOR = '.comment' |
| 93 | +first_rubric = 1 |
| 94 | + |
| 95 | +wait = WebDriverWait(driver, 45) |
| 96 | +USER_NAME_ID = 'j_username' |
| 97 | +USER_PASSWORD_ID = 'j_password' |
| 98 | +SUBMIT_LOGIN_NAME = '_eventId_proceed' |
| 99 | + |
| 100 | +SID_COL = 'B' |
| 101 | +COM_COL = 'C' |
| 102 | +TEST_COL = 'D' |
| 103 | + |
| 104 | + |
| 105 | +submit = wait.until(EC.element_to_be_clickable((By.NAME, SUBMIT_LOGIN_NAME))) |
| 106 | +username = driver.find_element_by_id(USER_NAME_ID) |
| 107 | +pwd = driver.find_element_by_id(USER_PASSWORD_ID) |
| 108 | +username.send_keys(USER_NAME) |
| 109 | +pwd.send_keys(USER_PWD) |
| 110 | +submit.click() |
| 111 | + |
| 112 | + |
| 113 | +print 'loading files from ' + fileName |
| 114 | +wb = load_workbook(fileName) |
| 115 | +worksheet = wb[wb.get_sheet_names()[0]] |
| 116 | + |
| 117 | +i = 2 |
| 118 | +first = 1 |
| 119 | +while True: |
| 120 | + sid = worksheet[SID_COL + str(i)].value |
| 121 | + if sid == None: |
| 122 | + break |
| 123 | + points = [worksheet[chr(ord(TEST_COL) + t) + str(i)].value for t in range(TESTS_NUM)] |
| 124 | + comments = worksheet[COM_COL + str(i)].value |
| 125 | + print sid |
| 126 | + print points |
| 127 | + print comments |
| 128 | + i = i + 1 |
| 129 | + time.sleep(1) |
| 130 | + student_submit(driver, assignment, str(sid), points, comments) |
| 131 | + |
| 132 | +driver.quit() |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
0 commit comments