Skip to content

Instantly share code, notes, and snippets.

@hannorein
Created November 27, 2012 19:44
Show Gist options
  • Select an option

  • Save hannorein/4156528 to your computer and use it in GitHub Desktop.

Select an option

Save hannorein/4156528 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from selenium import webdriver
import unittest, time, re
from random import choice
correctanswers = []
def tryitnow ():
# Copy and paste the module url below, including the key:
driver.get("https://www.citiprogram.org/members/learnersII/moduletext.asp")
driver.find_element_by_css_selector("#Table2 > tbody > tr > td > a").click()
labels = driver.find_elements_by_tag_name("label")
questions = []
for td in driver.find_elements_by_tag_name("td"):
if "color: rgb(0, 0, 128); padding-top: 5px;" in td.get_attribute("style"):
questions.append(td.text)
Nquestions = len(questions)
print " --- Found %d question " % Nquestions
numelement = 0
for element in driver.find_elements_by_tag_name("input"):
tagid = element.get_attribute("id")
if "answer" in tagid:
if numelement%4==0:
didfindanswer=0
allpossible = []
print tagid
for label in labels:
if label.get_attribute("for")==tagid:
if label.text.encode('utf-8').strip() in correctanswers:
print "Correct answer found:" + label.text.encode('utf-8')
didfindanswer = 1
element.click()
else:
allpossible.append(element)
#pick random one
if numelement%4==3 and didfindanswer==0:
print "Answer not found. Picking random guess."
choice(allpossible).click()
numelement += 1
driver.find_element_by_id("Submit1").click()
driver = webdriver.Firefox()
driver.implicitly_wait(10)
tryitnow()
# save correct answers
last = ""
last2 = ""
current = ""
for element in driver.find_elements_by_tag_name("td"):
if "Points Earned" in element.text:
print element.text.encode('utf-8')
try:
correctanswer = last2.text.encode('utf-8').strip()
correctanswers.append(correctanswer)
except:
pass
last2 = last
last = current
current = element
# try again
driver.back()
tryitnow()
@hannorein

Copy link
Copy Markdown
Author

This is a script that answers all questions correctly in online quizzed hosted by citiprogram.org. It's badly written but it did the job for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment