Advertisement
peteje

Untitled

Nov 26th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.05 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. import sys
  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. #def submit():
  38.    # button = browser.find_element_by_xpath('//input[contains(@class="button", @value="submit")]')
  39.    # button.click()
  40.  
  41. sys.path.append('./')
  42. continue_ask = True
  43.  
  44. print('Peter Edmonds: AutoVHLv0.1\n USE AT YOUR OWN RISK!')
  45.  
  46. while continue_ask:
  47.     USERNAME = input("enter your username: ")
  48.     PASSWORD = input("enter you password: ")
  49.     print('vhl-username:' + str(USERNAME))
  50.     print('vhl-password:' + str(PASSWORD))
  51.     correct = input('is this correct? (y/n)')
  52.     if correct == 'y':
  53.         continue_ask = False
  54.     else:
  55.         pass
  56.  
  57.  
  58. browser_choice = input('(c)hrome or (f)irefox?')
  59.  
  60.  
  61.  
  62.     # start browser
  63. if browser_choice == 'f':
  64.     browser = webdriver.Firefox(executable_path='c:\geckodriver.exe')
  65. elif browser_choice == 'c':
  66.     browser = webdriver.Chrome(executable_path='c:\chromedriver.exe')
  67.  
  68. # go to vhl home
  69. get_page("https://www.vhlcentral.com/home")
  70.  
  71. # login user
  72. login(USERNAME, PASSWORD)
  73.  
  74. # go to homepage
  75. get_page('http://descubre3.vhlcentral.com/home/?SS=on')
  76.  
  77. # set window name
  78. window_main = browser.window_handles[0]
  79.  
  80. # all urls give 'no assignment' until further notice, vhl only displays 10 assignments at a time
  81. hw_urls = ['no_assignment'] * 11
  82.  
  83.  
  84. # THIS BLOCK GATHERS ALL URLS FROM USER
  85.  
  86. # for every assignment
  87. for assignment_number in range(11):
  88.     # make sure the link actually exists, if not it will remain as no_assignment
  89.     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'
  90.                                '[' + str(assignment_number) + ']/a'):
  91.         # i use 'assignment_number + 1' so we never try to index the li[0] ([li] starts at 1)
  92.         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]'
  93.                                            '/ul/li[' + str(assignment_number) + ']/a')
  94.         # click on link
  95.         hw.click()
  96.         # name popup window as window_new so we can change its state
  97.         window_new = browser.window_handles[1]
  98.         # switch to the popup window
  99.         browser.switch_to.window(window_new)
  100.  
  101.         # this block makes sure a valid url is passed in
  102.         while True:
  103.             # constantly set url to be window url
  104.             hw_urls[assignment_number] = browser.current_url
  105.  
  106.             # by default, all tabs have url 'about:blank'
  107.             if hw_urls[assignment_number] == 'about:blank':
  108.  
  109.                 # if the url is still default, continue loop
  110.                 continue
  111.             else:
  112.                 # if url is valid, break out of loop
  113.                 hw_urls[assignment_number] = browser.current_url
  114.                 break
  115.  
  116.         # close window
  117.         browser.close()
  118.  
  119.         # return to main window
  120.         browser.switch_to.window(window_main)
  121.  
  122.  
  123. # THIS BLOCK GETS ALL VHL ANSWERS FROM VHL SCHOOL USER
  124. logout()
  125. login('vhlschool', 'vhlschool')
  126. answers_book = [['no answer' for x in range(1)] for y in range(11)]
  127.  
  128. # for every assignment
  129. for assignment_number in range(11):
  130.     # if there is an assignment to be done
  131.     if hw_urls[assignment_number] != 'no_assignment':
  132.         # load assignment page
  133.         get_page(hw_urls[assignment_number])
  134.  
  135.         # click submit
  136.         submit = browser.find_element_by_name('submitRecital')
  137.         submit.click()
  138.  
  139.         # convert to soup for web scraping
  140.         html_source = browser.page_source
  141.         tree = html.fromstring(html_source)
  142.  
  143.         # build a list containing all answers
  144.         # check if answers are classed as correct
  145.         if check_element_existence('//span[@class="correct"]/text()'):
  146.             answers = tree.xpath('//span[@class="correct"]/text()')
  147.             answers_book[assignment_number] = answers
  148.             f = open("vhl.txt", "a+")
  149.             f.write('assignment #' + str(assignment_number) + ': ')
  150.             for answer in answers:
  151.                 f.write(str(answer) + ', ')
  152.             f.write("\n")
  153.             f.close()
  154.         # check if answers are color: green
  155.         elif check_element_existence('//li[@class="answer_correction"]/label/span/text()'):
  156.             answers = tree.xpath('//li[@class="answer_correction"]/label/span/text()')
  157.             answers_book[assignment_number] = answers
  158.             f = open("vhl.txt", "a+")
  159.             f.write('assignment #' + str(assignment_number) + ': ')
  160.             for answer in answers:
  161.                 f.write(str(answer) + ', ')
  162.             f.write("\n")
  163.             f.close()
  164. for assignment_number in range(11):
  165.     for answer in range(len(answers_book[assignment_number])):
  166.         print('assignment: ' + str(assignment_number) + ', question:' + str(answer))
  167.         try:
  168.             if answers_book[assignment_number][answer].startswith(" ") or answers_book[assignment_number][answer].endswith(" "):
  169.                 try:
  170.                     del answers_book[assignment_number][answer]
  171.                 except IndexError:
  172.                     pass
  173.         except IndexError:
  174.             pass
  175. # THIS BLOCK ENTERS ALL VHL ANSWERS TO USER
  176. logout()
  177. login(USERNAME, PASSWORD)
  178.  
  179. for assignment_number in range(11):
  180.     if hw_urls[assignment_number] != 'no_assignment':
  181.         # open the page
  182.         get_page(hw_urls[assignment_number])
  183.         html_source = browser.page_source
  184.         tree = html.fromstring(html_source)
  185.  
  186.  
  187.         # if form is made of text boxes
  188.         if check_element_existence('//form[contains(@name, "RecitalForm")]//input[contains(@type, "text")]'):
  189.             print(str(assignment_number) + '= text forms')
  190.             input_box = browser.find_element_by_xpath(
  191.                 '//form[contains(@name, "RecitalForm")]//input[contains(@type, "text")]')
  192.             for questions in range(len(answers_book[assignment_number])):
  193.                 input_box.send_keys(answers_book[assignment_number][questions])
  194.                 input_box = browser.find_element_by_xpath('//body')
  195.                 input_box.send_keys(Keys.TAB)
  196.             input()
  197.  
  198.  
  199.  
  200.  
  201.         # if form is made up of buttons
  202.         elif check_element_existence('//form[contains(@name, "RecitalForm")]//input[contains(@type, "radio")]'):
  203.             print(str(assignment_number) + '= buttons')
  204.             # get web element of all question options
  205.             options = browser.find_elements_by_xpath('//label/span')
  206.             # for every question in the assignment
  207.             for questions in range(len(answers_book[assignment_number])):
  208.                 # for every option in the question
  209.                 for option in options:
  210.                     # get parent web element
  211.                     option_label = option.find_element_by_xpath('..')
  212.                     # get id of parent, this id matches the id of the corresponding button
  213.                     option_id = option_label.get_attribute('for')
  214.                     # select correct button
  215.                     input_box = browser.find_element_by_id(option_id)
  216.                     # if the book answer is == to the text of the option
  217.                     if answers_book[assignment_number][questions] == option.text:
  218.                         # click
  219.                         input_box.click()
  220.                         break
  221.             input()
  222.  
  223.     else:
  224.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement