vampire_DBR9

Python Vocab Express Bot Using Selenium

Jul 15th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.42 KB | None | 0 0
  1. import time, os
  2. try:
  3.     from selenium import webdriver
  4.     from selenium.webdriver.common.keys import Keys
  5.     from selenium.webdriver.common.by import By
  6.     from selenium.webdriver.support import expected_conditions as EC
  7.     from selenium.webdriver.support.ui import WebDriverWait
  8. except:
  9.     print("Selenium is not installed. \nAttempting to install the Selenium module\n\nTo install it manually, type 'pip install selenium' into the cmd prompt")
  10.     time.sleep(5)
  11.     file = open("installSelenium.bat", "w")
  12.     file.write("pip install selenium")
  13.     file.close()
  14.     os.system("installSelenium.bat")
  15.  
  16. def click(button):browser.find_element_by_xpath(button).click()
  17.  
  18. def waitXPATH(Name):
  19.     element = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, Name)))
  20.     return element
  21. def waitCLASS(Name):
  22.     element = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CLASSNAME, Name)))
  23.     return element
  24. def waitID(Name):
  25.     element = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.ID, Name)))
  26.     return element                                          
  27. def waitNAME(Name):
  28.     element = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.NAME, Name)))
  29.     return element
  30.  
  31. def slowSend(e, message, t):
  32.     chars = 0
  33.     for char in message:
  34.         chars += 1
  35.     timePer = float(t) / float(chars)
  36.     for char in message:
  37.         e.send_keys(char)
  38.         time.sleep(timePer)
  39.  
  40. def work():
  41.     time.sleep(5)
  42.     wordsTable = waitXPATH('/html/body/div[4]/div[2]/div[1]/div/table/tbody[2]')
  43.     wordsList = wordsTable.find_elements_by_class_name('wpwrd')
  44.     wordTotal = waitXPATH('/html/body/div[4]/div[2]/div[1]/div/table/tbody[1]/tr[4]/td[2]')
  45.     total = int(wordTotal.text)*2
  46.     print(total)
  47.     wordList = []
  48.     while len(wordList) < total:
  49.         wordsList = wordsTable.find_elements_by_class_name("wpwrd")
  50.         n = 0
  51.         for wf in wordsList:
  52.             s = wf.text
  53.             s = s.replace("\\[.*?\\]", "")
  54.             s = s.replace("[\n\r]", "")
  55.             s = s.strip()
  56.             if s[0] == ";":
  57.                
  58.                 old = wordList[n-1]
  59.                 wordList.remove(old)
  60.                 n -= 1
  61.                 wordList.append(str(old + s))
  62.             else:
  63.                 wordList.append(s)
  64.             n += 1
  65.     wordSet = []
  66.     n = 0
  67.     while n < total / 2:
  68.         wordSet.append(["", ""])
  69.         n += 1
  70.     c, n = (0, 0)
  71.     while c < len(wordSet):
  72.         n = c*2
  73.         while n < (c*2)+2:
  74.             if n % 2 == 0:
  75.                 wordSet[c][0] = wordList[n]
  76.             else:
  77.                 wordSet[c][1] = wordList[n]
  78.                
  79.             n += 1
  80.         c += 1
  81.     for word in wordSet:
  82.         print("|" + word[0] + "   " + word[1] + "|")
  83.     print("\n" * 2)
  84.     testButton = waitXPATH("/html/body/div[4]/div[2]/div[1]/div/table/tbody[3]/tr[2]/td[2]/button")
  85.     testButton.click()
  86.     time.sleep(10)
  87.     try:click(".//*[@id='launchLearningButton']")
  88.     except:print("No Launch Learning")
  89.     try:click(".//*[@id='inplayAnswersResetModal']/div/div[3]/div[2]/button")
  90.     except:print("No Dismiss button")
  91.     time.sleep(5)
  92.     try:click(".//*[@id='inplaySpecialCharactersModal']/div/div[3]/div[2]/button")
  93.     except:time.sleep(1)
  94.     time.sleep(5)
  95.     w = 1
  96.     while w <= total/2:
  97.         entryBox = waitXPATH(".//*[@id='cardBox']/div/div[contains(@class, 'qaCard active')]/div/div/div[4]/div[1]/div/input")
  98.         translateText = waitXPATH(".//*[@id='cardBox']/div/div[contains(@class, 'qaCard active')]/div/div/div[2]/span")
  99.         text = translateText.text
  100.         print(text)
  101.         for a  in (0, 1):
  102.             for aText in wordSet:
  103.                 aText[a], b, c = aText[a].partition(';')
  104.                 aText[a].strip()
  105.                 if aText[0] == text:answer = aText[1]                
  106.                 elif aText[1] == text:answer = aText[0]
  107.                
  108.         slowSend(entryBox, answer, 20)
  109.         time.sleep(2)
  110.         click(".//*[@id='cardBox']/div/div[contains(@class, 'qaCard active')]/div/div/div[4]/div[1]/button")
  111.         time.sleep(2)
  112.         click(".//*[@id='cardBox']/div/div[contains(@class, 'qaCard active')]/div/div/div[4]/div[1]/button")
  113.         time.sleep(5)
  114.         w += 1
  115.     time.sleep(2)
  116.     click(".//*[@id='inplayCompleteModal']/div/div[2]/div[2]/button")
  117. try:
  118.     index = 0
  119.     for line in open("VocabExpressStuff.txt"):
  120.         line = line.replace("\n", "")
  121.         if index == 0:username = line
  122.         elif index == 1:password = line
  123.         elif index == 2:schoolCode = line
  124.         index += 1
  125. except:
  126.     #First Time Login
  127.     print("Found no previous login attempt")
  128.     username, password, schoolCode = (input("Username: "), input("Password: "), input("School Code:"))
  129.     save = open("VocabExpressStuff.txt", "w")
  130.     save.write(username + "\n" + password + "\n" + schoolCode)
  131.     save.close()
  132.     print("\n\nSaved login details to VocabExpressStuff.txt \nOpen the file to change login details.\n")
  133.     time.sleep(3)    
  134.  
  135. #Testing for Browsers
  136. try:
  137.     browser = webdriver.Firefox()
  138.     print("Using the Firefox Webdriver")
  139.     a = True
  140. except:
  141.     time.sleep(1)
  142.     a = False
  143. try:
  144.     ass
  145.     browser = webdriver.Chrome()
  146.     print("Using the Chrome Webdriver")
  147. except:
  148.     if a == False:
  149.         file = open("setupPaths.bat", "w")
  150.         file.write("SET PATH=%~dp0")
  151.         file.close()
  152.         os.system("setupPaths.bat")
  153.         print("The Chrome Webdriver is not installed\nGet it from here: https://sites.google.com/a/chromium.org/chromedriver/downloads\nPlease download it into the same directory as this file")
  154.         input()
  155.         exit()
  156.  
  157. browser.get('https://www.vocabexpress.com/login/')
  158.  
  159. elem = browser.find_element_by_name('uname')
  160. elem.send_keys(username)
  161. elem = browser.find_element_by_name('pwd')
  162. elem.send_keys(password)
  163. click('.//*[@id=\'main-body\']/div/div/div[2]/form/footer/div[2]/a[1]')
  164. time.sleep(1)
  165. elem = browser.find_element_by_name('ocde')
  166. elem.send_keys(schoolCode + Keys.RETURN)
  167. time.sleep(2)
  168.  
  169. try:
  170.     click(".//*[@id='exit']")
  171. except:
  172.     print("No exit session button")
  173.  
  174. try:
  175.     click('.//*[@id=\'modal-container\']/div[3]/div[2]/button')
  176. except:
  177.     print("No login Notification")
  178.  
  179.  
  180. #Click Learn Vocab
  181. click('.//*[@id=\'mainMenu\']/div/ul/li[2]/a')
  182.  
  183. tablex = '//*[@id=\'topiclist\']/div[2]/div[2]'
  184.  
  185. allRows = browser.find_elements(By.XPATH, tablex)
  186. Aindex = 0
  187. for a in allRows:
  188.     allRows = browser.find_elements(By.XPATH, tablex)
  189.     allTasks = allRows[Aindex].find_elements_by_class_name('bl_row')
  190.     Bindex = 0
  191.     for b in allTasks:
  192.         allRows = browser.find_elements(By.XPATH, tablex)
  193.         allTasks = allRows[Aindex].find_elements_by_class_name('bl_row')
  194.         taskList = allTasks[Bindex].find_elements_by_class_name('bl_sqr')
  195.         Cindex = 0
  196.         for c in taskList:
  197.             allRows = browser.find_elements(By.XPATH, tablex)
  198.             allTasks = allRows[Aindex].find_elements_by_class_name('bl_row')
  199.             taskList = allTasks[Bindex].find_elements_by_class_name('bl_sqr')
  200.             completeAmount = taskList[Cindex].find_elements_by_class_name('bstat_cm')
  201.             if len(completeAmount) >= 1:
  202.                 taskList[Cindex].click()
  203.                 work()
  204.                 browser.get("https://www.vocabexpress.com/learnvocab/")
  205.             Cindex += 1
  206.         Bindex += 1
  207.     Aindex += 1
  208.  
  209. browser.quit()
Add Comment
Please, Sign In to add comment