Advertisement
Guest User

Untitled

a guest
Nov 26th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.40 KB | None | 0 0
  1. from selenium import webdriver
  2. from selenium.common.exceptions import NoSuchElementException
  3. from selenium.webdriver.common.keys import Keys
  4. from lxml import html
  5. from config import *
  6.  
  7.  
  8. def login(usr, pwd):
  9.     # enter in username
  10.     user = browser.find_element_by_css_selector('#user_session_username')
  11.     user.send_keys(usr)
  12.  
  13.     # enter in password
  14.     password = browser.find_element_by_css_selector('#user_session_password')
  15.     password.send_keys(pwd)
  16.  
  17.     # click login
  18.     element = browser.find_element_by_css_selector('#user_session_submit')
  19.     element.click()
  20.  
  21.  
  22. def get_page(url):
  23.     browser.get(url)
  24.  
  25.  
  26. def logout():
  27.     browser.get("https://www.vhlcentral.com/logout")
  28.  
  29.  
  30. def check_element_existence(element_path):
  31.     try:
  32.         browser.find_element_by_xpath(element_path)
  33.     except NoSuchElementException:
  34.         return False
  35.     return True
  36.  
  37.  
  38. # start browser
  39. browser = webdriver.Firefox(executable_path=r'C:\geckodriver.exe')
  40.  
  41. # go to vhl home
  42. get_page("https://www.vhlcentral.com/home")
  43.  
  44. # login user
  45. login(USERNAME, PASSWORD)
  46.  
  47. # go to homepage
  48. get_page('http://descubre3.vhlcentral.com/home/?SS=on')
  49.  
  50. # set window name
  51. window_main = browser.window_handles[0]
  52.  
  53. # all urls give 'no assignment' until further notice, vhl only displays 10 assignments at a time
  54. hw_urls = ['no_assignment'] * 11
  55.  
  56.  
  57. # THIS BLOCK GATHERS ALL URLS FROM USER
  58.  
  59. # for every assignment
  60. for assignment_number in range(11):
  61.     # make sure the link actually exists, if not it will remain as no_assignment
  62.     if check_element_existence('/html/body/div/div/div/div/div[4]/div[2]/div[1]/div[2]/div[3]/div/dl[1]/ul/li'
  63.                                '[' + str(assignment_number) + ']/a'):
  64.         # i use 'assignment_number + 1' so we never try to index the li[0] ([li] starts at 1)
  65.         hw = browser.find_element_by_xpath('/html/body/div/div/div/div/div[4]/div[2]/div[1]/div[2]/div[3]/div/dl[1]'
  66.                                            '/ul/li[' + str(assignment_number) + ']/a')
  67.         # click on link
  68.         hw.click()
  69.         # name popup window as window_new so we can change its state
  70.         window_new = browser.window_handles[1]
  71.         # switch to the popup window
  72.         browser.switch_to.window(window_new)
  73.  
  74.         # this block makes sure a valid url is passed in
  75.         while True:
  76.             # constantly set url to be window url
  77.             hw_urls[assignment_number] = browser.current_url
  78.  
  79.             # by default, all tabs have url 'about:blank'
  80.             if hw_urls[assignment_number] == 'about:blank':
  81.  
  82.                 # if the url is still default, continue loop
  83.                 continue
  84.             else:
  85.                 # if url is valid, break out of loop
  86.                 hw_urls[assignment_number] = browser.current_url
  87.                 break
  88.  
  89.         # close window
  90.         browser.close()
  91.  
  92.         # return to main window
  93.         browser.switch_to.window(window_main)
  94. print(hw_urls)
  95.  
  96. # THIS BLOCK GETS ALL VHL ANSWERS FROM VHL SCHOOL USER
  97. logout()
  98. login(VHL, VHL)
  99. answers_book = [['no answer' for x in range(1)] for y in range(11)]
  100.  
  101. # for every assignment
  102. for assignment_number in range(11):
  103.     # if there is an assignment to be done
  104.     if hw_urls[assignment_number] != 'no_assignment':
  105.         # load assignment page
  106.         get_page(hw_urls[assignment_number])
  107.  
  108.         # click submit
  109.         submit = browser.find_element_by_name('submitRecital')
  110.         submit.click()
  111.  
  112.         # convert to soup for web scraping
  113.         html_source = browser.page_source
  114.         tree = html.fromstring(html_source)
  115.  
  116.         # build a list containing all answers
  117.         # check if answers are classed as correct
  118.         if check_element_existence('//span[@class="correct"]/text()'):
  119.             answers = tree.xpath('//span[@class="correct"]/text()')
  120.             answers_book[assignment_number] = answers
  121.         # check if answers are color: green
  122.         elif check_element_existence('//li[@class="answer_correction"]/label/span/text()'):
  123.             answers = tree.xpath('//li[@class="answer_correction"]/label/span/text()')
  124.             answers_book[assignment_number] = answers
  125.  
  126.  
  127. # THIS BLOCK ENTERS ALL VHL ANSWERS TO USER
  128. logout()
  129. login(USERNAME, PASSWORD)
  130.  
  131. for assignment_number in range(11):
  132.     if hw_urls[assignment_number] != 'no_assignment':
  133.         # open the page
  134.         get_page(hw_urls[assignment_number])
  135.         html_source = browser.page_source
  136.         tree = html.fromstring(html_source)
  137.  
  138.  
  139.         # if form is made of text boxes
  140.         if check_element_existence('//form[contains(@name, "RecitalForm")]//input[contains(@type, "text")]'):
  141.             print(str(assignment_number) + '= text forms')
  142.             input_box = browser.find_element_by_xpath(
  143.                 '//form[contains(@name, "RecitalForm")]//input[contains(@type, "text")]')
  144.             for questions in range(len(answers_book[assignment_number])):
  145.                 input_box.send_keys(answers_book[assignment_number][questions])
  146.                 input_box = browser.find_element_by_xpath('//body')
  147.                 input_box.send_keys(Keys.TAB)
  148.  
  149.  
  150.         # if form is made up of buttons
  151.         elif check_element_existence('//form[contains(@name, "RecitalForm")]//input[contains(@type, "radio")]'):
  152.             print(str(assignment_number) + '= buttons')
  153.             # get web element of all question options
  154.             options = browser.find_elements_by_xpath('//label/span')
  155.             # for every question in the assignment
  156.             for questions in range(len(answers_book[assignment_number])):
  157.                 # for every option in the question
  158.                 for option in options:
  159.                     # get parent web element
  160.                     option_label = option.find_element_by_xpath('..')
  161.                     # get id of parent, this id matches the id of the corresponding button
  162.                     option_id = option_label.get_attribute('for')
  163.                     # select correct button
  164.                     input_box = browser.find_element_by_id(option_id)
  165.                     # if the book answer is == to the text of the option
  166.                     if answers_book[assignment_number][questions] == option.text:
  167.                         # click
  168.                         input_box.click()
  169.                         break
  170.                     elif answers_book[assignment_number][questions] != option.text:
  171.                         pass
  172.     else:
  173.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement