Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from selenium import webdriver
- from selenium.common.exceptions import NoSuchElementException
- from selenium.webdriver.common.keys import Keys
- from lxml import html
- from config import *
- def login(usr, pwd):
- # enter in username
- user = browser.find_element_by_css_selector('#user_session_username')
- user.send_keys(usr)
- # enter in password
- password = browser.find_element_by_css_selector('#user_session_password')
- password.send_keys(pwd)
- # click login
- element = browser.find_element_by_css_selector('#user_session_submit')
- element.click()
- def get_page(url):
- browser.get(url)
- def logout():
- browser.get("https://www.vhlcentral.com/logout")
- def check_element_existence(element_path):
- try:
- browser.find_element_by_xpath(element_path)
- except NoSuchElementException:
- return False
- return True
- # start browser
- browser = webdriver.Firefox(executable_path=r'C:\geckodriver.exe')
- # go to vhl home
- get_page("https://www.vhlcentral.com/home")
- # login user
- login(USERNAME, PASSWORD)
- # go to homepage
- get_page('http://descubre3.vhlcentral.com/home/?SS=on')
- # set window name
- window_main = browser.window_handles[0]
- # all urls give 'no assignment' until further notice, vhl only displays 10 assignments at a time
- hw_urls = ['no_assignment'] * 11
- # THIS BLOCK GATHERS ALL URLS FROM USER
- # for every assignment
- for assignment_number in range(11):
- # make sure the link actually exists, if not it will remain as no_assignment
- 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'
- '[' + str(assignment_number) + ']/a'):
- # i use 'assignment_number + 1' so we never try to index the li[0] ([li] starts at 1)
- 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]'
- '/ul/li[' + str(assignment_number) + ']/a')
- # click on link
- hw.click()
- # name popup window as window_new so we can change its state
- window_new = browser.window_handles[1]
- # switch to the popup window
- browser.switch_to.window(window_new)
- # this block makes sure a valid url is passed in
- while True:
- # constantly set url to be window url
- hw_urls[assignment_number] = browser.current_url
- # by default, all tabs have url 'about:blank'
- if hw_urls[assignment_number] == 'about:blank':
- # if the url is still default, continue loop
- continue
- else:
- # if url is valid, break out of loop
- hw_urls[assignment_number] = browser.current_url
- break
- # close window
- browser.close()
- # return to main window
- browser.switch_to.window(window_main)
- print(hw_urls)
- # THIS BLOCK GETS ALL VHL ANSWERS FROM VHL SCHOOL USER
- logout()
- login(VHL, VHL)
- answers_book = [['no answer' for x in range(1)] for y in range(11)]
- # for every assignment
- for assignment_number in range(11):
- # if there is an assignment to be done
- if hw_urls[assignment_number] != 'no_assignment':
- # load assignment page
- get_page(hw_urls[assignment_number])
- # click submit
- submit = browser.find_element_by_name('submitRecital')
- submit.click()
- # convert to soup for web scraping
- html_source = browser.page_source
- tree = html.fromstring(html_source)
- # build a list containing all answers
- # check if answers are classed as correct
- if check_element_existence('//span[@class="correct"]/text()'):
- answers = tree.xpath('//span[@class="correct"]/text()')
- answers_book[assignment_number] = answers
- # check if answers are color: green
- elif check_element_existence('//li[@class="answer_correction"]/label/span/text()'):
- answers = tree.xpath('//li[@class="answer_correction"]/label/span/text()')
- answers_book[assignment_number] = answers
- # THIS BLOCK ENTERS ALL VHL ANSWERS TO USER
- logout()
- login(USERNAME, PASSWORD)
- for assignment_number in range(11):
- if hw_urls[assignment_number] != 'no_assignment':
- # open the page
- get_page(hw_urls[assignment_number])
- html_source = browser.page_source
- tree = html.fromstring(html_source)
- # if form is made of text boxes
- if check_element_existence('//form[contains(@name, "RecitalForm")]//input[contains(@type, "text")]'):
- print(str(assignment_number) + '= text forms')
- input_box = browser.find_element_by_xpath(
- '//form[contains(@name, "RecitalForm")]//input[contains(@type, "text")]')
- for questions in range(len(answers_book[assignment_number])):
- input_box.send_keys(answers_book[assignment_number][questions])
- input_box = browser.find_element_by_xpath('//body')
- input_box.send_keys(Keys.TAB)
- # if form is made up of buttons
- elif check_element_existence('//form[contains(@name, "RecitalForm")]//input[contains(@type, "radio")]'):
- print(str(assignment_number) + '= buttons')
- # get web element of all question options
- options = browser.find_elements_by_xpath('//label/span')
- # for every question in the assignment
- for questions in range(len(answers_book[assignment_number])):
- # for every option in the question
- for option in options:
- # get parent web element
- option_label = option.find_element_by_xpath('..')
- # get id of parent, this id matches the id of the corresponding button
- option_id = option_label.get_attribute('for')
- # select correct button
- input_box = browser.find_element_by_id(option_id)
- # if the book answer is == to the text of the option
- if answers_book[assignment_number][questions] == option.text:
- # click
- input_box.click()
- break
- elif answers_book[assignment_number][questions] != option.text:
- pass
- else:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement