Guest User

Untitled

a guest
Mar 2nd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package control;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.concurrent.TimeUnit;
  7.  
  8. import org.openqa.selenium.By;
  9. import org.openqa.selenium.JavascriptExecutor;
  10. import org.openqa.selenium.Keys;
  11. import org.openqa.selenium.WebDriver;
  12. import org.openqa.selenium.WebElement;
  13. import org.openqa.selenium.firefox.FirefoxDriver;
  14. import org.openqa.selenium.support.ui.ExpectedCondition;
  15. import org.openqa.selenium.support.ui.ExpectedConditions;
  16. import org.openqa.selenium.support.ui.WebDriverWait;
  17.  
  18. public class Bot {
  19.    
  20.     private final String user = "joshcooper";
  21.     private final String pass = "qwertyq";
  22.     private final String URL = "https://www.vocabexpress.com/login/";
  23.     private final String learnURL = "https://www.vocabexpress.com/learnvocab/?cid=user-55641&qm=read&ipm=writ";
  24.     private final String userX = "//*[@id='main-body']/div/div/div[2]/form/p[1]/input";
  25.     private final String passX= "//*[@id='main-body']/div/div/div[2]/form/p[2]/input";
  26.     private final String loginX = "//*[@id='main-body']/div/div/div[2]/form/p[3]/input";
  27.     private final String tableX = "//*[@id='topiclist']/div[2]/div[2]";
  28.     private final String wordTableX = "/html/body/div[4]/div[2]/div[1]/table/tbody[2]";
  29.     private final String wordAmountX = "/html/body/div[4]/div[2]/div[1]/table/tbody[1]/tr[4]/td[2]";
  30.     private final String testButtonX = "/html/body/div[4]/div[2]/div[1]/table/tbody[3]/tr[2]/td[2]/button";
  31.     private final String okButtonX = "//*[@id='ms_ctrlbar']/input";
  32.     private final String startButtonX = "//*[@id='ipsb_start']";
  33.     private final String translateTextX = "//*[@id='ip_ansbox']/div[1]/span[1]";
  34.     private final String entryBoxX = "//*[@id='ab_written']/input[1]";
  35.     private final String submitButtonX = "//*[@id='ab_written']/input[2]";
  36.     private final String finishButtonX = "//*[@id='ms_ctrlbar']/input";
  37.     private final String closeButtonX = "/html/body/div[4]/div[1]/div[2]";
  38.     private final String table2X = "//*[@id='topiclist']/div[3]/div[2]";
  39.     private final String table3X = "//*[@id='topiclist']/div[4]/div[2]";
  40.    
  41.     public Bot(){
  42.         FirefoxDriver driver = new FirefoxDriver();
  43.         WebDriverWait driverWait = new WebDriverWait(driver, 5);
  44.         driver.get(URL);
  45.         waitLoad(driver, driverWait);
  46.         WebElement userField = driver.findElement(By.xpath(userX));
  47.         WebElement passField = driver.findElement(By.xpath(passX));
  48.         WebElement loginButton = driver.findElement(By.xpath(loginX));
  49.         userField.sendKeys(user);
  50.         passField.sendKeys(pass);
  51.         loginButton.click();
  52.         driver.get(learnURL);
  53.         waitLoad(driver, driverWait);
  54.         List<WebElement> allRows = driver.findElements(By.xpath(tableX));
  55.         for(int aa = 0; aa < allRows.size(); aa++){
  56.             allRows = driver.findElements(By.xpath(tableX));
  57.             List<WebElement> allTasks = allRows.get(aa).findElements(By.className("bl_row"));
  58.             for(int ab = 0; ab < allTasks.size(); ab++){
  59.                 allRows = driver.findElements(By.xpath(tableX));
  60.                 allTasks = allRows.get(aa).findElements(By.className("bl_row"));
  61.                 List<WebElement> taskList = allTasks.get(ab).findElements(By.className("bl_sqr"));
  62.                 for(int ac = 0; ac < taskList.size(); ac++){
  63.                     allRows = driver.findElements(By.xpath(tableX));
  64.                     allTasks = allRows.get(aa).findElements(By.className("bl_row"));
  65.                     taskList = allTasks.get(ab).findElements(By.className("bl_sqr"));
  66.                     List<WebElement> completeAmount = taskList.get(ac).findElements(By.className("bstat_cm"));
  67.                     if(completeAmount.size() >= 1) continue;
  68.                     taskList.get(ac).click();
  69.                     work(driver, driverWait);
  70.                 }  
  71.             }
  72.         }
  73.         List<WebElement> allRows2 = driver.findElements(By.xpath(table2X));
  74.         for(int aa = 0; aa < allRows2.size(); aa++){
  75.             allRows2 = driver.findElements(By.xpath(table2X));
  76.             List<WebElement> tabList = allRows2.get(aa).findElements(By.className("clicktop"));
  77.             for(int ab = 0; ab < tabList.size(); ab++){
  78.                 allRows2 = driver.findElements(By.xpath(table2X));
  79.                 tabList = allRows2.get(aa).findElements(By.className("clicktop"));
  80.                 tabList.get(ab).click();
  81.                 try {
  82.                     Thread.sleep(1000);
  83.                 } catch (InterruptedException e) {
  84.                     e.printStackTrace();
  85.                 }
  86.                 List<WebElement> allTasks = allRows2.get(aa).findElements(By.className("bl_row"));
  87.                 for(int ac = 0; ac < allTasks.size(); ac++){
  88.                     allRows2 = driver.findElements(By.xpath(table2X));
  89.                     tabList = allRows2.get(aa).findElements(By.className("clicktop"));
  90.                     allTasks = allRows2.get(aa).findElements(By.className("bl_row"));
  91.                     List<WebElement> taskList = allTasks.get(ac).findElements(By.className("bl_sqr"));
  92.                     for(int ad = 0; ad < taskList.size(); ad++){
  93.                         allRows2 = driver.findElements(By.xpath(table2X));
  94.                         tabList = allRows2.get(aa).findElements(By.className("clicktop"));
  95.                         allTasks = allRows2.get(aa).findElements(By.className("bl_row"));
  96.                         taskList = allTasks.get(ac).findElements(By.className("bl_sqr"));
  97.                         List<WebElement> completeAmount = taskList.get(ad).findElements(By.className("bstat_cm"));
  98.                         if(completeAmount.size() >= 1 || !taskList.get(ad).isDisplayed()) continue;
  99.                         taskList.get(ad).click();
  100.                         work(driver, driverWait);
  101.                     }  
  102.                 }
  103.                 tabList.get(ab).click();
  104.                 try {
  105.                     Thread.sleep(1000);
  106.                 } catch (InterruptedException e) {
  107.                     e.printStackTrace();
  108.                 }
  109.             }
  110.         }  
  111.         List<WebElement> allRows3 = driver.findElements(By.xpath(table3X));
  112.         for(int aa = 0; aa < allRows3.size(); aa++){
  113.             allRows3 = driver.findElements(By.xpath(table3X));
  114.             List<WebElement> allTasks = allRows3.get(aa).findElements(By.className("bl_row"));
  115.             for(int ab = 0; ab < allTasks.size(); ab++){
  116.                 allRows3 = driver.findElements(By.xpath(table3X));
  117.                 allTasks = allRows3.get(aa).findElements(By.className("bl_row"));
  118.                 List<WebElement> taskList = allTasks.get(ab).findElements(By.className("bl_sqr"));
  119.                 for(int ac = 0; ac < taskList.size(); ac++){
  120.                     allRows3 = driver.findElements(By.xpath(table3X));
  121.                     allTasks = allRows3.get(aa).findElements(By.className("bl_row"));
  122.                     taskList = allTasks.get(ab).findElements(By.className("bl_sqr"));
  123.                     List<WebElement> completeAmount = taskList.get(ac).findElements(By.className("bstat_cm"));
  124.                     if(completeAmount.size() >= 1) continue;
  125.                     taskList.get(ac).click();
  126.                     work(driver, driverWait);
  127.                 }  
  128.             }
  129.         }  
  130.     }
  131.    
  132.     public void work(FirefoxDriver driver, WebDriverWait driverWait){
  133.         try {
  134.             Thread.sleep(3000);
  135.         } catch (InterruptedException e) {
  136.             e.printStackTrace();
  137.         }
  138.         waitLoad(driver, driverWait);
  139.         ArrayList<String> wordList = new ArrayList<String>();
  140.         WebElement wordsTable =  driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(wordTableX)));
  141.         List<WebElement> wordsList = wordsTable.findElements(By.className("wpwrd"));
  142.         WebElement wordTotal = driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(wordAmountX)));
  143.         int total = Integer.parseInt(wordTotal.getText())*2;
  144.         do{
  145.             wordList.clear();
  146.             wordsList = wordsTable.findElements(By.className("wpwrd"));
  147.             int n = 0;
  148.             for(WebElement wf : wordsList){
  149.                 String s = wf.getText();
  150.                 s = s.replaceAll("\\[.*?\\]", "");
  151.                 s = s.replaceAll("[\n\r]", "");
  152.                 s = s.trim();
  153.                 System.out.print(s);
  154.                 if(s.charAt(0) == ';'){
  155.                     String old = wordList.get(n-1);
  156.                     wordList.remove(n-1);
  157.                     n--;
  158.                     wordList.add(new String(old + s));                     
  159.                 }else{
  160.                     wordList.add(s);
  161.                 }  
  162.                 n++;
  163.             }
  164.         }while(wordList.size() != total);
  165.         ArrayList<String[]> wordSet = new ArrayList<String[]>();
  166.         for(int n = 0; n < total/2; n++){
  167.             wordSet.add(new String[2]);
  168.         }
  169.         System.out.println(wordSet.size());
  170.         for(int c = 0; c < wordSet.size(); c++){
  171.             for(int n = c*2; n < c*2+2; n++){
  172.                 if(n % 2 == 0){
  173.                     wordSet.get(c)[0] = wordList.get(n);
  174.                 }else{
  175.                     wordSet.get(c)[1] = wordList.get(n);
  176.                 }
  177.             }
  178.         }  
  179.         for(String[] word : wordSet){          
  180.             System.out.println("|" + word[0] + "   " + word[1] + "|");
  181.         }
  182.         WebElement testButton = driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(testButtonX)));
  183.         testButton.click();
  184.         waitLoad(driver, driverWait);
  185.         WebElement okButton =  driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(okButtonX)));
  186.         okButton.click();
  187.         waitLoad(driver, driverWait);
  188.         WebElement startButton = driver.findElement(By.xpath(startButtonX));
  189.         startButton.click();
  190.         waitLoad(driver, driverWait);
  191.         for(int w = 1; w <= total/2; w++){
  192.             try {
  193.                 Thread.sleep(1500);
  194.             } catch (InterruptedException e) {
  195.                 e.printStackTrace();
  196.             }
  197.             WebElement translateText = driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(translateTextX)));
  198.             String text = translateText.getText();
  199.  
  200.             for(Iterator<String[]> iterator = wordSet.iterator(); iterator.hasNext();){
  201.                 String[] s = iterator.next();
  202.                 if(s[1].equals(text)){
  203.                     WebElement entryField = driverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(entryBoxX)));
  204.                     entryField.sendKeys(s[0]); 
  205.                     iterator.remove();
  206.                     try {
  207.                         Thread.sleep(1000);
  208.                     } catch (InterruptedException e) {
  209.                         e.printStackTrace();
  210.                     }  
  211.                     entryField.sendKeys(Keys.ENTER);
  212.                     try {
  213.                         Thread.sleep(2500);
  214.                     } catch (InterruptedException e) {
  215.                         e.printStackTrace();
  216.                     }
  217.                     driverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(submitButtonX)));
  218.                     driver.switchTo().activeElement().click();
  219.                     driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  220.                     driverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(entryBoxX)));
  221.                     waitLoad(driver, driverWait);
  222.                     break;
  223.                 }
  224.             }
  225.         }
  226.         WebElement finishButton = driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(finishButtonX)));
  227.         finishButton.click();
  228.         waitLoad(driver, driverWait);
  229.         try {
  230.             Thread.sleep(1500);
  231.         } catch (InterruptedException e) {
  232.             e.printStackTrace();
  233.         }
  234.         WebElement closeButton = driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(closeButtonX)));
  235.         closeButton.click();
  236.         try {
  237.             Thread.sleep(1000);
  238.         } catch (InterruptedException e) {
  239.             e.printStackTrace();
  240.         }
  241.         waitLoad(driver, driverWait);
  242.     }
  243.    
  244.     public static void main(String[] args){
  245.         Bot bot = new Bot();
  246.     }  
  247.    
  248.     public void waitLoad(WebDriver driver, WebDriverWait driverWait){
  249.         ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>(){
  250.             public Boolean apply(WebDriver driver) {
  251.                 return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
  252.             }
  253.         };
  254.         try {
  255.             driverWait.until(expectation);
  256.         } catch(Throwable error) {
  257.                              
  258.         }
  259.     }
  260. }
Add Comment
Please, Sign In to add comment