Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.36 KB | None | 0 0
  1. package selenium;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Set;
  7. import java.util.concurrent.TimeUnit;
  8.  
  9. import org.openqa.selenium.By;
  10. import org.openqa.selenium.WebDriver;
  11. import org.openqa.selenium.firefox.FirefoxDriver;
  12. //comment the above line and uncomment below line to use Chrome
  13. //import org.openqa.selenium.chrome.ChromeDriver;
  14.  
  15. public class Selenium {
  16.    
  17.     private static final long TIME_SLEEP = 350;
  18.    
  19.     public static void main(String[] args) {
  20.         runProgram();
  21.     }
  22.    
  23.     public static void runProgram() {
  24.         System.setProperty("webdriver.gecko.driver",
  25.                 "/Users/fernandorobertogonzaleztapia/downloads/gecko/geckodriver");
  26.         WebDriver driver = new FirefoxDriver();
  27.         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  28.         //comment the above 2 lines and uncomment below 2 lines to use Chrome
  29.         //System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
  30.         //WebDriver driver = new ChromeDriver();
  31.        
  32.         String baseUrl = "https://login.yahoo.com/config/login?.src=fpctx&.intl=e1&.lang=es-US&.done=https%3A%2F%2Fespanol.yahoo.com";
  33.    
  34.         // launch Fire fox and direct it to the Base URL
  35.         driver.get(baseUrl);
  36.        
  37.         driver.findElement(By.id("login-username")).sendKeys("reinhardsama");
  38.         driver.findElement(By.id("login-signin")).click();
  39.         driver.findElement(By.id("login-passwd")).sendKeys("R31nh4rd");
  40.         driver.findElement(By.id("login-signin")).click();
  41.        
  42.         boolean crashed = false;
  43.        
  44.         while(!crashed) {
  45.             try {
  46.                 restoreCat(driver);
  47.                 changeCat(driver);
  48.             } catch(Exception e) {
  49.                 e.printStackTrace();
  50.                 crashed = true;
  51.             }
  52.         }
  53.        
  54.         System.out.println("The program stopped");
  55.         driver.close();
  56.     }
  57.    
  58.     private static void changeCat(WebDriver driver) {
  59.         for (String url : getUrlsForCatChange()) {
  60.             changeCat(driver, url);
  61.         }
  62.     }
  63.    
  64.     private static void restoreCat(WebDriver driver) {
  65.         for (String url : getUrlsForCatRestore()) {
  66.             restoreCat(driver, url);
  67.         }
  68.     }
  69.    
  70.     private static List<String> getUrlsForCatRestore() {
  71.         List<String> urls = new ArrayList<>();
  72.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522174112AAIhzS9");
  73.         return urls;
  74.     }
  75.    
  76.     private static List<String> getUrlsForCatChange() {
  77.         Set<String> urls = new HashSet<>();
  78.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190518120045AAJM4F7");
  79.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522183849AAikTTb");
  80.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522182217AAdKWJC");
  81.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522182144AAkF6Ln");
  82.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522182054AAB9qzL");
  83.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522181826AA091uo");
  84.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522144820AACeHiJ");
  85.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522135959AAsqAVJ");
  86.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522135644AALO8VT");
  87.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522134439AAXNo9e");
  88.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522134255AAHmMPb");
  89.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522071931AAap2l8");
  90.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522071756AAh913n");
  91.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190522064341AA8lEWa");
  92.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190518115352AA1uT8S");
  93.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190518115154AAdYkST");
  94.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190518115034AAqGEzW");
  95.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190515130614AALP8it");
  96.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190515123848AAzGrfb");
  97.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190508111138AAdiGow");
  98.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190520142317AAL3Hg3");
  99.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190518120536AAFFTMh");
  100.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190518120439AAmQ4Ei");
  101.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190518120300AAZIsgn");
  102.         urls.add("https://mx.answers.yahoo.com/question/index?qid=20190518120151AAWmGZw");
  103.         return new ArrayList<String>(urls);
  104.     }
  105.    
  106.     private static void restoreCat(WebDriver driver, String url) {
  107.          driver.get(url);
  108.          driver.findElement(By.xpath("//*[contains(text(), 'Editar')]")).click();
  109.          sleep(TIME_SLEEP);
  110.          driver.findElement(By.xpath("//*[contains(text(), 'Editar')]")).click();
  111.          sleep(TIME_SLEEP);
  112.          driver.findElement(By.xpath("//*[contains(text(), 'Modificar categoría')]")).click();
  113.          sleep(TIME_SLEEP);
  114.          driver.findElement(By.id("parentCategory-0")).click();
  115.          sleep(TIME_SLEEP);
  116.          driver.findElement(By.id("childCategory-396545444")).click();
  117.          sleep(TIME_SLEEP);
  118.          driver.findElement(By.id("childCategory-396545450")).click();
  119.          sleep(TIME_SLEEP);
  120.          driver.findElement(By.id("ya-cat-submit")).click();
  121.          sleep(TIME_SLEEP);
  122.     }
  123.    
  124.     private static void changeCat(WebDriver driver, String url) {
  125.         driver.get(url);
  126.        
  127.         driver.findElement(By.xpath("//*[contains(text(), 'Editar')]")).click();
  128.         sleep(TIME_SLEEP);
  129.         driver.findElement(By.xpath("//*[contains(text(), 'Editar')]")).click();
  130.         sleep(TIME_SLEEP);
  131.         driver.findElement(By.xpath("//*[contains(text(), 'Modificar categoría')]")).click();
  132.         sleep(TIME_SLEEP);
  133.         driver.findElement(By.id("parentCategory-0")).click();
  134.         sleep(TIME_SLEEP);
  135.         driver.findElement(By.id("childCategory-396545443")).click();
  136.         sleep(TIME_SLEEP);
  137.         driver.findElement(By.id("childCategory-396546020")).click();
  138.         sleep(TIME_SLEEP);
  139.         driver.findElement(By.id("ya-cat-submit")).click();
  140.         sleep(TIME_SLEEP);
  141.     }
  142.    
  143.     private static void sleep(long time) {
  144.         try {
  145.             Thread.sleep(time);
  146.         } catch (InterruptedException e) {
  147.             // TODO Auto-generated catch block
  148.             e.printStackTrace();
  149.         }
  150.     }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement