Advertisement
Guest User

Untitled

a guest
Mar 13th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.26 KB | None | 0 0
  1. import getpass
  2. import sys
  3.  
  4. from bs4 import BeautifulSoup
  5. from selenium import webdriver
  6. from selenium.webdriver.support.ui import Select
  7.  
  8. LOGIN_URL = 'https://auth.vt.edu/login?service=https%3a%2f%2firweb.ir.vt.edu%2fwebtest%2fAuthenticated%2fGradeDistribution.aspx'
  9. URL = 'https://irweb.ir.vt.edu/webtest/Authenticated/GradeDistribution.aspx'
  10. driver = None
  11. page = None
  12. soup = None
  13. table = None
  14. table1 = None
  15. course_list = []
  16. total_pages = 0
  17. page_number = 0
  18. terms = {}
  19.  
  20.  
  21. def login(username, password):
  22.     term_value = []
  23.     term_name = []
  24.     print "Driver is opening........"
  25.     global driver
  26.     driver = webdriver.Firefox()
  27.     driver.get(LOGIN_URL)
  28.     print "Driver is maximizing....."
  29.     driver.maximize_window()
  30.     user_element = driver.find_element_by_id("username")
  31.     pass_element = driver.find_element_by_id("password")
  32.     user_element.send_keys(username)
  33.     pass_element.send_keys(password)
  34.     print "Logging in......"
  35.     pass_element.submit()
  36.     select = driver.find_element_by_xpath("//select[@id='ctl00_ContentPlaceHolder1_ddlTerm']")  # get the select element
  37.     options = select.find_elements_by_tag_name("option")  # get all the options into a list
  38.  
  39.     for option in options:  # iterate over the options, place attribute value in list
  40.         term_value.append(str(option.get_attribute("value")))
  41.         term_name.append(str(option.text))
  42.     global terms
  43.     terms = dict(zip(term_name, term_value))
  44.  
  45. def showSelectionsMenu():
  46.     hide_selection = driver.find_element_by_class_name('hideSelection')
  47.     is_hidden = "none" in hide_selection.get_attribute("style") #true when hide selection is hidden and show selection is active
  48.     if is_hidden:
  49.         driver.find_element_by_class_name('showSelection').click() #click on show selections
  50.  
  51. def chooseTerm(term):
  52.     showSelectionsMenu()
  53.     select_term = Select(driver.find_element_by_id('ctl00_ContentPlaceHolder1_ddlTerm'))
  54.     value = terms[term]
  55.     select_term.select_by_value(value)
  56.     driver.implicitly_wait(5)
  57.     showSelectionsMenu()
  58.     submit = driver.find_element_by_id("ctl00_ContentPlaceHolder1_Submit")
  59.     submit.click()
  60.     update()
  61.     # checkVar()
  62.  
  63.  
  64. def addCourses():
  65.     print "Storing table information......."
  66.     grade_table = table1[32].contents[0]  #Table of all grade data located at index 32
  67.     for row in grade_table.find_all('tr'):
  68.         course = []
  69.         for cell in row:
  70.             if (str(cell.text) == '' or str(cell.text) == 'Subject'):
  71.                 course.append("dontAdd")
  72.             else:
  73.                 course.append(str(cell.text))
  74.         if course[1] != "dontAdd":
  75.             if len(course) > 14: #If row longer 15 then it has extra spaces in the beginning after the first page
  76.                 course.pop(0)    #Pop off the blank cell in the beginning of course
  77.             course_list.append(course)
  78.  
  79.  
  80. ##    x = 1
  81. ##    for row in table[42:43][0].contents[0].contents[0].contents[0]:  # location of table in the document
  82. ##        # print x
  83. ##        course = []
  84. ##        length = len(row.contents)
  85. ##        n = 1
  86. ##        if length == 14:  # The first page has 14 elements and indexes at 0 whereas everything else has 15 elements and indexes at 1
  87. ##            n = 0
  88. ##        # print "Length of row: " + str(length)
  89. ##        for i in range(n, length):
  90. ##            #print "Column number: " + str(i) + " " + row.contents[i].text
  91. ##            if (str(row.contents[i].text) == '' or str(row.contents[i].text) == 'Subject'):
  92. ##                course.append("dontAdd")
  93. ##            else:
  94. ##                course.append(str(row.contents[i].text))
  95. ##        if (course[0] != "dontAdd"):
  96. ##            # print "course[0]: " + course[0]
  97. ##            course_list.append(course)
  98. ##        x += 1
  99.  
  100.         # checkVar()
  101.  
  102.  
  103. def nextPage():
  104.     next_element = driver.find_element_by_id("ctl00_ContentPlaceHolder1_ReportViewer_ctl05_ctl00_Next_ctl00_ctl00")
  105.     next_element.click()
  106.     ready = driver.find_elements_by_id("ctl00_ContentPlaceHolder1_ReportViewer_ctl05_ctl00_CurrentPage")[0].is_enabled()
  107.     while not ready:
  108.         ready = driver.find_elements_by_id("ctl00_ContentPlaceHolder1_ReportViewer_ctl05_ctl00_CurrentPage")[
  109.             0].is_enabled()
  110.     update()
  111.     # checkVar()
  112.  
  113.  
  114.  
  115. def addAllCourses():
  116.     print "Page " + str(page_number) + " of " + str(total_pages)
  117.     addCourses()
  118.     for n in range(total_pages - 1):
  119.         nextPage()
  120.         print "Page " + str(page_number) + " of " + str(total_pages)
  121.         addCourses()
  122.  
  123.  
  124. def finish():
  125.     print "Driver is closing........."
  126.     driver.quit()
  127.  
  128.  
  129. def checkVar():
  130.     print "Driver Status: " + str(driver)
  131.     print "Page Status: " + str(sys.getsizeof(page))
  132.     print "Page #: " + str(page_number)
  133.     print "Soup: " + str(sys.getsizeof(soup))
  134.     print "Table: " + str(sys.getsizeof(table))
  135.  
  136.  
  137.  
  138. def update():
  139.     #Update the soup and table
  140.     global soup
  141.     soup = BeautifulSoup(page, 'html.parser')
  142.     global table
  143.     table = soup.findAll('tr')
  144.     global table1
  145.     table1 = soup.find_all('table')
  146.     #Update the page information
  147.     global page
  148.     page = driver.page_source
  149.     page_t = driver.find_elements_by_id("ctl00_ContentPlaceHolder1_ReportViewer_ctl05_ctl00_TotalPages")[0].text
  150.     global total_pages
  151.     total_pages = int(page_t)
  152.     page_n = driver.find_elements_by_id("ctl00_ContentPlaceHolder1_ReportViewer_ctl05_ctl00_CurrentPage")[0].get_attribute('value')
  153.     global page_number
  154.     page_number = int(page_n)
  155.  
  156.  
  157.  
  158. def go():
  159.     username = raw_input("Username: ")
  160.     password = getpass.win_getpass('Password: ')
  161.     login(username, password)
  162.     print "Log In Successful!"
  163.     n = 0
  164.     for key in terms.viewkeys():
  165.         print n
  166.         n+=1
  167.         print key
  168.  
  169.     term = raw_input("Enter Term: ")
  170.     chooseTerm(term)
  171.     addAllCourses()
  172.     response = raw_input("Do you want to see the data(Y/N): ")
  173.     if (response == "Y"):
  174.         print 'Populating table information.......'
  175.         for course in course_list:
  176.             print course
  177.     finished = 0
  178.     finished = raw_input("Type 1 to quit:")
  179.     while (int(finished) != 1):
  180.         finished = raw_input("Type 1 to quit:")
  181.         term = raw_input("Enter Term: ")
  182.         chooseTerm(term)
  183.     finish()
  184.     print "Finished!!!!!!!"
  185.  
  186. go()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement