Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package control;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.concurrent.TimeUnit;
- import org.openqa.selenium.By;
- import org.openqa.selenium.JavascriptExecutor;
- import org.openqa.selenium.Keys;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.openqa.selenium.support.ui.ExpectedCondition;
- import org.openqa.selenium.support.ui.ExpectedConditions;
- import org.openqa.selenium.support.ui.WebDriverWait;
- public class Bot {
- private final String user = "joshcooper";
- private final String pass = "qwertyq";
- private final String URL = "https://www.vocabexpress.com/login/";
- private final String learnURL = "https://www.vocabexpress.com/learnvocab/?cid=user-55641&qm=read&ipm=writ";
- private final String userX = "//*[@id='main-body']/div/div/div[2]/form/p[1]/input";
- private final String passX= "//*[@id='main-body']/div/div/div[2]/form/p[2]/input";
- private final String loginX = "//*[@id='main-body']/div/div/div[2]/form/p[3]/input";
- private final String tableX = "//*[@id='topiclist']/div[2]/div[2]";
- private final String wordTableX = "/html/body/div[4]/div[2]/div[1]/table/tbody[2]";
- private final String wordAmountX = "/html/body/div[4]/div[2]/div[1]/table/tbody[1]/tr[4]/td[2]";
- private final String testButtonX = "/html/body/div[4]/div[2]/div[1]/table/tbody[3]/tr[2]/td[2]/button";
- private final String okButtonX = "//*[@id='ms_ctrlbar']/input";
- private final String startButtonX = "//*[@id='ipsb_start']";
- private final String translateTextX = "//*[@id='ip_ansbox']/div[1]/span[1]";
- private final String entryBoxX = "//*[@id='ab_written']/input[1]";
- private final String submitButtonX = "//*[@id='ab_written']/input[2]";
- private final String finishButtonX = "//*[@id='ms_ctrlbar']/input";
- private final String closeButtonX = "/html/body/div[4]/div[1]/div[2]";
- private final String table2X = "//*[@id='topiclist']/div[3]/div[2]";
- private final String table3X = "//*[@id='topiclist']/div[4]/div[2]";
- public Bot(){
- FirefoxDriver driver = new FirefoxDriver();
- WebDriverWait driverWait = new WebDriverWait(driver, 5);
- driver.get(URL);
- waitLoad(driver, driverWait);
- WebElement userField = driver.findElement(By.xpath(userX));
- WebElement passField = driver.findElement(By.xpath(passX));
- WebElement loginButton = driver.findElement(By.xpath(loginX));
- userField.sendKeys(user);
- passField.sendKeys(pass);
- loginButton.click();
- driver.get(learnURL);
- waitLoad(driver, driverWait);
- List<WebElement> allRows = driver.findElements(By.xpath(tableX));
- for(int aa = 0; aa < allRows.size(); aa++){
- allRows = driver.findElements(By.xpath(tableX));
- List<WebElement> allTasks = allRows.get(aa).findElements(By.className("bl_row"));
- for(int ab = 0; ab < allTasks.size(); ab++){
- allRows = driver.findElements(By.xpath(tableX));
- allTasks = allRows.get(aa).findElements(By.className("bl_row"));
- List<WebElement> taskList = allTasks.get(ab).findElements(By.className("bl_sqr"));
- for(int ac = 0; ac < taskList.size(); ac++){
- allRows = driver.findElements(By.xpath(tableX));
- allTasks = allRows.get(aa).findElements(By.className("bl_row"));
- taskList = allTasks.get(ab).findElements(By.className("bl_sqr"));
- List<WebElement> completeAmount = taskList.get(ac).findElements(By.className("bstat_cm"));
- if(completeAmount.size() >= 1) continue;
- taskList.get(ac).click();
- work(driver, driverWait);
- }
- }
- }
- List<WebElement> allRows2 = driver.findElements(By.xpath(table2X));
- for(int aa = 0; aa < allRows2.size(); aa++){
- allRows2 = driver.findElements(By.xpath(table2X));
- List<WebElement> tabList = allRows2.get(aa).findElements(By.className("clicktop"));
- for(int ab = 0; ab < tabList.size(); ab++){
- allRows2 = driver.findElements(By.xpath(table2X));
- tabList = allRows2.get(aa).findElements(By.className("clicktop"));
- tabList.get(ab).click();
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- List<WebElement> allTasks = allRows2.get(aa).findElements(By.className("bl_row"));
- for(int ac = 0; ac < allTasks.size(); ac++){
- allRows2 = driver.findElements(By.xpath(table2X));
- tabList = allRows2.get(aa).findElements(By.className("clicktop"));
- allTasks = allRows2.get(aa).findElements(By.className("bl_row"));
- List<WebElement> taskList = allTasks.get(ac).findElements(By.className("bl_sqr"));
- for(int ad = 0; ad < taskList.size(); ad++){
- allRows2 = driver.findElements(By.xpath(table2X));
- tabList = allRows2.get(aa).findElements(By.className("clicktop"));
- allTasks = allRows2.get(aa).findElements(By.className("bl_row"));
- taskList = allTasks.get(ac).findElements(By.className("bl_sqr"));
- List<WebElement> completeAmount = taskList.get(ad).findElements(By.className("bstat_cm"));
- if(completeAmount.size() >= 1 || !taskList.get(ad).isDisplayed()) continue;
- taskList.get(ad).click();
- work(driver, driverWait);
- }
- }
- tabList.get(ab).click();
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- List<WebElement> allRows3 = driver.findElements(By.xpath(table3X));
- for(int aa = 0; aa < allRows3.size(); aa++){
- allRows3 = driver.findElements(By.xpath(table3X));
- List<WebElement> allTasks = allRows3.get(aa).findElements(By.className("bl_row"));
- for(int ab = 0; ab < allTasks.size(); ab++){
- allRows3 = driver.findElements(By.xpath(table3X));
- allTasks = allRows3.get(aa).findElements(By.className("bl_row"));
- List<WebElement> taskList = allTasks.get(ab).findElements(By.className("bl_sqr"));
- for(int ac = 0; ac < taskList.size(); ac++){
- allRows3 = driver.findElements(By.xpath(table3X));
- allTasks = allRows3.get(aa).findElements(By.className("bl_row"));
- taskList = allTasks.get(ab).findElements(By.className("bl_sqr"));
- List<WebElement> completeAmount = taskList.get(ac).findElements(By.className("bstat_cm"));
- if(completeAmount.size() >= 1) continue;
- taskList.get(ac).click();
- work(driver, driverWait);
- }
- }
- }
- }
- public void work(FirefoxDriver driver, WebDriverWait driverWait){
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- waitLoad(driver, driverWait);
- ArrayList<String> wordList = new ArrayList<String>();
- WebElement wordsTable = driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(wordTableX)));
- List<WebElement> wordsList = wordsTable.findElements(By.className("wpwrd"));
- WebElement wordTotal = driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(wordAmountX)));
- int total = Integer.parseInt(wordTotal.getText())*2;
- do{
- wordList.clear();
- wordsList = wordsTable.findElements(By.className("wpwrd"));
- int n = 0;
- for(WebElement wf : wordsList){
- String s = wf.getText();
- s = s.replaceAll("\\[.*?\\]", "");
- s = s.replaceAll("[\n\r]", "");
- s = s.trim();
- System.out.print(s);
- if(s.charAt(0) == ';'){
- String old = wordList.get(n-1);
- wordList.remove(n-1);
- n--;
- wordList.add(new String(old + s));
- }else{
- wordList.add(s);
- }
- n++;
- }
- }while(wordList.size() != total);
- ArrayList<String[]> wordSet = new ArrayList<String[]>();
- for(int n = 0; n < total/2; n++){
- wordSet.add(new String[2]);
- }
- System.out.println(wordSet.size());
- for(int c = 0; c < wordSet.size(); c++){
- for(int n = c*2; n < c*2+2; n++){
- if(n % 2 == 0){
- wordSet.get(c)[0] = wordList.get(n);
- }else{
- wordSet.get(c)[1] = wordList.get(n);
- }
- }
- }
- for(String[] word : wordSet){
- System.out.println("|" + word[0] + " " + word[1] + "|");
- }
- WebElement testButton = driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(testButtonX)));
- testButton.click();
- waitLoad(driver, driverWait);
- WebElement okButton = driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(okButtonX)));
- okButton.click();
- waitLoad(driver, driverWait);
- WebElement startButton = driver.findElement(By.xpath(startButtonX));
- startButton.click();
- waitLoad(driver, driverWait);
- for(int w = 1; w <= total/2; w++){
- try {
- Thread.sleep(1500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- WebElement translateText = driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(translateTextX)));
- String text = translateText.getText();
- for(Iterator<String[]> iterator = wordSet.iterator(); iterator.hasNext();){
- String[] s = iterator.next();
- if(s[1].equals(text)){
- WebElement entryField = driverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(entryBoxX)));
- entryField.sendKeys(s[0]);
- iterator.remove();
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- entryField.sendKeys(Keys.ENTER);
- try {
- Thread.sleep(2500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- driverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(submitButtonX)));
- driver.switchTo().activeElement().click();
- driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
- driverWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(entryBoxX)));
- waitLoad(driver, driverWait);
- break;
- }
- }
- }
- WebElement finishButton = driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(finishButtonX)));
- finishButton.click();
- waitLoad(driver, driverWait);
- try {
- Thread.sleep(1500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- WebElement closeButton = driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(closeButtonX)));
- closeButton.click();
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- waitLoad(driver, driverWait);
- }
- public static void main(String[] args){
- Bot bot = new Bot();
- }
- public void waitLoad(WebDriver driver, WebDriverWait driverWait){
- ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>(){
- public Boolean apply(WebDriver driver) {
- return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
- }
- };
- try {
- driverWait.until(expectation);
- } catch(Throwable error) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement